forked from datanoise/amqp.cr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimed_channel_spec.cr
65 lines (55 loc) · 1.67 KB
/
timed_channel_spec.cr
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
require "spec"
require "../src/amqp/timed_channel"
describe "Timed::Channel" do
describe "TimerChannel" do
it "should periodically generate time values" do
timer = Timed::TimerChannel.new(10.milliseconds)
times = (1..2).map{ timer.receive }
times.length.should eq(2)
(times.last - times.first).should be >= 10.milliseconds
end
it "should raise exception on send" do
timer = Timed::TimerChannel.new(10.milliseconds)
expect_raises(Timed::ChannelError) do
timer.send Time.now
end
end
it "should time out Channel.select" do
timer = Timed::TimerChannel.new(5.milliseconds)
ch = Timed::Channel(Bool).new
spawn { sleep(0.007); ch.send(true) }
case Timed::Channel.select(ch, timer)
when timer
when ch
fail "channel should not be ready"
end
end
it "should not time out Channel.select if there is a send issued" do
timer = Timed::TimerChannel.new(7.milliseconds)
ch = Timed::Channel(Bool).new
spawn { sleep(0.005); ch.send(true) }
case Timed::Channel.select(ch, timer)
when timer
fail "timeout is received"
when ch
ch.receive.should be_true
end
end
end
it "should be able to close a channel while receiving" do
ch = Timed::Channel(Bool).new
spawn { sleep(0.005); ch.close }
expect_raises(Timed::ChannelClosed) do
ch.receive
end
end
it "should be able to close a channel while sending" do
ch = Timed::Channel(Bool).new
spawn { sleep(0.005); ch.close }
# send to fill the channel
ch.send(true)
expect_raises(Timed::ChannelClosed) do
ch.send(true)
end
end
end