165 lines
4.6 KiB
Lua
165 lines
4.6 KiB
Lua
local createLibTest = require('/apis/libtest');
|
|
local createEventLoop = require('/apis/eventloop');
|
|
local createNet = require('/apis/net');
|
|
|
|
local testlib = createLibTest({ ... });
|
|
|
|
local function fakeModem()
|
|
local transmits = {};
|
|
return {
|
|
open = function() end,
|
|
close = function() end,
|
|
transmit = function(channel, replyChannel, payload)
|
|
transmits[#transmits + 1] = {
|
|
channel = channel,
|
|
replyChannel = replyChannel,
|
|
payload = payload,
|
|
};
|
|
end,
|
|
isOpen = function() return true end,
|
|
_transmits = transmits,
|
|
};
|
|
end
|
|
|
|
testlib.test('isPacketOk requires sourceId and routerId', function()
|
|
local modem = fakeModem();
|
|
local net = createNet(createEventLoop(), modem, 'test_modem');
|
|
|
|
testlib.assertEquals(net.isPacketOk(nil), false);
|
|
testlib.assertEquals(net.isPacketOk({}), false);
|
|
testlib.assertEquals(net.isPacketOk({ sourceId = 1 }), false);
|
|
testlib.assertEquals(net.isPacketOk({ sourceId = 1, routerId = 2 }), true);
|
|
end);
|
|
|
|
testlib.test('isPacketOk matches destId by id or label', function()
|
|
local modem = fakeModem();
|
|
local net = createNet(createEventLoop(), modem, 'test_modem');
|
|
|
|
local selfId = os.getComputerID();
|
|
|
|
testlib.assertEquals(
|
|
net.isPacketOk({ sourceId = 9, routerId = 9, destId = selfId }),
|
|
true
|
|
);
|
|
testlib.assertEquals(
|
|
net.isPacketOk({ sourceId = 9, routerId = 9, destId = selfId + 100 }),
|
|
false
|
|
);
|
|
end);
|
|
|
|
testlib.test('send transmits an evt packet on the bus channel', function()
|
|
local modem = fakeModem();
|
|
local net = createNet(createEventLoop(), modem, 'test_modem');
|
|
|
|
net.send('myservice', { hello = 'world' }, { destId = 42 });
|
|
|
|
testlib.assertEquals(#modem._transmits, 1);
|
|
local t = modem._transmits[1];
|
|
testlib.assertEquals(t.channel, net.BUS_CHANNEL);
|
|
testlib.assertEquals(t.replyChannel, net.BUS_CHANNEL);
|
|
testlib.assertEquals(t.payload.service, 'myservice');
|
|
testlib.assertEquals(t.payload.kind, 'evt');
|
|
testlib.assertEquals(t.payload.destId, 42);
|
|
testlib.assertEquals(t.payload.payload.hello, 'world');
|
|
end);
|
|
|
|
testlib.test('setRouter stamps routerId on outbound packets', function()
|
|
local modem = fakeModem();
|
|
local net = createNet(createEventLoop(), modem, 'test_modem');
|
|
|
|
net.send('foo', 'bar', { destId = 42 });
|
|
testlib.assertEquals(modem._transmits[1].payload.routerId, nil);
|
|
|
|
net.setRouter(true);
|
|
net.send('foo', 'bar', { destId = 42 });
|
|
testlib.assertEquals(modem._transmits[2].payload.routerId, os.getComputerID());
|
|
end);
|
|
|
|
testlib.test('serve dispatches a request loopback to a registered handler', function()
|
|
local modem = fakeModem();
|
|
local el = createEventLoop();
|
|
local net = createNet(el, modem, 'test_modem');
|
|
|
|
local received = nil;
|
|
|
|
net.serve('echo', function(payload, reply)
|
|
received = payload;
|
|
reply({ echoed = payload });
|
|
el.stopLoop();
|
|
end);
|
|
|
|
local packet = {
|
|
sourceId = os.getComputerID(),
|
|
sourceLabel = os.getComputerLabel(),
|
|
destId = os.getComputerID(),
|
|
service = 'echo',
|
|
kind = 'req',
|
|
requestId = 'test-req-1',
|
|
payload = { foo = 'bar' },
|
|
routerId = os.getComputerID(),
|
|
};
|
|
os.queueEvent('modem_message', 'test_modem', net.BUS_CHANNEL, net.BUS_CHANNEL, packet, 0);
|
|
|
|
el.runLoop();
|
|
|
|
testlib.assertEquals(received.foo, 'bar');
|
|
end);
|
|
|
|
testlib.test('serve ignores packets with a different service', function()
|
|
local modem = fakeModem();
|
|
local el = createEventLoop();
|
|
local net = createNet(el, modem, 'test_modem');
|
|
|
|
local hit = false;
|
|
|
|
net.serve('echo', function() hit = true end);
|
|
|
|
el.setTimeout(function() el.stopLoop() end, 0);
|
|
|
|
local packet = {
|
|
sourceId = os.getComputerID(),
|
|
sourceLabel = os.getComputerLabel(),
|
|
destId = os.getComputerID(),
|
|
service = 'other',
|
|
kind = 'req',
|
|
requestId = 'test-req-2',
|
|
payload = nil,
|
|
routerId = os.getComputerID(),
|
|
};
|
|
os.queueEvent('modem_message', 'test_modem', net.BUS_CHANNEL, net.BUS_CHANNEL, packet, 0);
|
|
|
|
el.runLoop();
|
|
|
|
testlib.assertEquals(hit, false);
|
|
end);
|
|
|
|
testlib.test('serve ignores unrouted packets (no routerId)', function()
|
|
local modem = fakeModem();
|
|
local el = createEventLoop();
|
|
local net = createNet(el, modem, 'test_modem');
|
|
|
|
local hit = false;
|
|
|
|
net.serve('echo', function() hit = true end);
|
|
|
|
el.setTimeout(function() el.stopLoop() end, 0);
|
|
|
|
local packet = {
|
|
sourceId = os.getComputerID(),
|
|
sourceLabel = os.getComputerLabel(),
|
|
destId = os.getComputerID(),
|
|
service = 'echo',
|
|
kind = 'req',
|
|
requestId = 'test-req-3',
|
|
payload = nil,
|
|
-- no routerId
|
|
};
|
|
os.queueEvent('modem_message', 'test_modem', net.BUS_CHANNEL, net.BUS_CHANNEL, packet, 0);
|
|
|
|
el.runLoop();
|
|
|
|
testlib.assertEquals(hit, false);
|
|
end);
|
|
|
|
testlib.run();
|