summaryrefslogtreecommitdiff
path: root/spec/channel_spec.lua
blob: 252c12ee9841388752b9d741da7ba3bf633c1f59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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)