/
database_spec.rb
211 lines (170 loc) · 6.22 KB
/
database_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
require File.dirname(__FILE__) + '/../spec_helper'
module Crunch
describe Database do
describe ".connect method" do
before(:each) do
@this = Database.connect 'crunch_test'
end
it "is pointed at if you try to run .new" do
->{Database.new 'test'}.should raise_error(DatabaseError, /connect/)
end
it "returns an instance with .connect" do
@this.should be_a(Database)
end
it "requires a name" do
->{Database.connect}.should raise_error(ArgumentError)
end
it "knows its name" do
@this.name.should == 'crunch_test'
end
it "has a default host" do
@this.host.should == 'localhost'
end
it "has a default port" do
@this.port.should == 27017
end
it "has a default minimum connection count" do
@this.min_connections.should == 1
end
it "has a default maximum connection count" do
@this.max_connections.should == 10
end
it "has a default heartbeat" do
@this.heartbeat.should == 1.0
end
it "returns the same instance given the same name, host, and port" do
that = Database.connect 'crunch_test'
that.object_id.should == @this.object_id
end
it "returns a different instance if a different name is given" do
that = Database.connect 'crunchy_test'
that.object_id.should_not == @this.object_id
end
it "returns a different instance if a different host is given" do
that = Database.connect 'crunch_test', host: 'example.org'
that.object_id.should_not == @this.object_id
end
it "returns a different instance if a different port is given" do
that = Database.connect 'crunch_test', port: 1111
that.object_id.should_not == @this.object_id
end
it "can reset the minimum connection count at new connection invocations" do
that = Database.connect 'crunch_test', min_connections: 3
@this.min_connections.should == 3
end
it "can reset the minimum connection count at runtime" do
that = Database.connect 'crunch_test'
that.min_connections = 2
@this.min_connections.should == 2
end
it "can reset the maximum connection count at new connection invocations" do
that = Database.connect 'crunch_test', max_connections: 5
@this.max_connections.should == 5
end
it "can reset the maximum connection count at runtime" do
that = Database.connect 'crunch_test'
that.max_connections = 7
@this.max_connections.should == 7
end
it "can reset the heartbeat at new connection invocations" do
that = Database.connect 'crunch_test', heartbeat: 2.3
@this.heartbeat.should == 2.3
end
it "can reset the heartbeat count at runtime" do
that = Database.connect 'crunch_test'
that.heartbeat = 0.1
@this.heartbeat.should == 0.1
end
it "starts the event loop" do
EventMachine.should be_reactor_running
end
end
describe "request queue" do
before(:each) do
@this = Database.connect 'crunch_test'
end
it "starts with no requests" do
@this.pending_count.should == 0
end
it "can accept requests" do
tick {@this << DummyRequest.new}
@this.pending_count.should == 1
end
it "fails on a non-Request" do
->{@this << true}.should raise_error(DatabaseError, /Request/)
end
it "sets the begin time of the request" do
r = DummyRequest.new
r.began.should be_nil
tick {@this << r}
r.began.should be_within(1).of(Time.now)
end
it "can be chained" do
tick {@this << DummyRequest.new << DummyRequest.new}
@this.pending_count.should == 2
end
end
describe "connection pool" do
before(:each) do
@beat_count = 0
@this = Database.connect 'crunch_test', min_connections: 2, heartbeat: 0.01, on_heartbeat: ->{@beat_count += 1}
EM.set_quantum 10 # So we don't wait forever on our heartbeat calls
end
it "loads the minimum at startup" do
@this.connection_count.should == 2
end
it "stays at min_connections if the number of requests is < connection_count" do
tick {@this << DummyRequest.new}
sleep 0.05
@this.connection_count.should == 2
end
it "stays at min_connections if the number of requests == connection_count" do
tick {@this << DummyRequest.new << DummyRequest.new}
sleep 0.05
@this.connection_count.should == 2
end
it "adds more connections if the number of requests > connection_count" do
tick {3.times {@this << DummyRequest.new}}
sleep 0.05
@this.connection_count.should == 3
end
it "adds new connections gradually" do
tick {8.times {@this << DummyRequest.new}}
sleep 0.02
@this.connection_count.should be_within(2).of(3)
sleep 0.1
@this.connection_count.should == 8
end
it "removes connections slowly" do
tick {5.times {@this << DummyRequest.new}}
sleep 0.05
@this.connection_count.should == 5
5.times {@this.requests.pop {true}}
sleep 0.1
@this.connection_count.should == 5
sleep 0.2
@this.connection_count.should == 4
sleep 0.5
@this.connection_count.should == 2
end
it "can add other events to the heartbeat" do
foo = false
@this.on_heartbeat = ->{foo = true}
sleep 0.02
foo.should == true
end
it "calls the heartbeat timer" do
sleep 0.03
@beat_count.should be_within(1.01).of(2)
end
end
after(:each) do
# We have to clear the global Databases hash _and_ stop EventMachine to reset to initial state.
Database.class_variable_set(:@@databases, {})
if EM.reactor_running?
EM.run {EM.stop_event_loop}
Thread.pass while EM.reactor_running?
end
end
end
end