-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_spec.rb
148 lines (122 loc) · 4.35 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
require "spec_helper"
require "async"
require "plumbing/actor/async"
require "plumbing/actor/threaded"
RSpec.describe "Pipe examples" do
Plumbing::Spec.modes do
context "In #{Plumbing.config.mode} mode" do
it "observes events" do
@source = Plumbing::Pipe.start
@result = []
@source.add_observer do |event_name, data|
@result << event_name
end
@source.notify "something_happened", message: "But what was it?"
expect { @result }.to become ["something_happened"]
end
it "filters events" do
@source = Plumbing::Pipe.start
@filter = Plumbing::Pipe::Filter.start source: @source do |event_name, data|
%w[important urgent].include? event_name
end
@result = []
@filter.add_observer do |event_name, data|
@result << event_name
end
@source.notify "important", message: "ALERT! ALERT!"
expect { @result }.to become ["important"]
@source.notify "unimportant", message: "Nothing to see here"
expect { @result }.to become ["important"]
end
it "allows for custom filters" do
# standard:disable Lint/ConstantDefinitionInBlock
class EveryThirdEvent < Plumbing::Pipe::CustomFilter
def initialize source:
super
@events = []
end
def received event_name, data
safely do
@events << event_name
if @events.count >= 3
@events.clear
notify event_name, **data
end
end
end
end
# standard:enable Lint/ConstantDefinitionInBlock
@source = Plumbing::Pipe.start
@filter = EveryThirdEvent.start(source: @source)
@result = []
@filter.add_observer do |event_name, data|
@result << event_name
end
1.upto 10 do |i|
@source.notify i.to_s
end
expect { @result }.to become ["3", "6", "9"]
end
it "joins multiple source pipes" do
@first_source = Plumbing::Pipe.start
@second_source = Plumbing::Pipe.start
@junction = Plumbing::Pipe::Junction.start @first_source, @second_source
@result = []
@junction.add_observer do |event_name, data|
@result << event_name
end
@first_source.notify "one"
expect { @result }.to become ["one"]
@second_source.notify "two"
expect { @result }.to become ["one", "two"]
end
it "dispatches events asynchronously using async" do
Plumbing.configure mode: :async do
Sync do
@first_source = Plumbing::Pipe.start
@second_source = Plumbing::Pipe.start
@junction = Plumbing::Pipe::Junction.start @first_source, @second_source
@filter = Plumbing::Pipe::Filter.start source: @junction do |event_name, data|
%w[one-one two-two].include? event_name
end
@result = []
@filter.add_observer do |event_name, data|
@result << event_name
end
@first_source.notify "one-one"
@first_source.notify "one-two"
@second_source.notify "two-one"
@second_source.notify "two-two"
expect { @result.sort }.to become(["one-one", "two-two"])
end
end
end
it "dispatches events asynchronously using threads" do
Plumbing.configure mode: :threaded do
@result = []
@first_source = Plumbing::Pipe.start
@second_source = Plumbing::Pipe.start
@junction = Plumbing::Pipe::Junction.start @first_source, @second_source
@filter = Plumbing::Pipe::Filter.start source: @junction do |event_name, data|
%w[one-one two-two].include? event_name
end
await do
@filter.add_observer do |event_name, data|
@result << event_name
end
end
@first_source.notify "one-one"
@first_source.notify "one-two"
@second_source.notify "two-one"
@second_source.notify "two-two"
expect { @result.sort }.to become(["one-one", "two-two"])
ensure
@first_source.shutdown
@second_source.shutdown
@junction.shutdown
@filter.shutdown
end
end
end
end
end