summaryrefslogtreecommitdiff
path: root/spec/channel_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/channel_spec.lua')
-rw-r--r--spec/channel_spec.lua105
1 files changed, 105 insertions, 0 deletions
diff --git a/spec/channel_spec.lua b/spec/channel_spec.lua
new file mode 100644
index 0000000..252c12e
--- /dev/null
+++ b/spec/channel_spec.lua
@@ -0,0 +1,105 @@
+
+describe("channel module", function()
+ it("should load",function()
+ require("channel")
+ end)
+ describe("SimpleChannel",function()
+ local channel = require("channel")
+ it("should exist",function()
+ assert(channel.SimpleChannel)
+ assert(channel.SimpleChannel())
+ end)
+ it("should send messages",function()
+ local sc = channel.SimpleChannel()
+ assert(not sc:poll())
+ sc:send("Hello")
+ assert(sc:poll())
+ assert(sc:recv() == "Hello")
+ end)
+ it("should send any string",function()
+ local sc = channel.SimpleChannel()
+ local rng = tostring(math.random())
+ sc:send(rng)
+ assert(sc:recv() == rng)
+ end)
+ end)
+ describe("FaultyChannel",function()
+ local channel = require("channel")
+ it("should exist",function()
+ assert(channel.FaultyChannel)
+ assert(channel.FaultyChannel())
+ end)
+ it("can has a .time that allows it to time travel",function()
+ channel.FaultyChannel.time = 0
+ local fc = channel.FaultyChannel({
+ loss_std = 0,
+ avg_latency = 5
+ })
+ fc:send("a")
+ assert(#fc.to_deliver == 1)
+ assert(#fc.buffer == 0)
+ channel.FaultyChannel.time = 10
+ fc:pump()
+ assert(#fc.to_deliver == 0)
+ assert(#fc.buffer == 1)
+ assert(fc:recv() == "a")
+ end)
+ it("can pump() with no side effects if time has not changed", function()
+ channel.FaultyChannel.time = 0
+ local fc = channel.FaultyChannel({
+ loss_std = 0,
+ avg_latency = 5
+ })
+ fc:send("a")
+ assert(#fc.to_deliver == 1)
+ assert(#fc.buffer == 0)
+ fc:pump()
+ assert(#fc.to_deliver == 1)
+ assert(#fc.buffer == 0)
+
+ end)
+ --[[
+ it("can be configured to not drop messages", function()
+ channel.FaultyChannel.time = 0
+ local fc = channel.FaultyChannel({
+ avg_latency = 5,
+ loss_std = 0
+ })
+ local i = 100
+ for _ = 1,i do
+ fc:send("a")
+ end
+ print(#fc.to_deliver)
+ print(#fc.buffer)
+ fc:pump()
+ print(#fc.to_deliver)
+ print(#fc.buffer)
+ channel.FaultyChannel.time = 10
+ fc:pump()
+ print(#fc.to_deliver)
+ print(#fc.buffer)
+ local j = 0
+ while fc:poll() do
+ j = j + 1
+ fc:recv()
+ end
+ assert(j == i)
+ end)
+ it("should drop messages sometimes",function()
+ local fc = channel.FaultyChannel()
+ local i = 100
+ for _ = 1, i do
+ fc:send("a")
+ end
+ -- Time forward
+ --channel.FaultyChannel.time = math.huge
+ local j = 0
+ while fc:poll() do
+ j = j + 1
+ fc:recv()
+ end
+ assert(j < i)
+ end)
+ ]]
+ end)
+end)