Files: 7b38bf7ebbc27a6b04424d7feeb21dc16d76fbbb / tests / index.js
4228 bytesRaw
1 | const tape = require('tape') |
2 | const fs = require('fs') |
3 | const Hypervisor = require('primea-hypervisor') |
4 | const Message = require('primea-message') |
5 | const WasmContainer = require('../index.js') |
6 | const testInterface = require('./testInterface.js') |
7 | const level = require('level') |
8 | const RadixTree = require('dfinity-radix-tree') |
9 | const db = level('./testdb') |
10 | |
11 | const tree = new RadixTree({ |
12 | db: db |
13 | }) |
14 | |
15 | class ContainerTestInterface { |
16 | constructor (wasmContainer) { |
17 | this.wasmContainer = wasmContainer |
18 | } |
19 | |
20 | readMem (offset) { |
21 | return this.wasmContainer.getMemory(offset, 1) |
22 | } |
23 | |
24 | writeMem (offset, val) { |
25 | return this.wasmContainer.setMemory(offset, [val]) |
26 | } |
27 | |
28 | numOfReferances () { |
29 | return this.wasmContainer.referanceMap.size |
30 | } |
31 | |
32 | async callback (cb) { |
33 | const promise = new Promise((resolve, reject) => { |
34 | resolve() |
35 | }) |
36 | await this.wasmContainer.pushOpsQueue(promise) |
37 | this.wasmContainer.execute(cb) |
38 | } |
39 | } |
40 | |
41 | tape('wasm container - main', async t => { |
42 | t.plan(1) |
43 | const hypervisor = new Hypervisor(tree) |
44 | const main = fs.readFileSync(`${__dirname}/wasm/run.wasm`) |
45 | hypervisor.registerContainer(WasmContainer, { |
46 | test: testInterface(t) |
47 | }) |
48 | |
49 | let cap = await hypervisor.createActor(WasmContainer.typeId, new Message({ |
50 | data: main |
51 | })) |
52 | hypervisor.send(cap, new Message()) |
53 | }) |
54 | |
55 | tape('referances', async t => { |
56 | t.plan(1) |
57 | const hypervisor = new Hypervisor(tree) |
58 | const main = fs.readFileSync(`${__dirname}/wasm/referances.wasm`) |
59 | hypervisor.registerContainer(WasmContainer, { |
60 | env: ContainerTestInterface, |
61 | test: testInterface(t) |
62 | }) |
63 | |
64 | hypervisor.createActor(WasmContainer.typeId, new Message({ |
65 | data: main |
66 | })) |
67 | }) |
68 | |
69 | tape('wasm container - mem', async t => { |
70 | t.plan(1) |
71 | const hypervisor = new Hypervisor(tree) |
72 | const readMem = fs.readFileSync(`${__dirname}/wasm/readMem.wasm`) |
73 | hypervisor.registerContainer(WasmContainer, { |
74 | env: ContainerTestInterface, |
75 | test: testInterface(t) |
76 | }) |
77 | |
78 | hypervisor.createActor(WasmContainer.typeId, new Message({ |
79 | data: readMem |
80 | })) |
81 | }) |
82 | |
83 | tape('write mem', async t => { |
84 | const hypervisor = new Hypervisor(tree) |
85 | const readMem = fs.readFileSync(`${__dirname}/wasm/writeMem.wasm`) |
86 | |
87 | class WasmContainerNoIdle extends WasmContainer { |
88 | onIdle () {} |
89 | } |
90 | |
91 | hypervisor.registerContainer(WasmContainerNoIdle, { |
92 | env: ContainerTestInterface, |
93 | test: testInterface(t) |
94 | }) |
95 | |
96 | const cap = await hypervisor.createActor(WasmContainerNoIdle.typeId, new Message({ |
97 | data: readMem |
98 | })) |
99 | const actor = await hypervisor.getActor(cap.destId) |
100 | const mem = actor.container.getMemory(0, 1) |
101 | t.equals(mem[0], 9) |
102 | t.end() |
103 | }) |
104 | |
105 | tape('wasm container - callbacks', async t => { |
106 | t.plan(1) |
107 | const hypervisor = new Hypervisor(tree) |
108 | const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) |
109 | hypervisor.registerContainer(WasmContainer, { |
110 | env: ContainerTestInterface, |
111 | test: testInterface(t) |
112 | }) |
113 | |
114 | await hypervisor.createActor(WasmContainer.typeId, new Message({ |
115 | data: callBackWasm |
116 | })) |
117 | }) |
118 | |
119 | tape('wasm container - invalid', async t => { |
120 | t.plan(1) |
121 | const hypervisor = new Hypervisor(tree) |
122 | hypervisor.registerContainer(WasmContainer, { |
123 | env: ContainerTestInterface, |
124 | test: testInterface(t) |
125 | }) |
126 | |
127 | const message = new Message({ |
128 | data: Buffer.from([0]) |
129 | }) |
130 | |
131 | message.on('execution:error', (e) => { |
132 | t.pass('should cature error') |
133 | }) |
134 | |
135 | await hypervisor.createActor(WasmContainer.typeId, message) |
136 | }) |
137 | |
138 | tape('initailize', async t => { |
139 | t.plan(2) |
140 | |
141 | const callBackWasm = fs.readFileSync(`${__dirname}/wasm/callback.wasm`) |
142 | |
143 | class ContainerTestInterface { |
144 | constructor (wasmContainer) { |
145 | this.wasmContainer = wasmContainer |
146 | } |
147 | |
148 | async callback (cb) { |
149 | const promise = new Promise((resolve, reject) => { |
150 | resolve() |
151 | }) |
152 | await this.wasmContainer.pushOpsQueue(promise) |
153 | this.wasmContainer.execute(cb) |
154 | } |
155 | |
156 | static initialize (code) { |
157 | t.deepEquals(code, callBackWasm) |
158 | return code |
159 | } |
160 | } |
161 | |
162 | const hypervisor = new Hypervisor(tree) |
163 | hypervisor.registerContainer(WasmContainer, { |
164 | env: ContainerTestInterface, |
165 | test: testInterface(t) |
166 | }) |
167 | |
168 | await hypervisor.createActor(WasmContainer.typeId, new Message({ |
169 | data: callBackWasm |
170 | })) |
171 | }) |
172 |
Built with git-ssb-web