From 592330a2bd6fc8de987144af0ea8f430a5818988 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Fri, 21 Aug 2020 13:13:26 -0400 Subject: Update unit tests --- spec/aio_spec.lua | 86 +++++++++++++++++++++++++-------------------------- spec/startup_spec.lua | 78 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 105 insertions(+), 59 deletions(-) diff --git a/spec/aio_spec.lua b/spec/aio_spec.lua index 8c7c332..75e4a0a 100644 --- a/spec/aio_spec.lua +++ b/spec/aio_spec.lua @@ -86,51 +86,51 @@ describe("nng.aio",function() end print("Sucessful completion of recv_any test") end) - it("Should accept multiple sockets of dispirate types #writing",function() - local servers = {} - local clients = {} - local messages = {} - local halfs = { - {"rep","req",true}, - {"bus","bus",nil}, - {"surveyor","respondent",false}, - {"pub","sub",false} - } - for i = 1, 10 do - local rng = math.random(#halfs) + --it("Should accept multiple sockets of dispirate types #writing",function() + --local servers = {} + --local clients = {} + --local messages = {} + --local halfs = { + --{"rep","req",true}, + --{"bus","bus",nil}, + --{"surveyor","respondent",false}, + --{"pub","sub",false} + --} + --for i = 1, 10 do + --local rng = math.random(#halfs) - --Create server - servers[i] = assert(nng[halfs[rng][1] .. "0_open"]()) - local url = string.format("tcp://127.0.0.1:%d",4000 + i) - assert(servers[i]:listen(url)) + ----Create server + --servers[i] = assert(nng[halfs[rng][1] .. "0_open"]()) + --local url = string.format("tcp://127.0.0.1:%d",4000 + i) + --assert(servers[i]:listen(url)) - --Create clients - local numclients = math.random(1,3) - clients[i] = {} - messages[i] = {} - for j = 1, numclients do - clients[i][j] = assert(nng[halfs[rng][2] .. "0_open"]()) - assert(clients[i][j]:dial(url)) - messages[i][j] = {} - if halfs[rng][3] == true then - --we send messages - local nummessages = math.random(5) - for k = 1, nummessages do - local message = string.format("ping_%d_%d_%d",i,j,k) - assert(clients[i][j]:send(message)) - table.insert(messages[i][j],message) - end - --elseif halfs[rng][3] == nil then - --we can send or receive messages + ----Create clients + --local numclients = math.random(1,3) + --clients[i] = {} + --messages[i] = {} + --for j = 1, numclients do + --clients[i][j] = assert(nng[halfs[rng][2] .. "0_open"]()) + --assert(clients[i][j]:dial(url)) + --messages[i][j] = {} + --if halfs[rng][3] == true then + ----we send messages + --local nummessages = math.random(5) + --for k = 1, nummessages do + --local message = string.format("ping_%d_%d_%d",i,j,k) + --assert(clients[i][j]:send(message)) + --table.insert(messages[i][j],message) + --end + ----elseif halfs[rng][3] == nil then + ----we can send or receive messages - else - --we receive and reply to messages + --else + ----we receive and reply to messages - end - end - end - for socket, message in pairs(nng.aio.recv_any(table.unpack(servers))) do - print("GOT MESSAGE:",message) - end - end) + --end + --end + --end + --for socket, message in pairs(nng.aio.recv_any(table.unpack(servers))) do + --print("GOT MESSAGE:",message) + --end + --end) end) diff --git a/spec/startup_spec.lua b/spec/startup_spec.lua index 1b3f740..2d2d9d7 100644 --- a/spec/startup_spec.lua +++ b/spec/startup_spec.lua @@ -90,54 +90,100 @@ describe("nng",function() end avg = avg / #responses --avg should be about 0.5 - assert(avg > 0.4) - assert(avg < 0.6) + assert(avg > 0.4, "Average was:" .. avg) + assert(avg < 0.6, "Average was:" .. avg) print("Completed survayor respondent test") end) it("should be able to use publish and subscribe sockets to transfer information", function() print("starting pubsub test") - for i = 1,1000 do + --for i = 1,1000 do + for i = 1,10 do local s1 = assert(nng.pub0_open()) local s2 = assert(nng.sub0_open()) local s3 = assert(nng.sub0_open()) print("everything opened") --local listener, err = s1:listen("tcp://127.0.0.1:1000") local listener, err = s1:listen("ipc:///tmp/pub.ipc") + local dialers = {} local num_addr_in_use = 0 while err == "Address in use" do print("Got addr in use") num_addr_in_use = num_addr_in_use + 1 - if num_addr_in_use > 1000 then + if num_addr_in_use > 10 then error("After multiple attempts, failed to bind on round " .. i) end - --listener, err = s1:listen("tcp://127.0.0.1:1000") - listener, err = s1:listen("ipc:///tmp/pub.ipc") + listener, err = s1:listen("tcp://127.0.0.1:1000") + --listener, err = s1:listen("ipc:///tmp/pub.ipc") end print("s1 is listeneing...") - assert(s2:subscribe("hello")) - assert(s3:subscribe("hello")) - --assert(s2:dial("tcp://127.0.0.1:1000")) - --assert(s3:dial("tcp://127.0.0.1:1000")) - assert(s2:dial("ipc:///tmp/pub.ipc")) - assert(s3:dial("ipc:///tmp/pub.ipc")) + assert(s2:dial("tcp://127.0.0.1:1000")) + assert(s3:dial("tcp://127.0.0.1:1000")) + --local d1 = assert(s2:dial("ipc:///tmp/pub.ipc")) + --local d2 = assert(s3:dial("ipc:///tmp/pub.ipc")) + assert(s2:subscribe("")) + assert(s3:subscribe("")) + table.insert(dialers, d1) + table.insert(dialers, d2) print("Everything set up") assert(s1:send("hello 1")) print("sent") - assert.are_equal(s2:recv(),"hello 1") + local r1 = assert(s2:recv()) + assert.are_equal(r1,"hello 1") print("received 1") - assert.are_equal(s3:recv(),"hello 1") + local r2 = assert(s2:recv()) + assert.are_equal(s2,"hello 1") print("received 2") listener:close() s1:close() + s2:close() + s3:close() end print("Finishing pubsub test") end) + describe("socket option",function() + describe("LOCADDR",function() + it("should return the local address for a socket",function() + print("loc addr test") + local nng = require("nng") + print("open1") + local s1 = assert(nng.bus0_open()) + print("open2") + local s2 = assert(nng.bus0_open()) + print("3") + local listener = assert(s1:listen("tcp://127.0.0.1:1001")) + print("4") + local dialer = assert(s2:dial("tcp://127.0.0.1:1001")) + --local listener = s1:listen("ipc:///tmp/locaddr.ipc") + --local dialer = s2:dial("ipc:///tmp/locaddr.ipc") + print("5") + assert(s1:send("test")) + assert(s2:recv() == "test") + print("s1:",s1) + print("s2:",s2) + local addr = dialer[nng.NNG_OPT_URL] + print("addr:",addr) + print("family:",addr.type) + print("locaddr:",addr.addr) + end) + it("should be able to get the local ip address from google #writing",function() + print("rem addr test") + local nng = require("nng") + local s1 = assert(nng.req0_open()) + assert(s1:dial("tcp://www.ipchicken.com:80")) + assert(s1:send("")) + local data = assert(s1:recv()) + print("data:",data) + end) + end) + end) describe("tcp transport",function() - it("has a keepalive option that prevents the tcp connection from closing",function() + it("has a keepalive option that prevents the tcp connection from closing #writing2",function() + local nng = require("nng") print("starting tcp transport test") local s1 = assert(nng.pair1_open()) local s2 = assert(nng.pair1_open()) - --s1[nng.NNG_OPT_TCP_KEEPALIVE] = true + s1[nng.NNG_OPT_TCP_KEEPALIVE] = true + --print(s1[nng.NNG_OPT_TCP_KEEPALIVE]) assert(s1:listen("tcp://127.0.0.1:1000")) assert(s2:dial("tcp://127.0.0.1:1000")) print("Finished tcp transport test") -- cgit v1.2.3-70-g09d2