diff options
| author | Alexander Pickering <alex@cogarr.net> | 2020-09-15 03:40:00 -0400 |
|---|---|---|
| committer | Alexander Pickering <alex@cogarr.net> | 2020-09-15 03:40:00 -0400 |
| commit | 105ea66aba31825512fea8f281b67f9c2a1d79a5 (patch) | |
| tree | 2bd4d2bd4fa552d770d960420a3c35bf5c91e283 /spec | |
| parent | 94f798fe243313f78f055a48267313e87afe2a67 (diff) | |
| download | lua-nng-105ea66aba31825512fea8f281b67f9c2a1d79a5.tar.gz lua-nng-105ea66aba31825512fea8f281b67f9c2a1d79a5.tar.bz2 lua-nng-105ea66aba31825512fea8f281b67f9c2a1d79a5.zip | |
dos2unix
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/aio_spec.lua | 272 | ||||
| -rw-r--r-- | spec/race_spec.lua | 10 | ||||
| -rw-r--r-- | spec/startup_spec.lua | 294 |
3 files changed, 288 insertions, 288 deletions
diff --git a/spec/aio_spec.lua b/spec/aio_spec.lua index 75e4a0a..093ce59 100644 --- a/spec/aio_spec.lua +++ b/spec/aio_spec.lua @@ -1,136 +1,136 @@ ---[[
-test the asyncronous parts of nnng
-]]
-local nng = require("nng")
-local lanes = require("lanes").configure()
-describe("nng.aio",function()
- it("should be able to create aio object",function()
- print("one")
- local aio = assert(nng.aio.alloc(function() end))
- end)
- it("should accept a callback with arguments",function()
- print("two")
- local aio = assert(nng.aio.alloc(function(a,b,c)
- print("hello")
- end, "one", 2, "three"))
- end)
- --it("should call the callback after sleeping", function()
- --print("three")
- --local called = false
- --local callback = function(lock)
- --called = true
- --end
- --print("about to alloc")
- --local aio = assert(nng.aio.alloc(callback))
- --print("done alloc")
- --print("about to sleep")
- --nng.aio.sleep(1,aio)
- --print("whatever:",whatever)
- ----nng.aio.sleep(1,aio)
- --print("done sleep")
- --os.execute("sleep 1")
- --lock(-1)
- --print("checking called...")
- --print("called was",called)
- --assert(called)
- --end)
- it("should call more than one socket getting a callback at once",function()
- print("checking recv_any callback")
- local s1 = assert(nng.bus0_open())
- assert(s1:listen("tcp://127.0.0.1:4000"))
-
- local s2 = assert(nng.bus0_open())
- assert(s2:listen("tcp://127.0.0.1:4001"))
-
- local s3 = assert(nng.bus0_open())
- assert(s3:dial("tcp://127.0.0.1:4000"))
- assert(s3:dial("tcp://127.0.0.1:4001"))
-
- for i = 1, 100 do --100 times to try to trigger race conditions
- --print("i:",i)
- assert(s3:send("one"))
- assert(s3:send("two"))
- local s1_got_one, s1_got_two, s2_got_one, s2_got_two = false, false, false, false
- while not (s1_got_one and s1_got_two and s2_got_one and s2_got_two) do
- --local socket, message = nng.aio.recv_any(s1, s2)
- --print("about to start recv_any")
- --local tbl = nng.aio.recv_any(s1,s2)
- for socket, message in pairs(nng.aio.recv_any(s1,s2)) do
- --print("in one recv any:",socket, message)
- if socket == s1 then
- if message == "one" then
- s1_got_one = true
- elseif message == "two" then
- s1_got_two = true
- else
- error("message socket 1:" .. message)
- end
- elseif socket == s2 then
- if message == "one" then
- s2_got_one = true
- elseif message == "two" then
- s2_got_two = true
- else
- error("message socket 2:" .. message)
- end
- else
- error("socket:" .. tostring(socket))
- end
- --print("done with recv_any",s1_got_one, s1_got_two, s2_got_one, s2_got_two)
- end
- end
- assert(s1_got_one)
- assert(s1_got_two)
- assert(s2_got_one)
- assert(s2_got_two)
- 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)
-
- ----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
-
- --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)
+--[[ +test the asyncronous parts of nnng +]] +local nng = require("nng") +local lanes = require("lanes").configure() +describe("nng.aio",function() + it("should be able to create aio object",function() + print("one") + local aio = assert(nng.aio.alloc(function() end)) + end) + it("should accept a callback with arguments",function() + print("two") + local aio = assert(nng.aio.alloc(function(a,b,c) + print("hello") + end, "one", 2, "three")) + end) + --it("should call the callback after sleeping", function() + --print("three") + --local called = false + --local callback = function(lock) + --called = true + --end + --print("about to alloc") + --local aio = assert(nng.aio.alloc(callback)) + --print("done alloc") + --print("about to sleep") + --nng.aio.sleep(1,aio) + --print("whatever:",whatever) + ----nng.aio.sleep(1,aio) + --print("done sleep") + --os.execute("sleep 1") + --lock(-1) + --print("checking called...") + --print("called was",called) + --assert(called) + --end) + it("should call more than one socket getting a callback at once",function() + print("checking recv_any callback") + local s1 = assert(nng.bus0_open()) + assert(s1:listen("tcp://127.0.0.1:4000")) + + local s2 = assert(nng.bus0_open()) + assert(s2:listen("tcp://127.0.0.1:4001")) + + local s3 = assert(nng.bus0_open()) + assert(s3:dial("tcp://127.0.0.1:4000")) + assert(s3:dial("tcp://127.0.0.1:4001")) + + for i = 1, 100 do --100 times to try to trigger race conditions + --print("i:",i) + assert(s3:send("one")) + assert(s3:send("two")) + local s1_got_one, s1_got_two, s2_got_one, s2_got_two = false, false, false, false + while not (s1_got_one and s1_got_two and s2_got_one and s2_got_two) do + --local socket, message = nng.aio.recv_any(s1, s2) + --print("about to start recv_any") + --local tbl = nng.aio.recv_any(s1,s2) + for socket, message in pairs(nng.aio.recv_any(s1,s2)) do + --print("in one recv any:",socket, message) + if socket == s1 then + if message == "one" then + s1_got_one = true + elseif message == "two" then + s1_got_two = true + else + error("message socket 1:" .. message) + end + elseif socket == s2 then + if message == "one" then + s2_got_one = true + elseif message == "two" then + s2_got_two = true + else + error("message socket 2:" .. message) + end + else + error("socket:" .. tostring(socket)) + end + --print("done with recv_any",s1_got_one, s1_got_two, s2_got_one, s2_got_two) + end + end + assert(s1_got_one) + assert(s1_got_two) + assert(s2_got_one) + assert(s2_got_two) + 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) + + ----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 + + --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) diff --git a/spec/race_spec.lua b/spec/race_spec.lua index 732ae3e..b0a7f65 100644 --- a/spec/race_spec.lua +++ b/spec/race_spec.lua @@ -1,5 +1,5 @@ ---[[
-Test for race conditions
-]]
-describe("nng",function()
-end)
+--[[ +Test for race conditions +]] +describe("nng",function() +end) diff --git a/spec/startup_spec.lua b/spec/startup_spec.lua index e0faa82..94b0d39 100644 --- a/spec/startup_spec.lua +++ b/spec/startup_spec.lua @@ -1,147 +1,147 @@ ---[[
-test startup of the nng api
-]]
-
-describe("nng",function()
- local nng
- it("should be included with require()",function()
- nng = require("nng")
- end)
- it("should be able to create sockets",function()
- local socket = assert(nng.pair1_open())
- end)
- it("should be able to extablish a connection over inter-process communication",function()
- local s1 = assert(nng.pair1_open())
- local s2 = assert(nng.pair1_open())
- assert(s1:listen("ipc:///tmp/pair.ipc"))
- assert(s2:dial("ipc://tmp/pair.ipc"))
-
- assert(s2:send("hello"))
- local rec = assert(s1:recv())
- assert(rec == "hello","Failed to receive hello, received:" .. rec)
- end)
- it("should be able to use a bus socket to distribute information",function()
- local b = {}
- for i = 1,10 do
- local s = assert(nng.bus0_open())
- b[i] = s
- end
- for i = 1,10 do
- local ipcaddr = string.format("ipc:///tmp/bus_%d.ipc",i)
- assert(b[i]:listen(ipcaddr))
- end
- for i = 1,10 do
- for j = 1,10 do
- if i ~= j then
- local addr = string.format("ipc:///tmp/bus_%d.ipc",j)
- assert(b[i]:dial(addr))
- end
- end
- end
- assert(b[1]:send("Hello"))
- for i = 2,10 do
- local msg = assert(b[i]:recv())
- assert(msg == "Hello")
- end
- end)
- it("should be able to use a survey socket to gather information",function()
- math.randomseed(os.time())
- local s = assert(nng.surveyor0_open())
- assert(s:listen("ipc:///tmp/survey.ipc"))
- local b = {}
- for i = 1,100 do
- local r = assert(nng.respondent0_open())
- assert(r:dial("ipc:///tmp/survey.ipc"))
- b[i] = r
- end
- assert(s:send("Hello"))
- for i = 1,100 do
- local survey = assert(b[i]:recv())
- assert(survey == "Hello")
- assert(b[i]:send(string.format("%f",math.random())))
- end
- local responses = {}
- while true do
- local succ, msg = s:recv(nng.NNG_FLAG_NONBLOCK)
- if succ then
- table.insert(responses,tonumber(succ))
- elseif msg == "Try again" then
- os.execute("sleep 1")
- elseif msg == "Incorrect state" then
- break
- end
- end
- local avg = 0
- for _,v in pairs(responses) do
- avg = avg + v
- end
- avg = avg / #responses
- --avg should be about 0.5
- assert(avg > 0.4, "Average was:" .. avg)
- assert(avg < 0.6, "Average was:" .. avg)
- end)
- it("should be able to use publish and subscribe sockets to transfer information", function()
- --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())
- --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
- num_addr_in_use = num_addr_in_use + 1
- 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")
- end
- 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)
- assert(s1:send("hello 1"))
- local r1 = assert(s2:recv())
- assert.are_equal(r1,"hello 1")
- local r2 = assert(s2:recv())
- assert.are_equal(s2,"hello 1")
- listener:close()
- s1:close()
- s2:close()
- s3:close()
- end
- end)
- describe("socket option",function()
- describe("LOCADDR",function()
- it("should return the local address for a socket",function()
- local nng = require("nng")
- local s1 = assert(nng.bus0_open())
- local s2 = assert(nng.bus0_open())
- local listener = assert(s1:listen("tcp://127.0.0.1:1001"))
- 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")
- assert(s1:send("test"))
- assert(s2:recv() == "test")
- local addr = dialer[nng.NNG_OPT_URL]
- end)
- end)
- end)
- describe("tcp transport",function()
- it("has a keepalive option that prevents the tcp connection from closing #writing2",function()
- local nng = require("nng")
- local s1 = assert(nng.pair1_open())
- local s2 = assert(nng.pair1_open())
- 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"))
- end)
- end)
-end)
+--[[ +test startup of the nng api +]] + +describe("nng",function() + local nng + it("should be included with require()",function() + nng = require("nng") + end) + it("should be able to create sockets",function() + local socket = assert(nng.pair1_open()) + end) + it("should be able to extablish a connection over inter-process communication",function() + local s1 = assert(nng.pair1_open()) + local s2 = assert(nng.pair1_open()) + assert(s1:listen("ipc:///tmp/pair.ipc")) + assert(s2:dial("ipc://tmp/pair.ipc")) + + assert(s2:send("hello")) + local rec = assert(s1:recv()) + assert(rec == "hello","Failed to receive hello, received:" .. rec) + end) + it("should be able to use a bus socket to distribute information",function() + local b = {} + for i = 1,10 do + local s = assert(nng.bus0_open()) + b[i] = s + end + for i = 1,10 do + local ipcaddr = string.format("ipc:///tmp/bus_%d.ipc",i) + assert(b[i]:listen(ipcaddr)) + end + for i = 1,10 do + for j = 1,10 do + if i ~= j then + local addr = string.format("ipc:///tmp/bus_%d.ipc",j) + assert(b[i]:dial(addr)) + end + end + end + assert(b[1]:send("Hello")) + for i = 2,10 do + local msg = assert(b[i]:recv()) + assert(msg == "Hello") + end + end) + it("should be able to use a survey socket to gather information",function() + math.randomseed(os.time()) + local s = assert(nng.surveyor0_open()) + assert(s:listen("ipc:///tmp/survey.ipc")) + local b = {} + for i = 1,100 do + local r = assert(nng.respondent0_open()) + assert(r:dial("ipc:///tmp/survey.ipc")) + b[i] = r + end + assert(s:send("Hello")) + for i = 1,100 do + local survey = assert(b[i]:recv()) + assert(survey == "Hello") + assert(b[i]:send(string.format("%f",math.random()))) + end + local responses = {} + while true do + local succ, msg = s:recv(nng.NNG_FLAG_NONBLOCK) + if succ then + table.insert(responses,tonumber(succ)) + elseif msg == "Try again" then + os.execute("sleep 1") + elseif msg == "Incorrect state" then + break + end + end + local avg = 0 + for _,v in pairs(responses) do + avg = avg + v + end + avg = avg / #responses + --avg should be about 0.5 + assert(avg > 0.4, "Average was:" .. avg) + assert(avg < 0.6, "Average was:" .. avg) + end) + it("should be able to use publish and subscribe sockets to transfer information", function() + --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()) + --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 + num_addr_in_use = num_addr_in_use + 1 + 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") + end + 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) + assert(s1:send("hello 1")) + local r1 = assert(s2:recv()) + assert.are_equal(r1,"hello 1") + local r2 = assert(s2:recv()) + assert.are_equal(s2,"hello 1") + listener:close() + s1:close() + s2:close() + s3:close() + end + end) + describe("socket option",function() + describe("LOCADDR",function() + it("should return the local address for a socket",function() + local nng = require("nng") + local s1 = assert(nng.bus0_open()) + local s2 = assert(nng.bus0_open()) + local listener = assert(s1:listen("tcp://127.0.0.1:1001")) + 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") + assert(s1:send("test")) + assert(s2:recv() == "test") + local addr = dialer[nng.NNG_OPT_URL] + end) + end) + end) + describe("tcp transport",function() + it("has a keepalive option that prevents the tcp connection from closing #writing2",function() + local nng = require("nng") + local s1 = assert(nng.pair1_open()) + local s2 = assert(nng.pair1_open()) + 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")) + end) + end) +end) |
