colincasey-sequel 2.10.0 → 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. data/CHANGELOG +7 -1
  2. data/doc/advanced_associations.rdoc +614 -0
  3. data/doc/cheat_sheet.rdoc +223 -0
  4. data/doc/dataset_filtering.rdoc +158 -0
  5. data/doc/prepared_statements.rdoc +104 -0
  6. data/doc/release_notes/1.0.txt +38 -0
  7. data/doc/release_notes/1.1.txt +143 -0
  8. data/doc/release_notes/1.3.txt +101 -0
  9. data/doc/release_notes/1.4.0.txt +53 -0
  10. data/doc/release_notes/1.5.0.txt +155 -0
  11. data/doc/release_notes/2.0.0.txt +298 -0
  12. data/doc/release_notes/2.1.0.txt +271 -0
  13. data/doc/release_notes/2.10.0.txt +328 -0
  14. data/doc/release_notes/2.2.0.txt +253 -0
  15. data/doc/release_notes/2.3.0.txt +88 -0
  16. data/doc/release_notes/2.4.0.txt +106 -0
  17. data/doc/release_notes/2.5.0.txt +137 -0
  18. data/doc/release_notes/2.6.0.txt +157 -0
  19. data/doc/release_notes/2.7.0.txt +166 -0
  20. data/doc/release_notes/2.8.0.txt +171 -0
  21. data/doc/release_notes/2.9.0.txt +97 -0
  22. data/doc/schema.rdoc +29 -0
  23. data/doc/sharding.rdoc +113 -0
  24. data/lib/sequel.rb +1 -0
  25. data/lib/sequel_core/adapters/ado.rb +89 -0
  26. data/lib/sequel_core/adapters/db2.rb +143 -0
  27. data/lib/sequel_core/adapters/dbi.rb +112 -0
  28. data/lib/sequel_core/adapters/do/mysql.rb +38 -0
  29. data/lib/sequel_core/adapters/do/postgres.rb +92 -0
  30. data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
  31. data/lib/sequel_core/adapters/do.rb +205 -0
  32. data/lib/sequel_core/adapters/firebird.rb +298 -0
  33. data/lib/sequel_core/adapters/informix.rb +85 -0
  34. data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
  35. data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
  36. data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
  37. data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
  38. data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
  39. data/lib/sequel_core/adapters/jdbc.rb +491 -0
  40. data/lib/sequel_core/adapters/mysql.rb +369 -0
  41. data/lib/sequel_core/adapters/odbc.rb +174 -0
  42. data/lib/sequel_core/adapters/openbase.rb +68 -0
  43. data/lib/sequel_core/adapters/oracle.rb +107 -0
  44. data/lib/sequel_core/adapters/postgres.rb +456 -0
  45. data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
  46. data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
  47. data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
  48. data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
  49. data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
  50. data/lib/sequel_core/adapters/shared/progress.rb +31 -0
  51. data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
  52. data/lib/sequel_core/adapters/sqlite.rb +248 -0
  53. data/lib/sequel_core/connection_pool.rb +258 -0
  54. data/lib/sequel_core/core_ext.rb +217 -0
  55. data/lib/sequel_core/core_sql.rb +202 -0
  56. data/lib/sequel_core/database/schema.rb +164 -0
  57. data/lib/sequel_core/database.rb +691 -0
  58. data/lib/sequel_core/dataset/callback.rb +13 -0
  59. data/lib/sequel_core/dataset/convenience.rb +237 -0
  60. data/lib/sequel_core/dataset/pagination.rb +96 -0
  61. data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
  62. data/lib/sequel_core/dataset/query.rb +41 -0
  63. data/lib/sequel_core/dataset/schema.rb +15 -0
  64. data/lib/sequel_core/dataset/sql.rb +1010 -0
  65. data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
  66. data/lib/sequel_core/dataset/unsupported.rb +43 -0
  67. data/lib/sequel_core/dataset.rb +511 -0
  68. data/lib/sequel_core/deprecated.rb +26 -0
  69. data/lib/sequel_core/exceptions.rb +44 -0
  70. data/lib/sequel_core/migration.rb +212 -0
  71. data/lib/sequel_core/object_graph.rb +230 -0
  72. data/lib/sequel_core/pretty_table.rb +71 -0
  73. data/lib/sequel_core/schema/generator.rb +320 -0
  74. data/lib/sequel_core/schema/sql.rb +325 -0
  75. data/lib/sequel_core/schema.rb +2 -0
  76. data/lib/sequel_core/sql.rb +887 -0
  77. data/lib/sequel_core/version.rb +11 -0
  78. data/lib/sequel_core.rb +172 -0
  79. data/lib/sequel_model/association_reflection.rb +267 -0
  80. data/lib/sequel_model/associations.rb +499 -0
  81. data/lib/sequel_model/base.rb +523 -0
  82. data/lib/sequel_model/caching.rb +82 -0
  83. data/lib/sequel_model/dataset_methods.rb +26 -0
  84. data/lib/sequel_model/eager_loading.rb +370 -0
  85. data/lib/sequel_model/exceptions.rb +7 -0
  86. data/lib/sequel_model/hooks.rb +101 -0
  87. data/lib/sequel_model/inflector.rb +281 -0
  88. data/lib/sequel_model/plugins.rb +62 -0
  89. data/lib/sequel_model/record.rb +568 -0
  90. data/lib/sequel_model/schema.rb +49 -0
  91. data/lib/sequel_model/validations.rb +429 -0
  92. data/lib/sequel_model.rb +91 -0
  93. data/spec/adapters/ado_spec.rb +46 -0
  94. data/spec/adapters/firebird_spec.rb +376 -0
  95. data/spec/adapters/informix_spec.rb +96 -0
  96. data/spec/adapters/mysql_spec.rb +881 -0
  97. data/spec/adapters/oracle_spec.rb +244 -0
  98. data/spec/adapters/postgres_spec.rb +687 -0
  99. data/spec/adapters/spec_helper.rb +10 -0
  100. data/spec/adapters/sqlite_spec.rb +555 -0
  101. data/spec/integration/dataset_test.rb +134 -0
  102. data/spec/integration/eager_loader_test.rb +696 -0
  103. data/spec/integration/prepared_statement_test.rb +130 -0
  104. data/spec/integration/schema_test.rb +180 -0
  105. data/spec/integration/spec_helper.rb +58 -0
  106. data/spec/integration/type_test.rb +96 -0
  107. data/spec/rcov.opts +6 -0
  108. data/spec/sequel_core/connection_pool_spec.rb +526 -0
  109. data/spec/sequel_core/core_ext_spec.rb +156 -0
  110. data/spec/sequel_core/core_sql_spec.rb +522 -0
  111. data/spec/sequel_core/database_spec.rb +1188 -0
  112. data/spec/sequel_core/dataset_spec.rb +3481 -0
  113. data/spec/sequel_core/expression_filters_spec.rb +363 -0
  114. data/spec/sequel_core/migration_spec.rb +261 -0
  115. data/spec/sequel_core/object_graph_spec.rb +272 -0
  116. data/spec/sequel_core/pretty_table_spec.rb +58 -0
  117. data/spec/sequel_core/schema_generator_spec.rb +167 -0
  118. data/spec/sequel_core/schema_spec.rb +780 -0
  119. data/spec/sequel_core/spec_helper.rb +55 -0
  120. data/spec/sequel_core/version_spec.rb +7 -0
  121. data/spec/sequel_model/association_reflection_spec.rb +93 -0
  122. data/spec/sequel_model/associations_spec.rb +1767 -0
  123. data/spec/sequel_model/base_spec.rb +419 -0
  124. data/spec/sequel_model/caching_spec.rb +215 -0
  125. data/spec/sequel_model/dataset_methods_spec.rb +78 -0
  126. data/spec/sequel_model/eager_loading_spec.rb +1165 -0
  127. data/spec/sequel_model/hooks_spec.rb +485 -0
  128. data/spec/sequel_model/inflector_spec.rb +119 -0
  129. data/spec/sequel_model/model_spec.rb +588 -0
  130. data/spec/sequel_model/plugins_spec.rb +80 -0
  131. data/spec/sequel_model/record_spec.rb +1184 -0
  132. data/spec/sequel_model/schema_spec.rb +90 -0
  133. data/spec/sequel_model/spec_helper.rb +78 -0
  134. data/spec/sequel_model/validations_spec.rb +1067 -0
  135. data/spec/spec.opts +0 -0
  136. data/spec/spec_config.rb.example +10 -0
  137. metadata +177 -3
