forked from zendesk/ruby-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_with_timeout_spec.rb
64 lines (46 loc) · 1.48 KB
/
socket_with_timeout_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
# frozen_string_literal: true
describe Kafka::SocketWithTimeout, ".open" do
it "times out if the server doesn't accept the connection within the timeout" do
host = "172.16.0.0" # this address is non-routable!
port = 4444
timeout = 0.1
allowed_time = timeout + 0.1
start = Time.now
expect {
Kafka::SocketWithTimeout.new(host, port, connect_timeout: timeout, timeout: 1)
}.to raise_exception(Errno::ETIMEDOUT)
finish = Time.now
expect(finish - start).to be < allowed_time
end
describe "#read" do
it "times out after the specified amount of time" do
host = "localhost"
server = TCPServer.new(host, 0)
port = server.addr[1]
timeout = 0.1
allowed_time = timeout + 0.1
socket = Kafka::SocketWithTimeout.new(host, port, connect_timeout: 1, timeout: timeout)
start = Time.now
expect {
socket.read(4)
}.to raise_exception(Errno::ETIMEDOUT)
finish = Time.now
expect(finish - start).to be < allowed_time
end
it "finishes immediately when #close is called" do
host = "localhost"
server = TCPServer.new(host, 0)
port = server.addr[1]
socket = Kafka::SocketWithTimeout.new(host, port, connect_timeout: 1, timeout: 5)
thread = Thread.new do
begin
socket.read(4)
rescue Errno::ETIMEDOUT
:timeout
end
end
socket.close
expect { thread.value }.to raise_error(IOError)
end
end
end