The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / liboi / test / timeout.rb @ 90fc8d36
History | View | Annotate | Download (2.03 KB)
1 |
#!/usr/bin/env ruby
|
---|---|
2 |
|
3 |
def test(description) |
4 |
pid = fork do
|
5 |
exec(File.dirname(__FILE__) + "/echo") |
6 |
end
|
7 |
|
8 |
begin
|
9 |
sleep 0.5 # give time for the server to start |
10 |
yield(pid)
|
11 |
rescue
|
12 |
puts "\033[1;31mFAIL\033[m: #{description}"
|
13 |
raise $!
|
14 |
ensure
|
15 |
`kill -9 #{pid}`
|
16 |
end
|
17 |
puts "\033[1;32mPASS\033[m: #{description}"
|
18 |
end
|
19 |
|
20 |
test("make sure echo server works") do |
21 |
socket = TCPSocket.open("localhost", 5000) |
22 |
w = socket.write("hello");
|
23 |
raise "error" unless w == 5 |
24 |
|
25 |
got = socket.recv(5);
|
26 |
raise "error" unless got == "hello" |
27 |
|
28 |
socket.close |
29 |
end
|
30 |
|
31 |
test("doing nothing should not timeout the server") do |pid| |
32 |
10.times do |
33 |
print "."
|
34 |
STDOUT.flush
|
35 |
if Process.waitpid(pid, Process::WNOHANG) |
36 |
raise "server died when it shouldn't have"
|
37 |
end
|
38 |
sleep 1
|
39 |
end
|
40 |
puts ""
|
41 |
end
|
42 |
|
43 |
test("connecting and doing nothing to should timeout in 5 seconds") do |pid| |
44 |
socket = TCPSocket.open("localhost", 5000) |
45 |
i = 0
|
46 |
10.times do |
47 |
print "."
|
48 |
STDOUT.flush
|
49 |
break if Process.waitpid(pid, Process::WNOHANG) |
50 |
sleep 1
|
51 |
i+=1
|
52 |
end
|
53 |
puts ""
|
54 |
raise "died too soon (after #{i} seconds)" if i < 5 |
55 |
raise "died too late (after #{i} seconds)" if i > 6 |
56 |
end
|
57 |
|
58 |
|
59 |
test("connecting and writing once to should timeout in 5 seconds") do |pid| |
60 |
socket = TCPSocket.open("localhost", 5000) |
61 |
w = socket.write("hello");
|
62 |
raise "error" unless w == 5 |
63 |
|
64 |
i = 0
|
65 |
10.times do |
66 |
print "."
|
67 |
STDOUT.flush
|
68 |
break if Process.waitpid(pid, Process::WNOHANG) |
69 |
sleep 1
|
70 |
i+=1
|
71 |
end
|
72 |
puts ""
|
73 |
raise "died too soon (after #{i} seconds)" if i < 5 |
74 |
raise "died too late (after #{i} seconds)" if i > 6 |
75 |
end
|
76 |
|
77 |
test("connecting waiting 3, writing once to should timeout in 8 seconds") do |pid| |
78 |
socket = TCPSocket.open("localhost", 5000) |
79 |
|
80 |
sleep 3
|
81 |
|
82 |
w = socket.write("hello");
|
83 |
raise "error" unless w == 5 |
84 |
|
85 |
i = 0
|
86 |
10.times do |
87 |
print "."
|
88 |
STDOUT.flush
|
89 |
break if Process.waitpid(pid, Process::WNOHANG) |
90 |
sleep 1
|
91 |
i+=1
|
92 |
end
|
93 |
puts ""
|
94 |
raise "died too soon (after #{i} seconds)" if i < 5 |
95 |
raise "died too late (after #{i} seconds)" if i > 6 |
96 |
end
|