forked from avalanche123/uvrb
- Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpipe_spec.rb
151 lines (122 loc) · 3.2 KB
/
pipe_spec.rb
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require'libuv'
describeLibuv::Pipedo
before:eachdo
@log=[]
@general_failure=[]
@reactor=Libuv::Reactor.new
@server=@reactor.pipe
@client=@reactor.pipe
@timeout=@reactor.timerdo
@reactor.stop
@general_failure << "test timed out"
end
@timeout.start(5000)
@pipefile="/tmp/test-pipes.pipe"
@reactor.all(@server,@client,@timeout).catchdo |reason|
@general_failure << reason.inspect
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
end
@reactor.notifierdo |error,context|
begin
@general_failure << "Log called: #{context}\n#{error.message}\n#{error.backtrace.join("\n")iferror.backtrace}\n"
rescueException=>e
@general_failure << "error in logger #{e.inspect}"
end
end
begin
File.unlink(@pipefile)
rescue
end
end
after:eachdo
begin
File.unlink(@pipefile)
rescue
end
end
describe'bidirectional inter process communication'do
it"should send a ping and return a pong"do
@reactor.run{ |reactor|
@server.bind(@pipefile)do |client|
client.progressdo |data|
@log << data
client.write('pong')
end
client.start_read
end
# catch server errors
@server.catchdo |reason|
@general_failure << reason.inspect
@reactor.stop
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
end
# start listening
@server.listen(1024)
# connect client to server
@client.connect(@pipefile)do |client|
@client.progressdo |data|
@log << data
@client.close
end
@client.start_read
@client.write('ping')
end
@client.catchdo |reason|
@general_failure << reason.inspect
@reactor.stop
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
end
# Stop the reactor once the client handle is closed
@client.finallydo
@server.close
@reactor.stop
end
}
expect(@general_failure).toeq([])
expect(@log).toeq(['ping','pong'])
end
end
# This test won't pass on windows as pipes don't work like this on windows
describe'unidirectional pipeline',:unix_only=>truedo
before:eachdo
system"/usr/bin/mkfifo",@pipefile
end
it"should send work to a consumer"do
@reactor.run{ |reactor|
heartbeat=@reactor.timer
@file1=@reactor.file(@pipefile,File::RDWR|File::NONBLOCK)do
@server.open(@file1.fileno)do |server|
heartbeat.progressdo
@server.write('workload').catchdo |err|
@general_failure << err
end
end
heartbeat.start(0,200)
end
end
@file1.catchdo |e|
@general_failure << "Log called: #{e.inspect} - #{e.message}\n"
end
@file2=@reactor.file(@pipefile,File::RDWR|File::NONBLOCK)do
# connect client to server
@client.open(@file2.fileno)do |consumer|
consumer.progressdo |data|
@log=data
end
consumer.start_read
end
end
timeout=@reactor.timerdo
@server.close
@client.close
timeout.close
heartbeat.close
@reactor.stop
end
timeout.start(1000)
}
expect(@general_failure).toeq([])
expect(@log).toeq('workload')
end
end
end