UNPKG

irrelon-reactor-autonet

Version:

A module for automatically creating groups of self-networking services.

99 lines (74 loc) 2.49 kB
"use strict"; var async = require('async'); var AN = require('../es5/index').default; var tb = require('testbear'); tb.config.noCatch = true; tb.test('AutoNet', 'Test instantiate and usage', function (finishTest) { var an1 = new AN('TestNet1'), an2 = new AN('TestNet2'); an1.discover({_id: 'testNet2'}); an2.discover({_id: 'testNet1'}); an1.getRoute('/state', function (req, res) { var body = req.body; tb.ok(true, 'GET /state called in an1'); tb.strictEqual(typeof body, 'object', 'Body sent to get is an object'); tb.strictEqual(body.testData, true, 'Body test data is present'); res.sendStatus(200); an1.broadcast('POST', '/state', { an1TestData: true }, function (err) { tb.ok(!err, 'an1 broadcast no error'); }) }); an2.postRoute('/state', function (req, res) { var body = req.body; tb.ok(true, 'POST /state called in an2'); res.sendStatus(200); tb.strictEqual(typeof body, 'object', 'Body sent to get is an object'); tb.strictEqual(body.an1TestData, true, 'Body test data is present'); tb.expect(11, 'Correct number of tests were run'); finishTest(); }); an1.start('0.0.0.0', 0, function (err, serverAddress) { tb.ok(!err, 'Error did not occur when starting server an1'); an1.on('peerFound', function (peer) { //an1.log('Peer Found: ' + peer.name); //console.log(peer); }); an1.on('peerAdded', function (peer) { //an1.log('Peer Added: ' + peer.name); }); an1.on('clientAdded', function (peer) { //an1.log('Client Added: ' + peer.name); tb.ok(true, "Got client for an1"); }); an2.on('peerFound', function (peer) { //an2.log('Peer Found: ' + peer.name); //console.log(peer); }); an2.on('peerAdded', function (peer) { //an2.log('Peer Added: ' + peer.name); }); an2.on('clientAdded', function (peer) { //an2.log('Client Added: ' + peer.name); tb.ok(true, "Got client for an2"); an2.broadcast('GET', '/state', { testData: true }, function (err, responseArr) { tb.ok(!err, "an2 broadcast no error"); }); }); an1.advertise({ _id: 'testNet1', type: 'a test net service' }); an2.start('0.0.0.0', 0, function (err, serverAddress) { tb.ok(!err, 'Error did not occur when starting server an2'); an2.advertise({ _id: 'testNet2', type: 'a test net service' }); }); }); }); tb.start();