forked from Agoric/agoric-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit test for plugin device
- Loading branch information
1 parent
aa37b4f
commit 14b3ab2
Showing
6 changed files
with
218 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* global harden */ | ||
|
||
import { E } from '@agoric/eventual-send'; | ||
import { makePluginManager } from '../../src/vats/plugin-manager'; | ||
|
||
export function buildRootObject(vatPowers, vatParameters) { | ||
const { D } = vatPowers; | ||
const log = vatPowers.testLog; | ||
return harden({ | ||
async bootstrap(vats, devices) { | ||
try { | ||
const { argv } = vatParameters; | ||
if (argv[0] === 'plugin') { | ||
log(`starting plugin test`); | ||
const pluginManager = makePluginManager(devices.plugin, vatPowers); | ||
const { pluginRoot: pingPongP } = await E(pluginManager).load( | ||
'pingpong', | ||
{ | ||
prefix: 'Whoopie ', | ||
}, | ||
); | ||
E(vats.bridge).init(pingPongP); | ||
D(devices.bridge).registerInboundHandler(vats.bridge); | ||
} else { | ||
throw new Error(`unknown argv mode '${argv[0]}'`); | ||
} | ||
} catch (e) { | ||
console.error('have error', e); | ||
throw e; | ||
} | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* global harden */ | ||
|
||
export function bootPlugin() { | ||
return harden({ | ||
start(opts) { | ||
const { prefix } = opts; | ||
return harden({ | ||
ping(msg) { | ||
return `${prefix}${msg}`; | ||
}, | ||
}); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import '@agoric/install-ses'; | ||
import test from 'ava'; | ||
import { initSwingStore } from '@agoric/swing-store-simple'; | ||
|
||
import { buildVatController } from '../../src/index'; | ||
import { buildBridge } from '../../src/devices/bridge'; | ||
import { buildPlugin } from '../../src/devices/plugin'; | ||
|
||
test.before('initialize hostStorage', t => { | ||
const { storage } = initSwingStore(null); | ||
t.context.hostStorage = storage; | ||
}); | ||
|
||
const setupVatController = async t => { | ||
const inputQueue = []; | ||
const queueThunkForKernel = async thunk => { | ||
inputQueue.push(thunk); | ||
}; | ||
|
||
const pluginRequire = mod => { | ||
t.is(mod, 'pingpong'); | ||
// eslint-disable-next-line global-require | ||
return require('./pingpong'); | ||
}; | ||
const plugin = buildPlugin(__dirname, pluginRequire, queueThunkForKernel); | ||
const bridge = buildBridge(); | ||
const config = { | ||
bootstrap: 'bootstrap', | ||
vats: { | ||
bootstrap: { | ||
sourceSpec: require.resolve('./bootstrap'), | ||
}, | ||
bridge: { | ||
sourceSpec: require.resolve('./vat-bridge'), | ||
}, | ||
}, | ||
devices: [ | ||
['plugin', plugin.srcPath, plugin.endowments], | ||
['bridge', bridge.srcPath, bridge.endowments], | ||
], | ||
}; | ||
const c = await buildVatController(config, ['plugin'], { | ||
hostStorage: t.context.hostStorage, | ||
}); | ||
const cycle = async () => { | ||
await c.run(); | ||
while (inputQueue.length) { | ||
inputQueue.shift()(); | ||
// eslint-disable-next-line no-await-in-loop | ||
await c.run(); | ||
} | ||
}; | ||
return { bridge, cycle, dump: c.dump, plugin, queueThunkForKernel }; | ||
}; | ||
|
||
test.serial('plugin first time', async t => { | ||
const { bridge, cycle, dump, queueThunkForKernel } = await setupVatController( | ||
t, | ||
); | ||
|
||
queueThunkForKernel(() => bridge.deliverInbound('pingpong')); | ||
await cycle(); | ||
|
||
t.deepEqual(dump().log, [ | ||
'starting plugin test', | ||
'installing pingPongP', | ||
'starting pingpong test', | ||
'pingpong reply = Whoopie Agoric!', | ||
]); | ||
}); | ||
|
||
test.serial('plugin after restart', async t => { | ||
const { | ||
bridge, | ||
cycle, | ||
dump, | ||
plugin, | ||
queueThunkForKernel, | ||
} = await setupVatController(t); | ||
|
||
plugin.reset(); | ||
queueThunkForKernel(() => bridge.deliverInbound('pingpong')); | ||
await cycle(); | ||
|
||
t.deepEqual(dump().log, [ | ||
'starting plugin test', | ||
'installing pingPongP', | ||
'starting pingpong test', | ||
'pingpong reply = Whoopie Agoric!', | ||
'starting pingpong test', | ||
'pingpong reply = Whoopie Agoric!', | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* global harden */ | ||
|
||
import { E } from '@agoric/eventual-send'; | ||
|
||
export function buildRootObject(vatPowers, _vatParameters) { | ||
const log = vatPowers.testLog; | ||
let pingPongP; | ||
return harden({ | ||
init(pingPong) { | ||
log(`installing pingPongP`); | ||
pingPongP = pingPong; | ||
}, | ||
async inbound(msg) { | ||
try { | ||
switch (msg) { | ||
case 'pingpong': { | ||
log(`starting pingpong test`); | ||
const pong = await E(pingPongP).ping('Agoric!'); | ||
log(`pingpong reply = ${pong}`); | ||
break; | ||
} | ||
default: { | ||
throw new Error(`unknown bridge input ${msg}`); | ||
} | ||
} | ||
} catch (e) { | ||
console.error('failed with', e); | ||
log(`failed: ${e}`); | ||
} | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters