Files: 318e88b7d16e694a7a34015a3c1a6b831c91c49e / tests / index.js
6011 bytesRaw
1 | const tape = require('tape') |
2 | const IPFS = require('ipfs') |
3 | const Hypervisor = require('../') |
4 | const Message = require('primea-message') |
5 | |
6 | const node = new IPFS() |
7 | |
8 | class BaseContainer { |
9 | constructor (kernel) { |
10 | this.kernel = kernel |
11 | } |
12 | |
13 | static createState (code) { |
14 | return { |
15 | nonce: [0], |
16 | ports: {} |
17 | } |
18 | } |
19 | } |
20 | |
21 | node.on('error', err => { |
22 | console.log(err) |
23 | }) |
24 | |
25 | node.on('start', () => { |
26 | tape('basic', async t => { |
27 | const message = new Message() |
28 | const expectedState = { |
29 | '/': 'zdpuAntkdU7yBJojcBT5Q9wBhrK56NmLnwpHPKaEGMFnAXpv7' |
30 | } |
31 | |
32 | class testVMContainer extends BaseContainer { |
33 | run (m) { |
34 | t.true(m === message, 'should recive a message') |
35 | } |
36 | } |
37 | |
38 | const hypervisor = new Hypervisor({dag: node.dag}) |
39 | hypervisor.registerContainer('test', testVMContainer) |
40 | |
41 | const rootContainer = await hypervisor.createInstance('test') |
42 | const port = await rootContainer.createPort('test', 'first') |
43 | |
44 | await rootContainer.send(port, message) |
45 | |
46 | const stateRoot = await hypervisor.createStateRoot(rootContainer, Infinity) |
47 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
48 | t.end() |
49 | }) |
50 | |
51 | tape('one child contract', async t => { |
52 | let message = new Message() |
53 | const expectedState = { |
54 | '/': 'zdpuAofSzrBqwYs6z1r28fMeb8z5oSKF6CcWA6m22RqazgoTB' |
55 | } |
56 | let hasResolved = false |
57 | |
58 | class testVMContainer2 extends BaseContainer { |
59 | run (m) { |
60 | t.true(m === message, 'should recive a message 2') |
61 | return new Promise((resolve, reject) => { |
62 | setTimeout(() => { |
63 | this.kernel.incrementTicks(1) |
64 | hasResolved = true |
65 | resolve() |
66 | }, 200) |
67 | }) |
68 | } |
69 | } |
70 | |
71 | class testVMContainer extends BaseContainer { |
72 | async run (m) { |
73 | const port = await this.kernel.createPort('test2', 'child') |
74 | await this.kernel.send(port, m) |
75 | this.kernel.incrementTicks(1) |
76 | } |
77 | } |
78 | |
79 | const hypervisor = new Hypervisor({dag: node.dag}) |
80 | hypervisor.registerContainer('test', testVMContainer) |
81 | hypervisor.registerContainer('test2', testVMContainer2) |
82 | |
83 | let root = await hypervisor.createInstance('test') |
84 | let port = await root.createPort('test', 'first') |
85 | |
86 | await root.send(port, message) |
87 | const stateRoot = await hypervisor.createStateRoot(root, Infinity) |
88 | t.true(hasResolved, 'should resolve before generating the state root') |
89 | t.deepEquals(stateRoot, expectedState, 'expected state') |
90 | |
91 | // test reviving the state |
92 | class testVMContainer3 extends BaseContainer { |
93 | async run (m) { |
94 | const port = this.kernel.ports.get('child') |
95 | await this.kernel.send(port, m) |
96 | this.kernel.incrementTicks(1) |
97 | } |
98 | } |
99 | |
100 | hypervisor.registerContainer('test', testVMContainer3) |
101 | root = await hypervisor.createInstance('test', stateRoot) |
102 | port = await root.ports.get('first') |
103 | |
104 | await root.send(port, message) |
105 | await hypervisor.createStateRoot(root, Infinity) |
106 | |
107 | t.end() |
108 | }) |
109 | |
110 | tape('ping pong', async t => { |
111 | class Ping extends BaseContainer { |
112 | async run (m) { |
113 | let port = this.kernel.ports.get('child') |
114 | if (!port) { |
115 | port = await this.kernel.createPort('pong', 'child') |
116 | } |
117 | |
118 | if (this.kernel.ticks < 100) { |
119 | this.kernel.incrementTicks(1) |
120 | return this.kernel.send(port, new Message()) |
121 | } |
122 | } |
123 | } |
124 | |
125 | class Pong extends BaseContainer { |
126 | run (m) { |
127 | const port = m.fromPort |
128 | return this.kernel.send(port, new Message()) |
129 | } |
130 | } |
131 | |
132 | const hypervisor = new Hypervisor({ |
133 | dag: node.dag |
134 | }) |
135 | |
136 | hypervisor.registerContainer('ping', Ping) |
137 | hypervisor.registerContainer('pong', Pong) |
138 | const root = await hypervisor.createInstance('pong') |
139 | const port = await root.createPort('ping', 'child') |
140 | |
141 | await root.send(port, new Message()) |
142 | await hypervisor.createStateRoot(root, Infinity) |
143 | |
144 | t.end() |
145 | }) |
146 | |
147 | tape('queing multiple messages', async t => { |
148 | let runs = 0 |
149 | |
150 | class Root extends BaseContainer { |
151 | async run (m) { |
152 | const one = this.kernel.createPort('child', 'one') |
153 | const two = this.kernel.createPort('child', 'two') |
154 | const three = this.kernel.createPort('child', 'three') |
155 | |
156 | await Promise.all([ |
157 | this.kernel.send(one, new Message()), |
158 | this.kernel.send(two, new Message()), |
159 | this.kernel.send(three, new Message()) |
160 | ]) |
161 | |
162 | return new Promise((resolve, reject) => { |
163 | setTimeout(() => { |
164 | this.kernel.incrementTicks(2) |
165 | resolve() |
166 | }, 200) |
167 | }) |
168 | } |
169 | } |
170 | |
171 | class Child extends BaseContainer { |
172 | run (m) { |
173 | runs++ |
174 | this.kernel.incrementTicks(2) |
175 | } |
176 | } |
177 | |
178 | const hypervisor = new Hypervisor({ |
179 | dag: node.dag |
180 | }) |
181 | |
182 | hypervisor.registerContainer('root', Root) |
183 | hypervisor.registerContainer('child', Child) |
184 | |
185 | const root = await hypervisor.createInstance('root') |
186 | const port = await root.createPort('root', 'first') |
187 | await root.send(port, new Message()) |
188 | await root.wait(Infinity) |
189 | |
190 | t.equals(runs, 3, 'the number of run should be 3') |
191 | const nonce = await hypervisor.graph.get(root.state, 'ports/first/link/nonce/0') |
192 | t.equals(nonce, 3, 'should have the correct nonce') |
193 | |
194 | t.end() |
195 | }) |
196 | |
197 | tape('traps', async t => { |
198 | class Root extends BaseContainer { |
199 | async run (m) { |
200 | await Promise.all([ |
201 | this.kernel.createPort('root', 'one'), |
202 | this.kernel.createPort('root', 'two'), |
203 | this.kernel.createPort('root', 'three') |
204 | ]) |
205 | |
206 | throw new Error('it is a trap!!!') |
207 | } |
208 | } |
209 | |
210 | const hypervisor = new Hypervisor({ |
211 | dag: node.dag |
212 | }) |
213 | |
214 | hypervisor.registerContainer('root', Root) |
215 | const root = await hypervisor.createInstance('root') |
216 | await root.run() |
217 | |
218 | t.deepEquals(root.state, { |
219 | '/': { |
220 | nonce: [0], |
221 | ports: {} |
222 | } |
223 | }, 'should revert the state') |
224 | |
225 | t.end() |
226 | |
227 | node.stop(() => { |
228 | process.exit() |
229 | }) |
230 | }) |
231 | }) |
232 |
Built with git-ssb-web