tests/index.jsView |
---|
1 | 1 | const tape = require('tape') |
2 | 2 | const AbstractContainer = require('primea-abstract-container') |
3 | 3 | const Message = require('primea-message') |
4 | 4 | const Hypervisor = require('../') |
| 5 | +const CapsManager = require('../capsManager.js') |
5 | 6 | |
6 | 7 | const level = require('level') |
7 | 8 | const RadixTree = require('dfinity-radix-tree') |
8 | 9 | const db = level('./testdb') |
44 | 45 | |
45 | 46 | t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0) |
46 | 47 | }) |
47 | 48 | |
| 49 | +tape('caps manager', async t => { |
| 50 | + const capsManager = new CapsManager({}) |
| 51 | + const cap = {} |
| 52 | + capsManager.store('test', cap) |
| 53 | + const c = capsManager.get('test') |
| 54 | + t.equals(cap, c) |
| 55 | + capsManager.delete('test') |
| 56 | + const empty = capsManager.get('test') |
| 57 | + t.equals(empty, undefined) |
| 58 | + t.end() |
| 59 | +}) |
| 60 | + |
48 | 61 | tape('two communicating actors', async t => { |
49 | 62 | t.plan(3) |
50 | 63 | let message |
51 | 64 | const expectedState = { |
284 | 297 | |
285 | 298 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
286 | 299 | t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0) |
287 | 300 | }) |
| 301 | + |
| 302 | +tape('actor creation', async t => { |
| 303 | + t.plan(3) |
| 304 | + let message |
| 305 | + const expectedState = { |
| 306 | + '/': Buffer.from('8e809b10d473ef4592dc5c1683e89bc7001e5e3e', 'hex') |
| 307 | + } |
| 308 | + |
| 309 | + const tree = new RadixTree({ |
| 310 | + db: db |
| 311 | + }) |
| 312 | + |
| 313 | + class testVMContainerA extends BaseContainer { |
| 314 | + onCreation (m) { |
| 315 | + message = new Message() |
| 316 | + const cap = this.kernel.mintCap() |
| 317 | + message.caps.push(cap) |
| 318 | + return this.kernel.createInstance(testVMContainerB.typeId, message) |
| 319 | + } |
| 320 | + |
| 321 | + onMessage (m) { |
| 322 | + t.true(m, 'should recive a response message') |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + class testVMContainerB extends BaseContainer { |
| 327 | + onCreation (m) { |
| 328 | + const cap = m.caps[0] |
| 329 | + return this.kernel.send(cap, new Message()) |
| 330 | + } |
| 331 | + |
| 332 | + onMessage (m) { |
| 333 | + } |
| 334 | + |
| 335 | + static get typeId () { |
| 336 | + return 8 |
| 337 | + } |
| 338 | + } |
| 339 | + |
| 340 | + const hypervisor = new Hypervisor(tree) |
| 341 | + hypervisor.registerContainer(testVMContainerA) |
| 342 | + hypervisor.registerContainer(testVMContainerB) |
| 343 | + |
| 344 | + await hypervisor.createInstance(testVMContainerA.typeId, new Message()) |
| 345 | + |
| 346 | + const stateRoot = await hypervisor.createStateRoot(Infinity) |
| 347 | + |
| 348 | + t.deepEquals(stateRoot, expectedState, 'expected root!') |
| 349 | + t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0) |
| 350 | +}) |