Files: e729c860b2eaa16901cefd395558a96c683a5cad / tests / wasmContainer.js
3871 bytesRaw
1 | const tape = require('tape') |
2 | const fs = require('fs') |
3 | const Message = require('../message.js') |
4 | const Hypervisor = require('../') |
5 | const WasmContainer = require('../wasmContainer.js') |
6 | |
7 | const level = require('level-browserify') |
8 | const RadixTree = require('dfinity-radix-tree') |
9 | const db = level('./testdb') |
10 | |
11 | let tester |
12 | |
13 | class TestWasmContainer extends WasmContainer { |
14 | constructor (actor) { |
15 | super(actor) |
16 | this._storage = new Map() |
17 | } |
18 | getInterface (funcRef) { |
19 | const orginal = super.getInterface(funcRef) |
20 | return Object.assign(orginal, { |
21 | test: { |
22 | check: (a, b) => { |
23 | tester.equals(a, b) |
24 | } |
25 | } |
26 | }) |
27 | } |
28 | setState (key, ref) { |
29 | const obj = this.refs.get(ref) |
30 | this._storage.set(key, obj) |
31 | } |
32 | getState (key) { |
33 | const obj = this._storage.get(key) |
34 | return this.refs.add(obj) |
35 | } |
36 | } |
37 | |
38 | tape('basic', async t => { |
39 | t.plan(2) |
40 | tester = t |
41 | const expectedState = { |
42 | '/': Buffer.from('4494963fb0e02312510e675fbca8b60b6e03bd00', 'hex') |
43 | } |
44 | |
45 | const tree = new RadixTree({ |
46 | db |
47 | }) |
48 | |
49 | const wasm = fs.readFileSync('./wasm/reciever.wasm') |
50 | |
51 | const hypervisor = new Hypervisor(tree) |
52 | hypervisor.registerContainer(TestWasmContainer) |
53 | |
54 | const {exports} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
55 | |
56 | const message = new Message({ |
57 | funcRef: exports.receive, |
58 | funcArguments: [5] |
59 | }) |
60 | hypervisor.send(message) |
61 | const stateRoot = await hypervisor.createStateRoot() |
62 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
63 | }) |
64 | |
65 | tape('two communicating actors', async t => { |
66 | t.plan(2) |
67 | tester = t |
68 | const expectedState = { |
69 | '/': Buffer.from('123bcbf52421f0ebf0c9a28d6546a3b374f5d56d', 'hex') |
70 | } |
71 | |
72 | const tree = new RadixTree({db}) |
73 | |
74 | const recieverWasm = fs.readFileSync('./wasm/reciever.wasm') |
75 | const callerWasm = fs.readFileSync('./wasm/caller.wasm') |
76 | |
77 | const hypervisor = new Hypervisor(tree) |
78 | hypervisor.registerContainer(TestWasmContainer) |
79 | |
80 | const {exports: receiverExports} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm) |
81 | const {exports: callerExports} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm) |
82 | |
83 | const message = new Message({ |
84 | funcRef: callerExports.call, |
85 | funcArguments: [receiverExports.receive] |
86 | }) |
87 | |
88 | hypervisor.send(message) |
89 | const stateRoot = await hypervisor.createStateRoot() |
90 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
91 | }) |
92 | |
93 | tape.skip('two communicating actors with callback', async t => { |
94 | t.plan(2) |
95 | tester = t |
96 | const expectedState = { |
97 | '/': Buffer.from('f3cc5ba63d6b1737bea2c33bd1942e5488787b82', 'hex') |
98 | } |
99 | |
100 | const tree = new RadixTree({ |
101 | db |
102 | }) |
103 | |
104 | const recieverWasm = fs.readFileSync('./wasm/reciever.wasm') |
105 | const callerWasm = fs.readFileSync('./wasm/caller.wasm') |
106 | |
107 | const hypervisor = new Hypervisor(tree) |
108 | hypervisor.registerContainer(TestWasmContainer) |
109 | |
110 | const {exports: receiverExports} = await hypervisor.createActor(TestWasmContainer.typeId, recieverWasm) |
111 | const {exports: callerExports} = await hypervisor.createActor(TestWasmContainer.typeId, callerWasm) |
112 | |
113 | const message = new Message({ |
114 | funcRef: callerExports.call, |
115 | funcArguments: [receiverExports.receive] |
116 | }) |
117 | |
118 | hypervisor.send(message) |
119 | const stateRoot = await hypervisor.createStateRoot() |
120 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
121 | }) |
122 | |
123 | // Increment a counter. |
124 | tape.skip('increment', async t => { |
125 | const tree = new RadixTree({ |
126 | db |
127 | }) |
128 | |
129 | const wasm = fs.readFileSync('./wasm/counter.wasm') |
130 | |
131 | const hypervisor = new Hypervisor(tree) |
132 | hypervisor.registerContainer(TestWasmContainer) |
133 | |
134 | const {exports} = await hypervisor.createActor(TestWasmContainer.typeId, wasm) |
135 | |
136 | const message = new Message({ |
137 | funcRef: exports.increment, |
138 | funcArguments: [] |
139 | }) |
140 | hypervisor.send(message) |
141 | |
142 | const stateRoot = await hypervisor.createStateRoot() |
143 | t.end() |
144 | |
145 | console.log(stateRoot) |
146 | |
147 | }) |
148 |
Built with git-ssb-web