@@ -0,0 +1,526 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+ CONNECTION_POOL_DEFAULTS = {:pool_timeout=>5, :pool_sleep_time=>0.001,
3
+ :pool_reuse_connections=>:allow, :pool_convert_exceptions=>true, :max_connections=>4}
4
+
5
+ context "An empty ConnectionPool" do
6
+ setup do
7
+ @cpool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS)
8
+ end
9
+
10
+ specify "should have no available connections" do
11
+ @cpool.available_connections.should == []
12
+ end
13
+
14
+ specify "should have no allocated connections" do
15
+ @cpool.allocated.should == {}
16
+ end
17
+
18
+ specify "should have a created_count of zero" do
19
+ @cpool.created_count.should == 0
20
+ end
21
+ end
22
+
23
+ context "A connection pool handling connections" do
24
+ setup do
25
+ @max_size = 2
26
+ @cpool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS.merge(:disconnection_proc=>proc{|c| @max_size=3}, :max_connections=>@max_size)) {:got_connection}
27
+ end
28
+
29
+ specify "#hold should increment #created_count" do
30
+ @cpool.hold do
31
+ @cpool.created_count.should == 1
32
+ @cpool.hold {@cpool.hold {@cpool.created_count.should == 1}}
33
+ Thread.new{@cpool.hold {@cpool.created_count.should == 2}}.join
34
+ end
35
+ end
36
+
37
+ specify "#hold should add the connection to the #allocated array" do
38
+ @cpool.hold do
39
+ @cpool.allocated.size.should == 1
40
+
41
+ @cpool.allocated.should == {Thread.current=>:got_connection}
42
+ end
43
+ end
44
+
45
+ specify "#hold should yield a new connection" do
46
+ @cpool.hold {|conn| conn.should == :got_connection}
47
+ end
48
+
49
+ specify "a connection should be de-allocated after it has been used in #hold" do
50
+ @cpool.hold {}
51
+ @cpool.allocated.size.should == 0
52
+ end
53
+
54
+ specify "#hold should return the value of its block" do
55
+ @cpool.hold {:block_return}.should == :block_return
56
+ end
57
+
58
+ specify "#make_new should not make more than max_size connections" do
59
+ @cpool.send(:make_new, :default).should == :got_connection
60
+ @cpool.send(:make_new, :default).should == :got_connection
61
+ @cpool.send(:make_new, :default).should == nil
62
+ @cpool.created_count.should == 2
63
+ end
64
+
65
+ specify ":disconnection_proc option should set the disconnection proc to use" do
66
+ @max_size.should == 2
67
+ proc{@cpool.hold{raise Sequel::DatabaseDisconnectError}}.should raise_error(Sequel::DatabaseDisconnectError)
68
+ @max_size.should == 3
69
+ end
70
+
71
+ specify "#disconnection_proc= should set the disconnection proc to use" do
72
+ a = 1
73
+ @cpool.disconnection_proc = proc{|c| a += 1}
74
+ proc{@cpool.hold{raise Sequel::DatabaseDisconnectError}}.should raise_error(Sequel::DatabaseDisconnectError)
75
+ a.should == 2
76
+ end
77
+
78
+ specify "#hold should remove the connection if a DatabaseDisconnectError is raised" do
79
+ @cpool.created_count.should == 0
80
+ @cpool.hold{Thread.new{@cpool.hold{}}; sleep 0.01}
81
+ @cpool.created_count.should == 2
82
+ proc{@cpool.hold{raise Sequel::DatabaseDisconnectError}}.should raise_error(Sequel::DatabaseDisconnectError)
83
+ @cpool.created_count.should == 1
84
+ proc{@cpool.hold{raise Sequel::DatabaseDisconnectError}}.should raise_error(Sequel::DatabaseDisconnectError)
85
+ @cpool.created_count.should == 0
86
+ proc{@cpool.hold{raise Sequel::DatabaseDisconnectError}}.should raise_error(Sequel::DatabaseDisconnectError)
87
+ @cpool.created_count.should == 0
88
+ end
89
+ end
90
+
91
+ context "A connection pool handling connection errors" do
92
+ specify "#hold should raise a Sequel::DatabaseConnectionError if an exception is raised by the connection_proc" do
93
+ cpool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS){raise Interrupt}
94
+ proc{cpool.hold{:block_return}}.should raise_error(Sequel::DatabaseConnectionError)
95
+ cpool.created_count.should == 0
96
+ end
97
+
98
+ specify "#hold should raise a Sequel::DatabaseConnectionError if nil is returned by the connection_proc" do
99
+ cpool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS){nil}
100
+ proc{cpool.hold{:block_return}}.should raise_error(Sequel::DatabaseConnectionError)
101
+ cpool.created_count.should == 0
102
+ end
103
+ end
104
+
105
+ class DummyConnection
106
+ @@value = 0
107
+ def initialize
108
+ @@value += 1
109
+ end
110
+
111
+ def value
112
+ @@value
113
+ end
114
+ end
115
+
116
+ context "ConnectionPool#hold" do
117
+ setup do
118
+ @pool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS) {DummyConnection.new}
119
+ end
120
+
121
+ specify "should pass the result of the connection maker proc to the supplied block" do
122
+ res = nil
123
+ @pool.hold {|c| res = c}
124
+ res.should be_a_kind_of(DummyConnection)
125
+ res.value.should == 1
126
+ @pool.hold {|c| res = c}
127
+ res.should be_a_kind_of(DummyConnection)
128
+ res.value.should == 1 # the connection maker is invoked only once
129
+ end
130
+
131
+ specify "should be re-entrant by the same thread" do
132
+ cc = nil
133
+ @pool.hold {|c| @pool.hold {|c| @pool.hold {|c| cc = c}}}
134
+ cc.should be_a_kind_of(DummyConnection)
135
+ end
136
+
137
+ specify "should catch exceptions and reraise them" do
138
+ proc {@pool.hold {|c| c.foobar}}.should raise_error(NoMethodError)
139
+ end
140
+
141
+ specify "should handle Exception errors (normally not caught by rescue)" do
142
+ err = nil
143
+ begin
144
+ @pool.hold {raise Exception}
145
+ rescue => e
146
+ err = e
147
+ end
148
+ err.should be_a_kind_of(RuntimeError)
149
+ end
150
+ end
151
+
152
+ context "ConnectionPool#connection_proc" do
153
+ setup do
154
+ @pool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS)
155
+ end
156
+
157
+ specify "should be nil if no block is supplied to the pool" do
158
+ @pool.connection_proc.should be_nil
159
+ proc {@pool.hold {}}.should raise_error
160
+ end
161
+
162
+ specify "should be mutable" do
163
+ @pool.connection_proc = proc {'herro'}
164
+ res = nil
165
+ proc {@pool.hold {|c| res = c}}.should_not raise_error
166
+ res.should == 'herro'
167
+ end
168
+ end
169
+
170
+ context "A connection pool with a max size of 1" do
171
+ setup do
172
+ @invoked_count = 0
173
+ @pool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS.merge(:max_connections=>1)) {@invoked_count += 1; 'herro'}
174
+ end
175
+
176
+ specify "should let only one thread access the connection at any time" do
177
+ cc,c1, c2 = nil
178
+
179
+ t1 = Thread.new {@pool.hold {|c| cc = c; c1 = c.dup; while c == 'herro';sleep 0.01;end}}
180
+ sleep 0.02
181
+ cc.should == 'herro'
182
+ c1.should == 'herro'
183
+
184
+ t2 = Thread.new {@pool.hold {|c| c2 = c.dup; while c == 'hello';sleep 0.01;end}}
185
+ sleep 0.02
186
+
187
+ # connection held by t1
188
+ t1.should be_alive
189
+ t2.should be_alive
190
+
191
+ cc.should == 'herro'
192
+ c1.should == 'herro'
193
+ c2.should be_nil
194
+
195
+ @pool.available_connections.should be_empty
196
+ @pool.allocated.should == {t1=>cc}
197
+
198
+ cc.gsub!('rr', 'll')
199
+ sleep 0.05
200
+
201
+ # connection held by t2
202
+ t1.should_not be_alive
203
+ t2.should be_alive
204
+
205
+ c2.should == 'hello'
206
+
207
+ @pool.available_connections.should be_empty
208
+ @pool.allocated.should == {t2=>cc}
209
+
210
+ cc.gsub!('ll', 'rr')
211
+ sleep 0.05
212
+
213
+ #connection released
214
+ t2.should_not be_alive
215
+
216
+ cc.should == 'herro'
217
+
218
+ @invoked_count.should == 1
219
+ @pool.size.should == 1
220
+ @pool.available_connections.should == [cc]
221
+ @pool.allocated.should be_empty
222
+ end
223
+
224
+ specify "should let the same thread reenter #hold" do
225
+ c1, c2, c3 = nil
226
+ @pool.hold do |c|
227
+ c1 = c
228
+ @pool.hold do |c|
229
+ c2 = c
230
+ @pool.hold do |c|
231
+ c3 = c
232
+ end
233
+ end
234
+ end
235
+ c1.should == 'herro'
236
+ c2.should == 'herro'
237
+ c3.should == 'herro'
238
+
239
+ @invoked_count.should == 1
240
+ @pool.size.should == 1
241
+ @pool.available_connections.size.should == 1
242
+ @pool.allocated.should be_empty
243
+ end
244
+ end
245
+
246
+ context "A connection pool with a max size of 5" do
247
+ setup do
248
+ @invoked_count = 0
249
+ @pool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS.merge(:max_connections=>5)) {@invoked_count += 1}
250
+ end
251
+
252
+ specify "should let five threads simultaneously access separate connections" do
253
+ cc = {}
254
+ threads = []
255
+ stop = nil
256
+
257
+ 5.times {|i| threads << Thread.new {@pool.hold {|c| cc[i] = c; while !stop;sleep 0.01;end}}; sleep 0.01}
258
+ sleep 0.02
259
+ threads.each {|t| t.should be_alive}
260
+ cc.size.should == 5
261
+ @invoked_count.should == 5
262
+ @pool.size.should == 5
263
+ @pool.available_connections.should be_empty
264
+ i = 0
265
+ h = {}
266
+ threads.each{|t| h[t] = (i+=1)}
267
+ @pool.allocated.should == h
268
+
269
+ threads[0].raise "your'e dead"
270
+ sleep 0.01
271
+ threads[3].raise "your'e dead too"
272
+
273
+ sleep 0.01
274
+
275
+ @pool.available_connections.should == [1, 4]
276
+ @pool.allocated.should == {threads[1]=>2, threads[2]=>3, threads[4]=>5}
277
+
278
+ stop = true
279
+ sleep 0.02
280
+
281
+ @pool.available_connections.size.should == 5
282
+ @pool.allocated.should be_empty
283
+ end
284
+
285
+ specify "should block threads until a connection becomes available" do
286
+ cc = {}
287
+ threads = []
288
+ stop = nil
289
+
290
+ 5.times {|i| threads << Thread.new {@pool.hold {|c| cc[i] = c; while !stop;sleep 0.01;end}}; sleep 0.01}
291
+ sleep 0.02
292
+ threads.each {|t| t.should be_alive}
293
+ @pool.available_connections.should be_empty
294
+
295
+ 3.times {|i| threads << Thread.new {@pool.hold {|c| cc[i + 5] = c}}}
296
+
297
+ sleep 0.02
298
+ threads[5].should be_alive
299
+ threads[6].should be_alive
300
+ threads[7].should be_alive
301
+ cc.size.should == 5
302
+ cc[5].should be_nil
303
+ cc[6].should be_nil
304
+ cc[7].should be_nil
305
+
306
+ stop = true
307
+ sleep 0.05
308
+
309
+ threads.each {|t| t.should_not be_alive}
310
+
311
+ @pool.size.should == 5
312
+ @invoked_count.should == 5
313
+ @pool.available_connections.size.should == 5
314
+ @pool.allocated.should be_empty
315
+ end
316
+ end
317
+
318
+ context "ConnectionPool#disconnect" do
319
+ setup do
320
+ @count = 0
321
+ @pool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS.merge(:max_connections=>5)) {{:id => @count += 1}}
322
+ end
323
+
324
+ specify "should invoke the given block for each available connection" do
325
+ threads = []
326
+ stop = nil
327
+ 5.times {|i| threads << Thread.new {@pool.hold {|c| while !stop;sleep 0.01;end}}; sleep 0.01}
328
+ while @pool.size < 5
329
+ sleep 0.02
330
+ end
331
+ stop = true
332
+ sleep 0.1
333
+ threads.each {|t| t.join}
334
+
335
+ @pool.size.should == 5
336
+ @pool.available_connections.size.should == 5
337
+ @pool.available_connections.each {|c| c[:id].should_not be_nil}
338
+ conns = []
339
+ @pool.disconnect {|c| conns << c}
340
+ conns.size.should == 5
341
+ end
342
+
343
+ specify "should remove all available connections" do
344
+ threads = []
345
+ stop = nil
346
+ 5.times {|i| threads << Thread.new {@pool.hold {|c| while !stop;sleep 0.01;end}}; sleep 0.01}
347
+ while @pool.size < 5
348
+ sleep 0.02
349
+ end
350
+ stop = true
351
+ sleep 0.1
352
+ threads.each {|t| t.join}
353
+
354
+ @pool.size.should == 5
355
+ @pool.disconnect
356
+ @pool.size.should == 0
357
+ end
358
+
359
+ specify "should not touch connections in use" do
360
+ threads = []
361
+ stop = nil
362
+ 5.times {|i| threads << Thread.new {@pool.hold {|c| while !stop;sleep 0.01;end}}; sleep 0.01}
363
+ while @pool.size < 5
364
+ sleep 0.02
365
+ end
366
+ stop = true
367
+ sleep 0.1
368
+ threads.each {|t| t.join}
369
+
370
+ @pool.size.should == 5
371
+
372
+ @pool.hold do |conn|
373
+ @pool.available_connections.size.should == 4
374
+ @pool.available_connections.each {|c| c.should_not be(conn)}
375
+ conns = []
376
+ @pool.disconnect {|c| conns << c}
377
+ conns.size.should == 4
378
+ end
379
+ @pool.size.should == 1
380
+ end
381
+ end
382
+
383
+ context "A connection pool with multiple servers" do
384
+ setup do
385
+ @invoked_counts = Hash.new(0)
386
+ @pool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS.merge(:servers=>{:read_only=>{}})){|server| "#{server}#{@invoked_counts[server] += 1}"}
387
+ end
388
+
389
+ specify "should use the :default server by default" do
390
+ @pool.size.should == 0
391
+ @pool.hold do |c|
392
+ c.should == "default1"
393
+ @pool.allocated.should == {Thread.current=>"default1"}
394
+ end
395
+ @pool.available_connections.should == ["default1"]
396
+ @pool.size.should == 1
397
+ @invoked_counts.should == {:default=>1}
398
+ end
399
+
400
+ specify "should use the requested server if server is given" do
401
+ @pool.size(:read_only).should == 0
402
+ @pool.hold(:read_only) do |c|
403
+ c.should == "read_only1"
404
+ @pool.allocated(:read_only).should == {Thread.current=>"read_only1"}
405
+ end
406
+ @pool.available_connections(:read_only).should == ["read_only1"]
407
+ @pool.size(:read_only).should == 1
408
+ @invoked_counts.should == {:read_only=>1}
409
+ end
410
+
411
+ specify "#hold should only yield connections for the server requested" do
412
+ @pool.hold(:read_only) do |c|
413
+ c.should == "read_only1"
414
+ @pool.allocated(:read_only).should == {Thread.current=>"read_only1"}
415
+ @pool.hold do |d|
416
+ d.should == "default1"
417
+ @pool.hold do |e|
418
+ e.should == d
419
+ @pool.hold(:read_only){|b| b.should == c}
420
+ end
421
+ @pool.allocated.should == {Thread.current=>"default1"}
422
+ end
423
+ end
424
+ @invoked_counts.should == {:read_only=>1, :default=>1}
425
+ end
426
+
427
+ specify "#disconnect should disconnect from all servers" do
428
+ @pool.hold(:read_only){}
429
+ @pool.hold{}
430
+ conns = []
431
+ @pool.size.should == 1
432
+ @pool.size(:read_only).should == 1
433
+ @pool.disconnect{|c| conns << c}
434
+ conns.sort.should == %w'default1 read_only1'
435
+ @pool.size.should == 0
436
+ @pool.size(:read_only).should == 0
437
+ @pool.hold(:read_only){|c| c.should == 'read_only2'}
438
+ @pool.hold{|c| c.should == 'default2'}
439
+ end
440
+ end
441
+
442
+ context "SingleThreadedPool" do
443
+ setup do
444
+ @pool = Sequel::SingleThreadedPool.new(CONNECTION_POOL_DEFAULTS){1234}
445
+ end
446
+
447
+ specify "should provide a #hold method" do
448
+ conn = nil
449
+ @pool.hold {|c| conn = c}
450
+ conn.should == 1234
451
+ end
452
+
453
+ specify "should provide a #disconnect method" do
454
+ @pool.hold {|c|}
455
+ @pool.conn.should == 1234
456
+ conn = nil
457
+ @pool.disconnect {|c| conn = c}
458
+ conn.should == 1234
459
+ @pool.conn.should be_nil
460
+ end
461
+ end
462
+
463
+ context "A single threaded pool with multiple servers" do
464
+ setup do
465
+ @max_size=2
466
+ @pool = Sequel::SingleThreadedPool.new(CONNECTION_POOL_DEFAULTS.merge(:disconnection_proc=>proc{|c| @max_size=3}, :servers=>{:read_only=>{}})){|server| server}
467
+ end
468
+
469
+ specify "should use the :default server by default" do
470
+ @pool.hold{|c| c.should == :default}
471
+ @pool.conn.should == :default
472
+ end
473
+
474
+ specify "should use the requested server if server is given" do
475
+ @pool.hold(:read_only){|c| c.should == :read_only}
476
+ @pool.conn(:read_only).should == :read_only
477
+ end
478
+
479
+ specify "#hold should only yield connections for the server requested" do
480
+ @pool.hold(:read_only) do |c|
481
+ c.should == :read_only
482
+ @pool.hold do |d|
483
+ d.should == :default
484
+ @pool.hold do |e|
485
+ e.should == d
486
+ @pool.hold(:read_only){|b| b.should == c}
487
+ end
488
+ end
489
+ end
490
+ @pool.conn.should == :default
491
+ @pool.conn(:read_only).should == :read_only
492
+ end
493
+
494
+ specify "#disconnect should disconnect from all servers" do
495
+ @pool.hold(:read_only){}
496
+ @pool.hold{}
497
+ conns = []
498
+ @pool.conn.should == :default
499
+ @pool.conn(:read_only).should == :read_only
500
+ @pool.disconnect{|c| conns << c}
501
+ conns.sort_by{|x| x.to_s}.should == [:default, :read_only]
502
+ @pool.conn.should == nil
503
+ @pool.conn(:read_only).should == nil
504
+ end
505
+
506
+ specify ":disconnection_proc option should set the disconnection proc to use" do
507
+ @max_size.should == 2
508
+ proc{@pool.hold{raise Sequel::DatabaseDisconnectError}}.should raise_error(Sequel::DatabaseDisconnectError)
509
+ @max_size.should == 3
510
+ end
511
+
512
+ specify "#disconnection_proc= should set the disconnection proc to use" do
513
+ a = 1
514
+ @pool.disconnection_proc = proc{|c| a += 1}
515
+ proc{@pool.hold{raise Sequel::DatabaseDisconnectError}}.should raise_error(Sequel::DatabaseDisconnectError)
516
+ a.should == 2
517
+ end
518
+
519
+ specify "#hold should remove the connection if a DatabaseDisconnectError is raised" do
520
+ @pool.instance_variable_get(:@conns).length.should == 0
521
+ @pool.hold{}
522
+ @pool.instance_variable_get(:@conns).length.should == 1
523
+ proc{@pool.hold{raise Sequel::DatabaseDisconnectError}}.should raise_error(Sequel::DatabaseDisconnectError)
524
+ @pool.instance_variable_get(:@conns).length.should == 0
525
+ end
526
+ end
@@ -0,0 +1,156 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ context "Array#extract_options!" do
4
+ specify "should pop the last item if it is a hash" do
5
+ a = [1,2,{1=>2}]
6
+ a.extract_options!.should == {1=>2}
7
+ a.should == [1,2]
8
+ end
9
+
10
+ specify "should return an empty hash if the last item is not a hash" do
11
+ a = [1,2]
12
+ a.extract_options!.should == {}
13
+ a.should == [1,2]
14
+ end
15
+ end
16
+
17
+ context "Enumerable#send_each" do
18
+ specify "should send the supplied method to each item" do
19
+ a = ['abbc', 'bbccdd', 'hebtre']
20
+ a.send_each(:gsub!, 'b', '_')
21
+ a.should == ['a__c', '__ccdd', 'he_tre']
22
+ end
23
+ end
24
+
25
+ context "Range#interval" do
26
+ specify "should return the interval between the beginning and end for an inclusive range" do
27
+ (1..10).interval.should == 9
28
+
29
+ r = rand(100000) + 10
30
+ t1 = Time.now.to_i; t2 = t1 + r
31
+ (t1..t2).interval.should == r
32
+ end
33
+
34
+ specify "should return the interval between the beginning and end for an exclusive range" do
35
+ (1...10).interval.should == 8
36
+
37
+ r = rand(100000) + 10
38
+ t1 = Time.now.to_i; t2 = t1 + r
39
+ (t1...t2).interval.should == r - 1
40
+ end
41
+ end
42
+
43
+ context "Module#class_attr_reader" do
44
+ specify "it should create instance methods that call class methods of the same name" do
45
+ @c = Class.new do
46
+ def self.x; 1; end
47
+ class_attr_reader :x
48
+ end
49
+ @c.new.x.should == 1
50
+ def @c.x; 2; end
51
+ @c.new.x.should == 2
52
+ end
53
+ end
54
+
55
+ context "Module#metaalias" do
56
+ specify "it should create aliases of singleton/class methods" do
57
+ @c = Class.new do
58
+ def self.x; 1; end
59
+ metaalias :y, :x
60
+ end
61
+ @c.y.should == 1
62
+ def @c.x; 2; end
63
+ @c.y.should == 1
64
+ end
65
+ end
66
+
67
+ context "Module#metaattr_reader" do
68
+ specify "it should create attr_readers of singleton/class methods" do
69
+ @c = Class.new do
70
+ @y = 1
71
+ @x = 2
72
+ metaattr_reader :y, :x
73
+ end
74
+ @c.y.should == 1
75
+ @c.x.should == 2
76
+ end
77
+ end
78
+
79
+ context "Object#is_one_of?" do
80
+ specify "it should be true if the object is one of the classes" do
81
+ 1.is_one_of?(Numeric, Array).should == true
82
+ [].is_one_of?(Numeric, Array).should == true
83
+ {}.is_one_of?(Numeric, Enumerable).should == true
84
+ end
85
+
86
+ specify "it should be false if the object is not one of the classes" do
87
+ 'a'.is_one_of?(Numeric, Array).should == false
88
+ Object.new.is_one_of?(Numeric, Array).should == false
89
+ end
90
+ end
91
+
92
+ context "Object#blank?" do
93
+ specify "it should be true if the object responds true to empty?" do
94
+ [].blank?.should == true
95
+ {}.blank?.should == true
96
+ o = Object.new
97
+ def o.empty?; true; end
98
+ o.blank?.should == true
99
+ end
100
+
101
+ specify "it should be false if the object doesn't respond true to empty?" do
102
+ [2].blank?.should == false
103
+ {1=>2}.blank?.should == false
104
+ Object.new.blank?.should == false
105
+ end
106
+ end
107
+
108
+ context "Numeric#blank?" do
109
+ specify "it should always be false" do
110
+ 1.blank?.should == false
111
+ 0.blank?.should == false
112
+ -1.blank?.should == false
113
+ 1.0.blank?.should == false
114
+ 0.0.blank?.should == false
115
+ -1.0.blank?.should == false
116
+ 10000000000000000.blank?.should == false
117
+ -10000000000000000.blank?.should == false
118
+ 10000000000000000.0.blank?.should == false
119
+ -10000000000000000.0.blank?.should == false
120
+ end
121
+ end
122
+
123
+ context "NilClass#blank?" do
124
+ specify "it should always be true" do
125
+ nil.blank?.should == true
126
+ end
127
+ end
128
+
129
+ context "TrueClass#blank?" do
130
+ specify "it should always be false" do
131
+ true.blank?.should == false
132
+ end
133
+ end
134
+
135
+ context "FalseClass#blank?" do
136
+ specify "it should always be true" do
137
+ false.blank?.should == true
138
+ end
139
+ end
140
+
141
+ context "FalseClass#blank?" do
142
+ specify "it should be true if the string is empty" do
143
+ ''.blank?.should == true
144
+ end
145
+ specify "it should be true if the string is composed of just whitespace" do
146
+ ' '.blank?.should == true
147
+ "\r\n\t".blank?.should == true
148
+ (' '*4000).blank?.should == true
149
+ ("\r\n\t"*4000).blank?.should == true
150
+ end
151
+ specify "it should be false if the string has any non whitespace characters" do
152
+ '1'.blank?.should == false
153
+ ("\r\n\t"*4000 + 'a').blank?.should == false
154
+ ("\r\na\t"*4000).blank?.should == false
155
+ end
156
+ end