summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorAlexander M Pickering <alex@cogarr.net>2024-12-09 21:48:17 -0600
committerAlexander M Pickering <alex@cogarr.net>2024-12-09 21:48:17 -0600
commit00a3451004d664a5386f3750540facddc0d2686b (patch)
tree7fbe672c24b3721d95a5bb932760e120ba040799 /spec
parent541ef6aaee248626c1a1c6e0db05f5980d09c7a5 (diff)
downloadggj25-00a3451004d664a5386f3750540facddc0d2686b.tar.gz
ggj25-00a3451004d664a5386f3750540facddc0d2686b.tar.bz2
ggj25-00a3451004d664a5386f3750540facddc0d2686b.zip
Add a channel abstraction to use for networking
Diffstat (limited to 'spec')
-rw-r--r--spec/channel_spec.lua80
1 files changed, 67 insertions, 13 deletions
diff --git a/spec/channel_spec.lua b/spec/channel_spec.lua
index 252c12e..d8dbb13 100644
--- a/spec/channel_spec.lua
+++ b/spec/channel_spec.lua
@@ -44,10 +44,18 @@ describe("channel module", function()
assert(#fc.buffer == 1)
assert(fc:recv() == "a")
end)
+ it("has a normal() function that does normal distribution", function()
+ local normal = channel.FaultyChannel.normal
+ for i = 1, 20 do
+ math.randomseed(i)
+ local res = normal(nil,1,0.1)
+ assert(res > 0.5 and res < 1.5)
+ end
+ 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,
+ loss = 0,
avg_latency = 5
})
fc:send("a")
@@ -56,28 +64,27 @@ describe("channel module", function()
fc:pump()
assert(#fc.to_deliver == 1)
assert(#fc.buffer == 0)
-
+ channel.FaultyChannel.time = 4
+ fc:pump()
+ fc:send("b")
+ assert(#fc.to_deliver == 2)
+ assert(#fc.buffer == 0)
+ channel.FaultyChannel.time = 6
+ fc:pump()
+ assert(#fc.to_deliver == 1)
+ assert(#fc.buffer == 1)
end)
- --[[
it("can be configured to not drop messages", function()
channel.FaultyChannel.time = 0
local fc = channel.FaultyChannel({
avg_latency = 5,
- loss_std = 0
+ loss = 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
@@ -100,6 +107,53 @@ describe("channel module", function()
end
assert(j < i)
end)
- ]]
+ it("should keep a queue of messages", function()
+ channel.FaultyChannel.time = 0
+ local fc = channel.FaultyChannel({
+ loss = 0,
+ avg_latency=0,
+ latency_std=0,
+ })
+ local i = 100
+ for j = 1, i do
+ fc:send(tostring(j))
+ end
+ channel.FaultyChannel.time = 1
+ fc:pump()
+ assert(#fc.to_deliver == 0)
+ assert(#fc.buffer == i)
+ for j = 1, i do
+ assert(tonumber(fc:recv()) == j)
+ end
+ assert(not fc:poll())
+ end)
+ it("should deliver messages with different latencies", function()
+ channel.FaultyChannel.time = 0
+ local fc = channel.FaultyChannel({
+ loss = 0,
+ avg_latency = 20,
+ latency_std = 5,
+ })
+ local i = 100
+ for j = 1, i do
+ fc:send(tostring(j))
+ end
+ local received = 0
+ local increment = 0.5
+ local last_received = nil
+ local found_out_of_order = false
+ while received < i do
+ while fc:poll() do
+ local recv = tonumber(fc:recv())
+ if last_received and last_received < recv then
+ found_out_of_order = true
+ end
+ last_received = recv
+ received = received + 1
+ end
+ channel.FaultyChannel.time = channel.FaultyChannel.time + increment
+ end
+ assert(found_out_of_order)
+ end)
end)
end)