|
| 1 | +import { makeSQLKernelDatabase } from '@metamask/kernel-store/sqlite/nodejs'; |
| 2 | +import { kunser } from '@metamask/ocap-kernel'; |
| 3 | +import type { ClusterConfig } from '@metamask/ocap-kernel'; |
| 4 | +import { delay } from '@ocap/repo-tools/test-utils'; |
| 5 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 6 | +import { tmpdir } from 'node:os'; |
| 7 | +import { join } from 'node:path'; |
| 8 | +import { describe, it, expect } from 'vitest'; |
| 9 | + |
| 10 | +import { makeTestKernel } from '../helpers/kernel.ts'; |
| 11 | + |
| 12 | +const PROVIDER_BUNDLE = |
| 13 | + 'http://localhost:3000/orphaned-ephemeral-provider-vat.bundle'; |
| 14 | +const CONSUMER_BUNDLE = |
| 15 | + 'http://localhost:3000/orphaned-ephemeral-consumer-vat.bundle'; |
| 16 | + |
| 17 | +const clusterConfig: ClusterConfig = { |
| 18 | + bootstrap: 'consumer', |
| 19 | + vats: { |
| 20 | + provider: { |
| 21 | + bundleSpec: PROVIDER_BUNDLE, |
| 22 | + parameters: {}, |
| 23 | + }, |
| 24 | + consumer: { |
| 25 | + bundleSpec: CONSUMER_BUNDLE, |
| 26 | + parameters: {}, |
| 27 | + }, |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +describe('Orphaned ephemeral exo', { timeout: 30_000 }, () => { |
| 32 | + it('rejects when provider vat restarts', async () => { |
| 33 | + const tempDir = await mkdtemp(join(tmpdir(), 'ocap-ephemeral-')); |
| 34 | + const dbFilename = join(tempDir, 'kernel.db'); |
| 35 | + try { |
| 36 | + const kernel = await makeTestKernel( |
| 37 | + await makeSQLKernelDatabase({ dbFilename }), |
| 38 | + ); |
| 39 | + try { |
| 40 | + const { rootKref, subclusterId } = |
| 41 | + await kernel.launchSubcluster(clusterConfig); |
| 42 | + await delay(); |
| 43 | + |
| 44 | + // Works before restart |
| 45 | + const r1 = await kernel.queueMessage(rootKref, 'useEphemeral', []); |
| 46 | + expect(kunser(r1)).toBe(999); |
| 47 | + |
| 48 | + // Restart only the provider — the consumer still holds the |
| 49 | + // ephemeral ref, but the exo behind it no longer exists. |
| 50 | + const subcluster = kernel.getSubcluster(subclusterId); |
| 51 | + expect(subcluster).toBeDefined(); |
| 52 | + await kernel.restartVat(subcluster!.vats.provider); |
| 53 | + await delay(); |
| 54 | + |
| 55 | + // The consumer's E(ephemeral).increment() targets an orphaned vref. |
| 56 | + // Liveslots in the provider throws "I don't remember allocating", |
| 57 | + // which terminates the provider and rejects the caller's promise. |
| 58 | + await expect( |
| 59 | + kernel.queueMessage(rootKref, 'useEphemeral', []), |
| 60 | + ).rejects.toMatchObject({ |
| 61 | + body: expect.stringContaining("I don't remember allocating"), |
| 62 | + }); |
| 63 | + } finally { |
| 64 | + await kernel.stop(); |
| 65 | + } |
| 66 | + } finally { |
| 67 | + await rm(tempDir, { recursive: true, force: true }); |
| 68 | + } |
| 69 | + }); |
| 70 | +}); |
0 commit comments