-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher_spec.cr
More file actions
76 lines (63 loc) · 2.43 KB
/
Copy pathpublisher_spec.cr
File metadata and controls
76 lines (63 loc) · 2.43 KB
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
require "../spec_helper"
module PlaceOS::Source
# Records the order work was done in, so the queue's guarantees can be
# asserted without a broker in the way
private class RecordingPublisher < Publisher
getter handled : Array(String) = [] of String
def publish(message : Publisher::Message)
handled << "publish:#{message.payload}"
end
def delete(message : Publisher::Message)
handled << "delete:#{message.payload}"
end
end
describe Publisher do
# A rename removes the old topic and republishes under the new one. If a
# publish were to win that race the delete would take the new value straight
# back out, so deletions have to be drained first
it "drains queued deletions before queued messages" do
publisher = RecordingPublisher.new
event = Mappings::Metadata.new("mod-1234", "hello")
# queued messages first, deletions second — order of arrival must not
# decide order of work
3.times { |i| publisher.message_queue.send(Publisher::Message.new(event, "message-#{i}", Time.utc)) }
2.times { |i| publisher.queue_delete(Publisher::Message.new(event, "deletion-#{i}", Time.utc)) }
publisher.start
sleep 200.milliseconds
publisher.handled.first(2).should eq ["delete:deletion-0", "delete:deletion-1"]
publisher.handled.size.should eq 5
publisher.deleted.should eq 2
publisher.processed.should eq 3
publisher.stop
end
it "reports whether deletions are still queued" do
publisher = RecordingPublisher.new
event = Mappings::Metadata.new("mod-1234", "hello")
publisher.deletes_pending?.should be_false
publisher.queue_delete(Publisher::Message.new(event, "pending", Time.utc))
publisher.deletes_pending?.should be_true
publisher.start
sleep 200.milliseconds
publisher.deletes_pending?.should be_false
publisher.stop
end
# a consumer must notice a closed queue rather than spinning on it
it "stops cleanly once the queues are closed" do
publisher = RecordingPublisher.new
publisher.start
sleep 50.milliseconds
done = Channel(Nil).new(1)
spawn do
publisher.stop
done.send(nil)
end
select
when done.receive
publisher.message_queue.closed?.should be_true
publisher.delete_queue.closed?.should be_true
when timeout(5.seconds)
fail "stop did not return"
end
end
end
end