libuv 0.11.3 → 0.11.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +14 -6
- data/.gitignore +17 -17
- data/.gitmodules +3 -3
- data/.rspec +1 -1
- data/.travis.yml +16 -16
- data/Gemfile +4 -4
- data/LICENSE +23 -23
- data/README.md +82 -82
- data/Rakefile +31 -31
- data/lib/libuv.rb +54 -54
- data/lib/libuv/async.rb +47 -47
- data/lib/libuv/check.rb +55 -55
- data/lib/libuv/dns.rb +85 -85
- data/lib/libuv/error.rb +73 -70
- data/lib/libuv/ext/ext.rb +258 -258
- data/lib/libuv/ext/platform/darwin_x64.rb +23 -23
- data/lib/libuv/ext/platform/linux.rb +7 -7
- data/lib/libuv/ext/platform/unix.rb +29 -29
- data/lib/libuv/ext/platform/windows.rb +40 -40
- data/lib/libuv/ext/tasks.rb +29 -29
- data/lib/libuv/ext/tasks/mac.rb +23 -23
- data/lib/libuv/ext/tasks/unix.rb +23 -23
- data/lib/libuv/ext/tasks/win.rb +11 -11
- data/lib/libuv/ext/types.rb +238 -238
- data/lib/libuv/file.rb +191 -191
- data/lib/libuv/filesystem.rb +232 -232
- data/lib/libuv/fs_event.rb +31 -31
- data/lib/libuv/handle.rb +85 -85
- data/lib/libuv/idle.rb +56 -56
- data/lib/libuv/loop.rb +387 -385
- data/lib/libuv/mixins/assertions.rb +23 -23
- data/lib/libuv/mixins/fs_checks.rb +55 -55
- data/lib/libuv/mixins/listener.rb +34 -34
- data/lib/libuv/mixins/net.rb +40 -40
- data/lib/libuv/mixins/resource.rb +27 -27
- data/lib/libuv/mixins/stream.rb +154 -154
- data/lib/libuv/pipe.rb +197 -197
- data/lib/libuv/prepare.rb +56 -56
- data/lib/libuv/signal.rb +51 -51
- data/lib/libuv/tcp.rb +317 -315
- data/lib/libuv/timer.rb +91 -91
- data/lib/libuv/tty.rb +37 -37
- data/lib/libuv/udp.rb +224 -224
- data/lib/libuv/version.rb +3 -3
- data/lib/libuv/work.rb +75 -75
- data/libuv.gemspec +56 -56
- data/spec/async_spec.rb +60 -60
- data/spec/cpu_spec.rb +10 -10
- data/spec/defer_spec.rb +980 -980
- data/spec/dns_spec.rb +90 -90
- data/spec/filesystem_spec.rb +124 -124
- data/spec/idle_spec.rb +56 -56
- data/spec/pipe_spec.rb +160 -160
- data/spec/tcp_spec.rb +267 -267
- metadata +24 -30
data/spec/pipe_spec.rb
CHANGED
@@ -1,160 +1,160 @@
|
|
1
|
-
require 'libuv'
|
2
|
-
|
3
|
-
|
4
|
-
describe Libuv::Pipe do
|
5
|
-
before :each do
|
6
|
-
@log = []
|
7
|
-
@general_failure = []
|
8
|
-
|
9
|
-
@loop = Libuv::Loop.new
|
10
|
-
@server = @loop.pipe
|
11
|
-
@client = @loop.pipe
|
12
|
-
@timeout = @loop.timer do
|
13
|
-
@loop.stop
|
14
|
-
@general_failure << "test timed out"
|
15
|
-
end
|
16
|
-
@timeout.start(5000)
|
17
|
-
|
18
|
-
@pipefile = "/tmp/test-pipes.pipe"
|
19
|
-
|
20
|
-
@loop.all(@server, @client, @timeout).catch do |reason|
|
21
|
-
@general_failure << reason.inspect
|
22
|
-
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
|
23
|
-
end
|
24
|
-
|
25
|
-
begin
|
26
|
-
File.unlink(@pipefile)
|
27
|
-
rescue
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
after :each do
|
32
|
-
begin
|
33
|
-
File.unlink(@pipefile)
|
34
|
-
rescue
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe 'bidirectional inter process communication' do
|
39
|
-
|
40
|
-
it "should send a ping and return a pong" do
|
41
|
-
@loop.run { |logger|
|
42
|
-
logger.progress do |level, errorid, error|
|
43
|
-
begin
|
44
|
-
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace}\n"
|
45
|
-
rescue Exception
|
46
|
-
@general_failure << 'error in logger'
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
@server.bind(@pipefile) do |connection|
|
51
|
-
connection.accept do |client|
|
52
|
-
client.progress do |data|
|
53
|
-
@log << data
|
54
|
-
client.write('pong')
|
55
|
-
end
|
56
|
-
client.start_read
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# catch server errors
|
61
|
-
@server.catch do |reason|
|
62
|
-
@general_failure << reason.inspect
|
63
|
-
@loop.stop
|
64
|
-
|
65
|
-
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
|
66
|
-
end
|
67
|
-
|
68
|
-
# start listening
|
69
|
-
@server.listen(1024)
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
# connect client to server
|
74
|
-
@client.connect(@pipefile) do |client|
|
75
|
-
@client.progress do |data|
|
76
|
-
@log << data
|
77
|
-
|
78
|
-
@client.close
|
79
|
-
end
|
80
|
-
|
81
|
-
@client.start_read
|
82
|
-
@client.write('ping')
|
83
|
-
end
|
84
|
-
|
85
|
-
@client.catch do |reason|
|
86
|
-
@general_failure << reason.inspect
|
87
|
-
@loop.stop
|
88
|
-
|
89
|
-
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
|
90
|
-
end
|
91
|
-
|
92
|
-
# Stop the loop once the client handle is closed
|
93
|
-
@client.finally do
|
94
|
-
@server.close
|
95
|
-
@loop.stop
|
96
|
-
end
|
97
|
-
}
|
98
|
-
|
99
|
-
@general_failure.should == []
|
100
|
-
@log.should == ['ping', 'pong']
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# This test won't pass on windows as pipes don't work like this on windows
|
105
|
-
describe 'unidirectional pipeline', :unix_only => true do
|
106
|
-
before :each do
|
107
|
-
system "/usr/bin/mkfifo", @pipefile
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should send work to a consumer" do
|
111
|
-
@loop.run { |logger|
|
112
|
-
logger.progress do |level, errorid, error|
|
113
|
-
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
114
|
-
end
|
115
|
-
|
116
|
-
|
117
|
-
heartbeat = @loop.timer
|
118
|
-
@file1 = @loop.file(@pipefile, File::RDWR|File::NONBLOCK)
|
119
|
-
@file1.progress do
|
120
|
-
@server.open(@file1.fileno) do |server|
|
121
|
-
heartbeat.progress do
|
122
|
-
@server.write('workload').catch do |err|
|
123
|
-
@general_failure << err
|
124
|
-
end
|
125
|
-
end
|
126
|
-
heartbeat.start(0, 200)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
@file1.catch do |e|
|
130
|
-
@general_failure << "Log called: #{e.inspect} - #{e.message}\n"
|
131
|
-
end
|
132
|
-
|
133
|
-
@file2 = @loop.file(@pipefile, File::RDWR|File::NONBLOCK)
|
134
|
-
@file2.progress do
|
135
|
-
# connect client to server
|
136
|
-
@client.open(@file2.fileno) do |consumer|
|
137
|
-
consumer.progress do |data|
|
138
|
-
@log = data
|
139
|
-
end
|
140
|
-
|
141
|
-
consumer.start_read
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
|
146
|
-
timeout = @loop.timer do
|
147
|
-
@server.close
|
148
|
-
@client.close
|
149
|
-
timeout.close
|
150
|
-
heartbeat.close
|
151
|
-
@loop.stop
|
152
|
-
end
|
153
|
-
timeout.start(1000)
|
154
|
-
}
|
155
|
-
|
156
|
-
@general_failure.should == []
|
157
|
-
@log.should == 'workload'
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
1
|
+
require 'libuv'
|
2
|
+
|
3
|
+
|
4
|
+
describe Libuv::Pipe do
|
5
|
+
before :each do
|
6
|
+
@log = []
|
7
|
+
@general_failure = []
|
8
|
+
|
9
|
+
@loop = Libuv::Loop.new
|
10
|
+
@server = @loop.pipe
|
11
|
+
@client = @loop.pipe
|
12
|
+
@timeout = @loop.timer do
|
13
|
+
@loop.stop
|
14
|
+
@general_failure << "test timed out"
|
15
|
+
end
|
16
|
+
@timeout.start(5000)
|
17
|
+
|
18
|
+
@pipefile = "/tmp/test-pipes.pipe"
|
19
|
+
|
20
|
+
@loop.all(@server, @client, @timeout).catch do |reason|
|
21
|
+
@general_failure << reason.inspect
|
22
|
+
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
File.unlink(@pipefile)
|
27
|
+
rescue
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
after :each do
|
32
|
+
begin
|
33
|
+
File.unlink(@pipefile)
|
34
|
+
rescue
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'bidirectional inter process communication' do
|
39
|
+
|
40
|
+
it "should send a ping and return a pong" do
|
41
|
+
@loop.run { |logger|
|
42
|
+
logger.progress do |level, errorid, error|
|
43
|
+
begin
|
44
|
+
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace}\n"
|
45
|
+
rescue Exception
|
46
|
+
@general_failure << 'error in logger'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
@server.bind(@pipefile) do |connection|
|
51
|
+
connection.accept do |client|
|
52
|
+
client.progress do |data|
|
53
|
+
@log << data
|
54
|
+
client.write('pong')
|
55
|
+
end
|
56
|
+
client.start_read
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# catch server errors
|
61
|
+
@server.catch do |reason|
|
62
|
+
@general_failure << reason.inspect
|
63
|
+
@loop.stop
|
64
|
+
|
65
|
+
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
|
66
|
+
end
|
67
|
+
|
68
|
+
# start listening
|
69
|
+
@server.listen(1024)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
# connect client to server
|
74
|
+
@client.connect(@pipefile) do |client|
|
75
|
+
@client.progress do |data|
|
76
|
+
@log << data
|
77
|
+
|
78
|
+
@client.close
|
79
|
+
end
|
80
|
+
|
81
|
+
@client.start_read
|
82
|
+
@client.write('ping')
|
83
|
+
end
|
84
|
+
|
85
|
+
@client.catch do |reason|
|
86
|
+
@general_failure << reason.inspect
|
87
|
+
@loop.stop
|
88
|
+
|
89
|
+
@general_failure << "Failed with: #{reason.message}\n#{reason.backtrace}\n"
|
90
|
+
end
|
91
|
+
|
92
|
+
# Stop the loop once the client handle is closed
|
93
|
+
@client.finally do
|
94
|
+
@server.close
|
95
|
+
@loop.stop
|
96
|
+
end
|
97
|
+
}
|
98
|
+
|
99
|
+
@general_failure.should == []
|
100
|
+
@log.should == ['ping', 'pong']
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# This test won't pass on windows as pipes don't work like this on windows
|
105
|
+
describe 'unidirectional pipeline', :unix_only => true do
|
106
|
+
before :each do
|
107
|
+
system "/usr/bin/mkfifo", @pipefile
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should send work to a consumer" do
|
111
|
+
@loop.run { |logger|
|
112
|
+
logger.progress do |level, errorid, error|
|
113
|
+
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
heartbeat = @loop.timer
|
118
|
+
@file1 = @loop.file(@pipefile, File::RDWR|File::NONBLOCK)
|
119
|
+
@file1.progress do
|
120
|
+
@server.open(@file1.fileno) do |server|
|
121
|
+
heartbeat.progress do
|
122
|
+
@server.write('workload').catch do |err|
|
123
|
+
@general_failure << err
|
124
|
+
end
|
125
|
+
end
|
126
|
+
heartbeat.start(0, 200)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
@file1.catch do |e|
|
130
|
+
@general_failure << "Log called: #{e.inspect} - #{e.message}\n"
|
131
|
+
end
|
132
|
+
|
133
|
+
@file2 = @loop.file(@pipefile, File::RDWR|File::NONBLOCK)
|
134
|
+
@file2.progress do
|
135
|
+
# connect client to server
|
136
|
+
@client.open(@file2.fileno) do |consumer|
|
137
|
+
consumer.progress do |data|
|
138
|
+
@log = data
|
139
|
+
end
|
140
|
+
|
141
|
+
consumer.start_read
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
timeout = @loop.timer do
|
147
|
+
@server.close
|
148
|
+
@client.close
|
149
|
+
timeout.close
|
150
|
+
heartbeat.close
|
151
|
+
@loop.stop
|
152
|
+
end
|
153
|
+
timeout.start(1000)
|
154
|
+
}
|
155
|
+
|
156
|
+
@general_failure.should == []
|
157
|
+
@log.should == 'workload'
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
data/spec/tcp_spec.rb
CHANGED
@@ -1,267 +1,267 @@
|
|
1
|
-
require 'libuv'
|
2
|
-
require 'thread'
|
3
|
-
|
4
|
-
|
5
|
-
describe Libuv::TCP do
|
6
|
-
before :each do
|
7
|
-
@log = []
|
8
|
-
@general_failure = []
|
9
|
-
|
10
|
-
@loop = Libuv::Loop.new
|
11
|
-
@server = @loop.tcp
|
12
|
-
@client = @loop.tcp
|
13
|
-
@timeout = @loop.timer do
|
14
|
-
@loop.stop
|
15
|
-
@loop2.stop if @loop2
|
16
|
-
@general_failure << "test timed out"
|
17
|
-
end
|
18
|
-
@timeout.start(5000)
|
19
|
-
|
20
|
-
@loop.all(@server, @client, @timeout).catch do |reason|
|
21
|
-
@general_failure << reason.inspect
|
22
|
-
end
|
23
|
-
|
24
|
-
|
25
|
-
@pipefile = "/tmp/test-pipe.pipe"
|
26
|
-
|
27
|
-
begin
|
28
|
-
File.unlink(@pipefile)
|
29
|
-
rescue
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe 'basic client server' do
|
34
|
-
it "should send a ping and return a pong", :network => true do
|
35
|
-
@loop.run { |logger|
|
36
|
-
logger.progress do |level, errorid, error|
|
37
|
-
begin
|
38
|
-
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
39
|
-
rescue Exception
|
40
|
-
@general_failure << 'error in logger'
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
@server.bind('127.0.0.1', 34567) do |server|
|
46
|
-
server.accept do |client|
|
47
|
-
client.progress do |data|
|
48
|
-
@log << data
|
49
|
-
|
50
|
-
client.write('pong')
|
51
|
-
end
|
52
|
-
client.start_read
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
# catch errors
|
57
|
-
@server.catch do |reason|
|
58
|
-
@general_failure << reason.inspect
|
59
|
-
end
|
60
|
-
|
61
|
-
# start listening
|
62
|
-
@server.listen(1024)
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
# connect client to server
|
67
|
-
@client.connect('127.0.0.1', 34567) do |client|
|
68
|
-
client.progress do |data|
|
69
|
-
@log << data
|
70
|
-
|
71
|
-
@client.shutdown
|
72
|
-
end
|
73
|
-
|
74
|
-
@client.start_read
|
75
|
-
@client.write('ping')
|
76
|
-
end
|
77
|
-
|
78
|
-
# catch errors
|
79
|
-
@client.catch do |reason|
|
80
|
-
@general_failure << reason.inspect
|
81
|
-
end
|
82
|
-
|
83
|
-
# close the handle
|
84
|
-
@client.finally do
|
85
|
-
@server.close
|
86
|
-
@loop.stop
|
87
|
-
end
|
88
|
-
}
|
89
|
-
|
90
|
-
@general_failure.should == []
|
91
|
-
@log.should == ['ping', 'pong']
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should handle requests on different threads", :network => true do
|
96
|
-
@sync = Mutex.new
|
97
|
-
|
98
|
-
@loop.run { |logger|
|
99
|
-
logger.progress do |level, errorid, error|
|
100
|
-
begin
|
101
|
-
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
102
|
-
rescue Exception
|
103
|
-
@general_failure << 'error in logger'
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
@remote = nil
|
109
|
-
@server.bind('127.0.0.1', 45678) do |server|
|
110
|
-
server.accept do |client|
|
111
|
-
@remote.write2(client)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
# catch errors
|
116
|
-
@server.catch do |reason|
|
117
|
-
@general_failure << reason.inspect
|
118
|
-
end
|
119
|
-
|
120
|
-
|
121
|
-
@pipeserve = @loop.pipe(true)
|
122
|
-
@pipeserve.bind(@pipefile) do |connection|
|
123
|
-
connection.accept do |client|
|
124
|
-
@remote = client
|
125
|
-
|
126
|
-
# start listening on TCP server
|
127
|
-
@server.listen(1024)
|
128
|
-
|
129
|
-
# connect client to server
|
130
|
-
@client.connect('127.0.0.1', 45678) do |client|
|
131
|
-
client.progress do |data|
|
132
|
-
@sync.synchronize {
|
133
|
-
@log << data
|
134
|
-
}
|
135
|
-
@client.shutdown
|
136
|
-
end
|
137
|
-
|
138
|
-
@client.start_read
|
139
|
-
@client.write('ping')
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
# start listening
|
145
|
-
@pipeserve.listen(1024)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
# catch errors
|
150
|
-
@client.catch do |reason|
|
151
|
-
@general_failure << reason.inspect
|
152
|
-
end
|
153
|
-
|
154
|
-
# close the handle
|
155
|
-
@client.finally do
|
156
|
-
@server.close
|
157
|
-
@pipeserve.close
|
158
|
-
end
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
Thread.new do
|
163
|
-
@loop2 = Libuv::Loop.new
|
164
|
-
@pipeclient = @loop2.pipe(true)
|
165
|
-
|
166
|
-
|
167
|
-
@loop2.run do |logger|
|
168
|
-
logger.progress do |level, errorid, error|
|
169
|
-
begin
|
170
|
-
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
171
|
-
rescue Exception
|
172
|
-
@general_failure << 'error in logger'
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
# connect client to server
|
177
|
-
@pipeclient.connect(@pipefile) do |client|
|
178
|
-
@pipeclient.progress do |data, connection|
|
179
|
-
connection.progress do |data|
|
180
|
-
@sync.synchronize {
|
181
|
-
@log << data
|
182
|
-
}
|
183
|
-
connection.write('pong')
|
184
|
-
end
|
185
|
-
connection.start_read
|
186
|
-
connection.finally do
|
187
|
-
@pipeclient.close
|
188
|
-
@loop2.stop
|
189
|
-
@loop.stop
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
@pipeclient.start_read2
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
197
|
-
}
|
198
|
-
|
199
|
-
@general_failure.should == []
|
200
|
-
@log.should == ['ping', 'pong']
|
201
|
-
end
|
202
|
-
|
203
|
-
describe 'basic TLS client and server' do
|
204
|
-
it "should send a ping and return a pong", :network => true do
|
205
|
-
@loop.run { |logger|
|
206
|
-
logger.progress do |level, errorid, error|
|
207
|
-
begin
|
208
|
-
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
209
|
-
rescue Exception
|
210
|
-
@general_failure << 'error in logger'
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
|
215
|
-
@server.bind('127.0.0.1', 34567) do |server|
|
216
|
-
server.accept do |client|
|
217
|
-
client.start_tls(server: true)
|
218
|
-
client.progress do |data|
|
219
|
-
@log << data
|
220
|
-
|
221
|
-
client.write('pong')
|
222
|
-
end
|
223
|
-
client.start_read
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
# catch errors
|
228
|
-
@server.catch do |reason|
|
229
|
-
@general_failure << reason.inspect
|
230
|
-
end
|
231
|
-
|
232
|
-
# start listening
|
233
|
-
@server.listen(1024)
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
# connect client to server
|
238
|
-
@client.connect('127.0.0.1', 34567) do |client|
|
239
|
-
client.start_tls
|
240
|
-
client.progress do |data|
|
241
|
-
@log << data
|
242
|
-
|
243
|
-
@client.shutdown
|
244
|
-
end
|
245
|
-
|
246
|
-
@client.start_read
|
247
|
-
@client.write('ping')
|
248
|
-
end
|
249
|
-
|
250
|
-
# catch errors
|
251
|
-
@client.catch do |reason|
|
252
|
-
@general_failure << reason.inspect
|
253
|
-
end
|
254
|
-
|
255
|
-
# close the handle
|
256
|
-
@client.finally do
|
257
|
-
@server.close
|
258
|
-
@loop.stop
|
259
|
-
end
|
260
|
-
}
|
261
|
-
|
262
|
-
@general_failure.should == []
|
263
|
-
@log.should == ['ping', 'pong']
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
end
|
1
|
+
require 'libuv'
|
2
|
+
require 'thread'
|
3
|
+
|
4
|
+
|
5
|
+
describe Libuv::TCP do
|
6
|
+
before :each do
|
7
|
+
@log = []
|
8
|
+
@general_failure = []
|
9
|
+
|
10
|
+
@loop = Libuv::Loop.new
|
11
|
+
@server = @loop.tcp
|
12
|
+
@client = @loop.tcp
|
13
|
+
@timeout = @loop.timer do
|
14
|
+
@loop.stop
|
15
|
+
@loop2.stop if @loop2
|
16
|
+
@general_failure << "test timed out"
|
17
|
+
end
|
18
|
+
@timeout.start(5000)
|
19
|
+
|
20
|
+
@loop.all(@server, @client, @timeout).catch do |reason|
|
21
|
+
@general_failure << reason.inspect
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
@pipefile = "/tmp/test-pipe.pipe"
|
26
|
+
|
27
|
+
begin
|
28
|
+
File.unlink(@pipefile)
|
29
|
+
rescue
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'basic client server' do
|
34
|
+
it "should send a ping and return a pong", :network => true do
|
35
|
+
@loop.run { |logger|
|
36
|
+
logger.progress do |level, errorid, error|
|
37
|
+
begin
|
38
|
+
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
39
|
+
rescue Exception
|
40
|
+
@general_failure << 'error in logger'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
@server.bind('127.0.0.1', 34567) do |server|
|
46
|
+
server.accept do |client|
|
47
|
+
client.progress do |data|
|
48
|
+
@log << data
|
49
|
+
|
50
|
+
client.write('pong')
|
51
|
+
end
|
52
|
+
client.start_read
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# catch errors
|
57
|
+
@server.catch do |reason|
|
58
|
+
@general_failure << reason.inspect
|
59
|
+
end
|
60
|
+
|
61
|
+
# start listening
|
62
|
+
@server.listen(1024)
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
# connect client to server
|
67
|
+
@client.connect('127.0.0.1', 34567) do |client|
|
68
|
+
client.progress do |data|
|
69
|
+
@log << data
|
70
|
+
|
71
|
+
@client.shutdown
|
72
|
+
end
|
73
|
+
|
74
|
+
@client.start_read
|
75
|
+
@client.write('ping')
|
76
|
+
end
|
77
|
+
|
78
|
+
# catch errors
|
79
|
+
@client.catch do |reason|
|
80
|
+
@general_failure << reason.inspect
|
81
|
+
end
|
82
|
+
|
83
|
+
# close the handle
|
84
|
+
@client.finally do
|
85
|
+
@server.close
|
86
|
+
@loop.stop
|
87
|
+
end
|
88
|
+
}
|
89
|
+
|
90
|
+
@general_failure.should == []
|
91
|
+
@log.should == ['ping', 'pong']
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should handle requests on different threads", :network => true do
|
96
|
+
@sync = Mutex.new
|
97
|
+
|
98
|
+
@loop.run { |logger|
|
99
|
+
logger.progress do |level, errorid, error|
|
100
|
+
begin
|
101
|
+
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
102
|
+
rescue Exception
|
103
|
+
@general_failure << 'error in logger'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
@remote = nil
|
109
|
+
@server.bind('127.0.0.1', 45678) do |server|
|
110
|
+
server.accept do |client|
|
111
|
+
@remote.write2(client)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# catch errors
|
116
|
+
@server.catch do |reason|
|
117
|
+
@general_failure << reason.inspect
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
@pipeserve = @loop.pipe(true)
|
122
|
+
@pipeserve.bind(@pipefile) do |connection|
|
123
|
+
connection.accept do |client|
|
124
|
+
@remote = client
|
125
|
+
|
126
|
+
# start listening on TCP server
|
127
|
+
@server.listen(1024)
|
128
|
+
|
129
|
+
# connect client to server
|
130
|
+
@client.connect('127.0.0.1', 45678) do |client|
|
131
|
+
client.progress do |data|
|
132
|
+
@sync.synchronize {
|
133
|
+
@log << data
|
134
|
+
}
|
135
|
+
@client.shutdown
|
136
|
+
end
|
137
|
+
|
138
|
+
@client.start_read
|
139
|
+
@client.write('ping')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# start listening
|
145
|
+
@pipeserve.listen(1024)
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
# catch errors
|
150
|
+
@client.catch do |reason|
|
151
|
+
@general_failure << reason.inspect
|
152
|
+
end
|
153
|
+
|
154
|
+
# close the handle
|
155
|
+
@client.finally do
|
156
|
+
@server.close
|
157
|
+
@pipeserve.close
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
Thread.new do
|
163
|
+
@loop2 = Libuv::Loop.new
|
164
|
+
@pipeclient = @loop2.pipe(true)
|
165
|
+
|
166
|
+
|
167
|
+
@loop2.run do |logger|
|
168
|
+
logger.progress do |level, errorid, error|
|
169
|
+
begin
|
170
|
+
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
171
|
+
rescue Exception
|
172
|
+
@general_failure << 'error in logger'
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
# connect client to server
|
177
|
+
@pipeclient.connect(@pipefile) do |client|
|
178
|
+
@pipeclient.progress do |data, connection|
|
179
|
+
connection.progress do |data|
|
180
|
+
@sync.synchronize {
|
181
|
+
@log << data
|
182
|
+
}
|
183
|
+
connection.write('pong')
|
184
|
+
end
|
185
|
+
connection.start_read
|
186
|
+
connection.finally do
|
187
|
+
@pipeclient.close
|
188
|
+
@loop2.stop
|
189
|
+
@loop.stop
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
@pipeclient.start_read2
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
}
|
198
|
+
|
199
|
+
@general_failure.should == []
|
200
|
+
@log.should == ['ping', 'pong']
|
201
|
+
end
|
202
|
+
|
203
|
+
describe 'basic TLS client and server' do
|
204
|
+
it "should send a ping and return a pong", :network => true do
|
205
|
+
@loop.run { |logger|
|
206
|
+
logger.progress do |level, errorid, error|
|
207
|
+
begin
|
208
|
+
@general_failure << "Log called: #{level}: #{errorid}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
|
209
|
+
rescue Exception
|
210
|
+
@general_failure << 'error in logger'
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
@server.bind('127.0.0.1', 34567) do |server|
|
216
|
+
server.accept do |client|
|
217
|
+
client.start_tls(server: true)
|
218
|
+
client.progress do |data|
|
219
|
+
@log << data
|
220
|
+
|
221
|
+
client.write('pong')
|
222
|
+
end
|
223
|
+
client.start_read
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
# catch errors
|
228
|
+
@server.catch do |reason|
|
229
|
+
@general_failure << reason.inspect
|
230
|
+
end
|
231
|
+
|
232
|
+
# start listening
|
233
|
+
@server.listen(1024)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
# connect client to server
|
238
|
+
@client.connect('127.0.0.1', 34567) do |client|
|
239
|
+
client.start_tls
|
240
|
+
client.progress do |data|
|
241
|
+
@log << data
|
242
|
+
|
243
|
+
@client.shutdown
|
244
|
+
end
|
245
|
+
|
246
|
+
@client.start_read
|
247
|
+
@client.write('ping')
|
248
|
+
end
|
249
|
+
|
250
|
+
# catch errors
|
251
|
+
@client.catch do |reason|
|
252
|
+
@general_failure << reason.inspect
|
253
|
+
end
|
254
|
+
|
255
|
+
# close the handle
|
256
|
+
@client.finally do
|
257
|
+
@server.close
|
258
|
+
@loop.stop
|
259
|
+
end
|
260
|
+
}
|
261
|
+
|
262
|
+
@general_failure.should == []
|
263
|
+
@log.should == ['ping', 'pong']
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|