standard-procedure-plumbing 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,167 +0,0 @@
1
- require "spec_helper"
2
- require "async"
3
- require_relative "../../lib/plumbing/valve/async"
4
-
5
- RSpec.describe Plumbing::Valve do
6
- # standard:disable Lint/ConstantDefinitionInBlock
7
- class Counter
8
- include Plumbing::Valve
9
- query :name, :count, :am_i_failing?, :slow_query
10
- command "slowly_increment", "raises_error"
11
- attr_reader :name, :count
12
-
13
- def initialize name, initial_value: 0
14
- @name = name
15
- @count = initial_value
16
- end
17
-
18
- def slowly_increment
19
- sleep 0.5
20
- @count += 1
21
- end
22
-
23
- def slow_query
24
- sleep 0.5
25
- @count
26
- end
27
-
28
- def am_i_failing? = raise "I'm a failure"
29
-
30
- def raises_error = raise "I'm an error"
31
- end
32
-
33
- class StepCounter < Counter
34
- query :step_value
35
- attr_reader :step_value
36
-
37
- def initialize name, initial_value: 0, step_value: 5
38
- super(name, initial_value: initial_value)
39
- @step_value = step_value
40
- end
41
-
42
- def slowly_increment
43
- sleep 0.5
44
- @count += @step_value
45
- end
46
-
47
- def failing_query
48
- raise "I'm a failure"
49
- end
50
- end
51
- # standard:enable Lint/ConstantDefinitionInBlock
52
-
53
- it "knows which queries are defined" do
54
- expect(Counter.queries).to eq [:name, :count, :am_i_failing?, :slow_query]
55
- end
56
-
57
- it "knows which commands are defined" do
58
- expect(Counter.commands).to eq [:slowly_increment, :raises_error]
59
- end
60
-
61
- it "raises exceptions from queries" do
62
- @counter = Counter.start "failure"
63
-
64
- expect { @counter.am_i_failing? }.to raise_error "I'm a failure"
65
- end
66
-
67
- it "does not raise exceptions from commands" do
68
- @counter = Counter.start "failure"
69
-
70
- expect { @counter.raises_error }.not_to raise_error
71
- end
72
-
73
- it "raises exceptions from queries" do
74
- @counter = Counter.start "failure"
75
-
76
- expect { @counter.am_i_failing? }.to raise_error "I'm a failure"
77
- end
78
-
79
- it "reuses existing proxy classes" do
80
- @counter = Counter.start "inline counter", initial_value: 100
81
- @proxy_class = @counter.class
82
-
83
- @counter = Counter.start "another inline counter", initial_value: 200
84
- expect(@counter.class).to eq @proxy_class
85
- end
86
-
87
- it "includes commands and queries from the superclass" do
88
- expect(StepCounter.queries).to eq [:name, :count, :am_i_failing?, :slow_query, :step_value]
89
- expect(StepCounter.commands).to eq [:slowly_increment, :raises_error]
90
-
91
- @step_counter = StepCounter.start "step counter", initial_value: 100, step_value: 10
92
-
93
- expect(@step_counter.count).to eq 100
94
- expect(@step_counter.step_value).to eq 10
95
- @step_counter.slowly_increment
96
- expect(@step_counter.count).to eq 110
97
- end
98
-
99
- context "inline" do
100
- around :example do |example|
101
- Plumbing.configure mode: :inline, &example
102
- end
103
-
104
- it "sends all queries immediately" do
105
- @counter = Counter.start "inline counter", initial_value: 100
106
- @time = Time.now
107
-
108
- expect(@counter.name).to eq "inline counter"
109
- expect(@counter.count).to eq 100
110
- expect(Time.now - @time).to be < 0.1
111
-
112
- expect(@counter.slow_query).to eq 100
113
- expect(Time.now - @time).to be > 0.4
114
- end
115
-
116
- it "sends all commands immediately" do
117
- @counter = Counter.start "inline counter", initial_value: 100
118
- @time = Time.now
119
-
120
- @counter.slowly_increment
121
-
122
- expect(@counter.count).to eq 101
123
- expect(Time.now - @time).to be > 0.4
124
- end
125
- end
126
-
127
- context "async" do
128
- around :example do |example|
129
- Sync do
130
- Plumbing.configure mode: :async, &example
131
- end
132
- end
133
-
134
- it "sends all queries using fibers and waits for the response" do
135
- @counter = Counter.start "async counter", initial_value: 100
136
- @time = Time.now
137
-
138
- expect(@counter.name).to eq "async counter"
139
- expect(@counter.count).to eq 100
140
- expect(Time.now - @time).to be < 0.1
141
-
142
- expect(@counter.slow_query).to eq 100
143
- expect(Time.now - @time).to be > 0.4
144
- end
145
-
146
- it "ignores the response from a query and returns immediately" do
147
- @counter = Counter.start "async counter", initial_value: 100
148
- @time = Time.now
149
-
150
- expect(@counter.slow_query(ignore_result: true)).to be_nil
151
-
152
- expect(Time.now - @time).to be < 0.1
153
- end
154
-
155
- it "sends all commands using fibers without waiting for the response" do
156
- @counter = Counter.start "async counter", initial_value: 100
157
- @time = Time.now
158
-
159
- @counter.slowly_increment
160
- expect(Time.now - @time).to be < 0.1
161
-
162
- # wait for the async task to complete
163
- expect(@counter.count).to become_equal_to { 101 }
164
- expect(Time.now - @time).to be > 0.4
165
- end
166
- end
167
- end