aboutsummaryrefslogtreecommitdiff
path: root/spec/test3_spec.lua
blob: e19741d631edc55ea11dfc3f7208503aeae3fdff (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
function rungame()
	f = io.popen("bin\\client\\bin\\brokengine_client.exe spec/headless","r")
	d = f:read("*all")
	f:close()
	return d
end

function runboth()
	--print("Running both")
	--Do we have a race condition here? (Can client start and send it's message
	--before the server is ready to accept?
	f1 = io.popen("bin\\server\\bin\\brokengine_server.exe spec/server","r")
	f2 = io.popen("bin\\client\\bin\\brokengine_client.exe spec/headless","r")
	--print("Both ran...")
	d1 = f1:read("*all")
	d2 = f2:read("*all")
	--print("Both read all")
	f1:close()
	f2:close()
	--print("returning")
	return d1, d2
end

function writegame(...)
	f = io.open("spec/headless/init.lua","w")
	data = {"GAME.crashy()"}
	for _,v in pairs({...}) do
		data[#data + 1] = v
	end
	data[#data + 1] = "\nGAME.exit()\n"
	f:write(table.concat(data,"\n"))
	f:close()
end

function writeserver(...)
	f = io.open("spec/server/init.lua","w")
	data = {...}
	data[#data + 1] = "\nGAME.exit()\n"
	f:write(table.concat(data,"\n"))
	f:close()
end

function assert_game_runs()
	test.is_not_nil(rungame():find("\nGoodbye\n$"))
end
function assert_both_run()
	local a,b = runboth()
	assert.is_not_nil(a:find("\nGoodbye\n$"))
	assert.is_not_nil(b:find("\nGoodbye\n$"))
end
describe("networking",function()
	it("should communicate with PAIR sockets",function()
		writeserver([[
			local has_ponged = false
			local socket  = net.newsocket(net.PAIR)
			socket:bind("tcp://127.0.0.1:5555")
			function socket:receive(stream)
				print("Socket receive triggered")
				has_ponged = true
				local message = stream:readstring()
				assert(message == "ping")
				socket:send(function(stream2)
					stream2:writestring("pong")
				end)
			end
			function GAME.tick()
				if has_ponged then
					GAME.exit()
				end
			end
		]])
		--print("writing game...")
		writegame([[
			local socket = net.newsocket(net.PAIR)
			socket:connect("tcp://127.0.0.1:5555")
			function socket:receive(stream)
				local message = stream:readstring()
				assert(message == "pong")
			end
			socket:send(function(stream)
				stream:writestring("ping")
			end)
		]])
		--print("asserting both run...")
		assert_both_run()
	end)
end)