em-mongo 0.2.13 → 0.2.14

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.
@@ -118,9 +118,10 @@ module EM::Mongo
118
118
  @retries = 0
119
119
  @responses = {}
120
120
  @is_connected = false
121
- @host = options[:host] || DEFAULT_IP
122
- @port = options[:port] || DEFAULT_PORT
123
- @on_unbind = options[:unbind_cb] || proc {}
121
+ @host = options[:host] || DEFAULT_IP
122
+ @port = options[:port] || DEFAULT_PORT
123
+ @on_unbind = options[:unbind_cb] || proc {}
124
+ @reconnect_in = options[:reconnect_in]|| false
124
125
 
125
126
  @on_close = proc {
126
127
  raise Error, "failure with mongodb server #{@host}:#{@port}"
@@ -130,7 +131,7 @@ module EM::Mongo
130
131
  end
131
132
 
132
133
  def self.connect(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = nil)
133
- opt = {:host => host, :port => port, :timeout => timeout}.merge(opts)
134
+ opt = {:host => host, :port => port, :timeout => timeout, :reconnect_in => false}.merge(opts)
134
135
  EM.connect(host, port, self, opt)
135
136
  end
136
137
 
@@ -214,19 +215,21 @@ module EM::Mongo
214
215
 
215
216
  set_deferred_status(nil)
216
217
 
217
- if @retries >= MAX_RETRIES
218
+ if @reconnect_in
219
+ EM.add_timer(@reconnect_in) { reconnect(@host, @port) }
220
+ elsif @on_unbind and @retries >= MAX_RETRIES
218
221
  @on_unbind.call
219
222
  return
220
223
  end
221
224
 
222
225
  @retries += 1
223
- EM.add_timer(5) { reconnect(@host, @port) }
226
+
224
227
  end
225
228
 
226
229
  def close
227
230
  @on_close = proc { yield if block_given? }
228
231
  if @responses.empty?
229
- close_connection
232
+ close_connection_after_writing
230
233
  else
231
234
  @close_pending = true
232
235
  end
@@ -253,6 +256,7 @@ module EM::Mongo
253
256
  @em_connection.close
254
257
  end
255
258
  end
259
+
256
260
  class Connection
257
261
  def initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {})
258
262
  @em_connection = EMConnection.connect(host, port, timeout, opts)
@@ -0,0 +1,223 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__) + '/../')
2
+
3
+ describe EMMongo::Collection do
4
+ include EM::Spec
5
+
6
+ it 'should insert an object' do
7
+ @conn, @coll = connection_and_collection
8
+
9
+ obj = @coll .insert('hello' => 'world')
10
+ obj.keys.should include '_id'
11
+ obj['_id'].should be_a_kind_of(BSON::ObjectId)
12
+ done
13
+ end
14
+
15
+ it 'should insert an object with a custom _id' do
16
+ @conn, @coll = connection_and_collection
17
+
18
+ obj = @coll .insert('_id' => 1234, 'hello' => 'world')
19
+ obj.keys.should include '_id'
20
+ obj['_id'].should == 1234
21
+ r = @coll.find({"hello" => "world"},{}) do |res|
22
+ res.size.should >= 1
23
+ res[0]['_id'].should == 1234
24
+ done
25
+ end
26
+ end
27
+
28
+ it 'should find an object by attribute' do
29
+ @conn, @coll = connection_and_collection
30
+
31
+ @coll.insert("hello" => 'world')
32
+ r = @coll.find({"hello" => "world"},{}) do |res|
33
+ res.size.should >= 1
34
+ res[0]["hello"].should == "world"
35
+ done
36
+ end
37
+ end
38
+
39
+ it 'should find an object by symbol' do
40
+ @conn, @coll = connection_and_collection
41
+
42
+ @coll.insert('hello' => 'world')
43
+ r = @coll.find({:hello => "world"},{}) do |res|
44
+ res.size.should >= 1
45
+ res[0]["hello"].should == "world"
46
+ done
47
+ end
48
+ end
49
+
50
+ it 'should find an object by id' do
51
+ @conn, @coll = connection_and_collection
52
+
53
+ obj = @coll.insert('hello' => 'world')
54
+ @coll.find({'_id' => obj['_id']},{}) do |res|
55
+ res.size.should >= 1
56
+ res[0]['hello'].should == "world"
57
+ done
58
+ end
59
+ end
60
+
61
+ it 'should find all objects' do
62
+ @conn, @coll = connection_and_collection
63
+
64
+ @coll.insert('one' => 'one')
65
+ @coll.insert('two' => 'two')
66
+ @coll.find do |res|
67
+ res.size.should >= 2
68
+ done
69
+ end
70
+ end
71
+
72
+ it 'should find large sets of objects' do
73
+ @conn, @coll = connection_and_collection
74
+
75
+ (0..1500).each { |n| @coll.insert({n.to_s => n.to_s}) }
76
+ @coll.find do |res|
77
+ res.size.should == EM::Mongo::DEFAULT_QUERY_DOCS
78
+ @coll.find({}, {:limit => 1500}) do |res|
79
+ res.size.should == 1500
80
+ done
81
+ end
82
+ end
83
+ end
84
+
85
+ it 'should update an object' do
86
+ @conn, @coll = connection_and_collection
87
+
88
+ obj = @coll.insert('hello' => 'world')
89
+ @coll.update({'hello' => 'world'}, {'hello' => 'newworld'})
90
+ @coll.find({'_id' => obj['_id']},{}) do |res|
91
+ res[0]['hello'].should == 'newworld'
92
+ done
93
+ end
94
+ end
95
+
96
+ it 'should update an object wxith $inc' do
97
+ @conn, @coll = connection_and_collection
98
+
99
+ obj = @coll.insert('hello' => 'world')
100
+ @coll.update({'hello' => 'world'}, {'$inc' => {'count' => 1}})
101
+ @coll.find({'_id' => obj['_id']},{}) do |res|
102
+ res.first['hello'].should == 'world'
103
+ res.first['count'].should == 1
104
+ done
105
+ end
106
+ end
107
+
108
+ it 'should remove an object' do
109
+ @conn, @coll = connection_and_collection
110
+
111
+ obj = @coll.insert('hello' => 'world')
112
+ @coll.remove('_id' => obj['_id'])
113
+ @coll.find({'hello' => "world"}) do |res|
114
+ res.size.should == 0
115
+ done
116
+ end
117
+ end
118
+
119
+ it 'should remove all objects' do
120
+ @conn, @coll = connection_and_collection
121
+
122
+ @coll.insert('one' => 'one')
123
+ @coll.insert('two' => 'two')
124
+ @coll.remove
125
+ @coll.find do |res|
126
+ res.size.should == 0
127
+ done
128
+ end
129
+ end
130
+
131
+ it 'should insert a Time' do
132
+ @conn, @coll = connection_and_collection
133
+
134
+ t = Time.now.utc.freeze
135
+ @coll.insert('date' => t)
136
+ @coll.find do |res|
137
+ res[0]['date'].to_s.should == t.to_s
138
+ done
139
+ end
140
+ end
141
+
142
+ it 'should insert a complex object' do
143
+ @conn, @coll = connection_and_collection
144
+
145
+ obj = {
146
+ 'array' => [1,2,3],
147
+ 'float' => 123.456,
148
+ 'hash' => {'boolean' => true},
149
+ 'nil' => nil,
150
+ 'symbol' => :name,
151
+ 'string' => 'hello world',
152
+ 'time' => Time.now.to_f,
153
+ 'regex' => /abc$/ix
154
+ }
155
+ retobj = @coll.insert(obj)
156
+ @coll.find({'_id' => obj['_id']}) do |ret|
157
+ ret.size.should == 1
158
+ ret[0].each_key do |key|
159
+ ret[0][key].should == obj[key]
160
+ end
161
+ done
162
+ end
163
+ end
164
+
165
+ it 'should find an object using nested properties' do
166
+ @conn, @coll = connection_and_collection
167
+
168
+ @coll.insert({
169
+ 'name' => 'Google',
170
+ 'address' => {
171
+ 'cxity' => 'Mountain View',
172
+ 'state' => 'California'}
173
+ })
174
+
175
+ @coll.first('address.cxity' => 'Mountain View') do |res|
176
+ res['name'].should == 'Google'
177
+ done
178
+ end
179
+ end
180
+
181
+ it 'should find objects wxith specific values' do
182
+ @conn, @coll = connection_and_collection
183
+
184
+ number_hash.each do |num, word|
185
+ @coll.insert({'num' => num, 'word' => word})
186
+ end
187
+
188
+ @coll.find({'num' => {'$in' => [1,3,5]}}) do |res|
189
+ res.size.should == 3
190
+ res.map{|r| r['num'] }.sort.should == [1,3,5]
191
+ done
192
+ end
193
+ end
194
+
195
+ it 'should find objects greater than something' do
196
+ @conn, @coll = connection_and_collection
197
+
198
+ number_hash.each do |num, word|
199
+ @coll.insert('num' => num, 'word' => word)
200
+ end
201
+
202
+ @coll.find({'num' => {'$gt' => 3}}) do |res|
203
+ res.size.should == 6
204
+ res.map{|r| r['num'] }.sort.should == [4,5,6,7,8,9]
205
+ done
206
+ end
207
+ end
208
+
209
+ it 'should handle multiple pending queries' do
210
+ @conn, @coll = connection_and_collection
211
+
212
+ id = @coll.insert("foo" => "bar")['_id']
213
+ received = 0
214
+
215
+ 10.times do |n|
216
+ @coll.first("_id" => id) do |res|
217
+ received += 1
218
+ done
219
+ end
220
+ end
221
+ end
222
+
223
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__) + '/../')
2
+
3
+ describe EMMongo::Connection do
4
+ include EM::Spec
5
+ include EM::Mongo
6
+
7
+ it 'should connect' do
8
+ @conn = EMMongo::Connection.new
9
+ EM.add_timer(0.1) do
10
+ @conn.should be_connected
11
+ done
12
+ end
13
+ end
14
+
15
+ it 'should close' do
16
+ @conn = EMMongo::Connection.new
17
+
18
+ EM.add_timer(0.1) do
19
+ @conn.should be_connected
20
+ @conn.close
21
+ end
22
+
23
+ EM.add_timer(0.2) do
24
+ EM.next_tick do
25
+ @conn.should_not be_connected
26
+ done
27
+ end
28
+ end
29
+ end
30
+
31
+ it 'should reconnect' do
32
+ @conn = EMMongo::Connection.new(DEFAULT_IP, DEFAULT_PORT, nil, {:reconnect_in => 0.5})
33
+ EM.add_timer(0.1) do
34
+ @conn.close
35
+ end
36
+ EM.add_timer(0.9) do
37
+ @conn.should be_connected
38
+ done
39
+ end
40
+ end
41
+
42
+ it 'should instantiate a Database' do
43
+ @conn = EMMongo::Connection.new
44
+
45
+ db1 = @conn.db
46
+ db1.should be_kind_of(EM::Mongo::Database)
47
+
48
+ db2 = @conn.db('db2')
49
+ db2.should be_kind_of(EM::Mongo::Database)
50
+ db2.should_not == db1
51
+
52
+ done
53
+ end
54
+
55
+
56
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 13
9
- version: 0.2.13
8
+ - 14
9
+ version: 0.2.14
10
10
  platform: ruby
11
11
  authors:
12
12
  - bcg
@@ -59,9 +59,8 @@ files:
59
59
  - lib/em-mongo/collection.rb
60
60
  - lib/em-mongo/connection.rb
61
61
  - lib/em-mongo.rb
62
- - spec/collection_spec.rb
63
- - spec/connection_spec.rb
64
- - spec/spec_helper.rb
62
+ - spec/integration/collection_spec.rb
63
+ - spec/integration/connection_spec.rb
65
64
  has_rdoc: true
66
65
  homepage:
67
66
  licenses: []
@@ -95,6 +94,5 @@ signing_key:
95
94
  specification_version: 3
96
95
  summary: EventMachine driver for MongoDB.
97
96
  test_files:
98
- - spec/collection_spec.rb
99
- - spec/connection_spec.rb
100
- - spec/spec_helper.rb
97
+ - spec/integration/collection_spec.rb
98
+ - spec/integration/connection_spec.rb
@@ -1,218 +0,0 @@
1
- require File.expand_path('spec_helper', File.dirname(__FILE__))
2
-
3
- describe EMMongo::Collection do
4
- include EM::SpecHelper
5
-
6
- before(:all) do
7
- @numbers = {
8
- 1 => 'one',
9
- 2 => 'two',
10
- 3 => 'three',
11
- 4 => 'four',
12
- 5 => 'five',
13
- 6 => 'six',
14
- 7 => 'seven',
15
- 8 => 'eight',
16
- 9 => 'nine'
17
- }
18
- end
19
-
20
- after(:all) do
21
- end
22
-
23
- it 'should insert an object' do
24
- EM::Spec::Mongo.collection do |collection|
25
- obj = collection.insert('hello' => 'world')
26
- obj.keys.should include '_id'
27
- obj['_id'].should be_a_kind_of(BSON::ObjectId)
28
- EM::Spec::Mongo.close
29
- end
30
- end
31
-
32
- it 'should find an object by attribute' do
33
- EM::Spec::Mongo.collection do |collection|
34
- collection.insert("hello" => 'world')
35
- r = collection.find({"hello" => "world"},{}) do |res|
36
- res.size.should >= 1
37
- res[0]["hello"].should == "world"
38
- EM::Spec::Mongo.close
39
- end
40
- end
41
- end
42
-
43
- it 'should find an object by id' do
44
- EM::Spec::Mongo.collection do |collection|
45
- obj = collection.insert('hello' => 'world')
46
- collection.find({'_id' => obj['_id']},{}) do |res|
47
- res.size.should >= 1
48
- res[0]['hello'].should == "world"
49
- EM::Spec::Mongo.close
50
- end
51
- end
52
- end
53
-
54
- it 'should find all objects' do
55
- EM::Spec::Mongo.collection do |collection|
56
- collection.insert('one' => 'one')
57
- collection.insert('two' => 'two')
58
- collection.find do |res|
59
- res.size.should >= 2
60
- EM::Spec::Mongo.close
61
- end
62
- end
63
- end
64
-
65
- it 'should find large sets of objects' do
66
- EM::Spec::Mongo.collection do |collection|
67
- (0..1500).each { |n| collection.insert({n.to_s => n.to_s}) }
68
- collection.find do |res|
69
- res.size.should == EM::Mongo::DEFAULT_QUERY_DOCS
70
- collection.find({}, {:limit => 1500}) do |res|
71
- res.size.should == 1500
72
- EM::Spec::Mongo.close
73
- end
74
- end
75
- end
76
- end
77
-
78
- it 'should update an object' do
79
- EM::Spec::Mongo.collection do |collection|
80
- obj = collection.insert('hello' => 'world')
81
- collection.update({'hello' => 'world'}, {'hello' => 'newworld'})
82
- collection.find({'_id' => obj['_id']},{}) do |res|
83
- res[0]['hello'].should == 'newworld'
84
- EM::Spec::Mongo.close
85
- end
86
- end
87
- end
88
-
89
- it 'should update an object with $inc' do
90
- EM::Spec::Mongo.collection do |collection|
91
- obj = collection.insert('hello' => 'world')
92
- collection.update({'hello' => 'world'}, {'$inc' => {'count' => 1}})
93
- collection.find({'_id' => obj['_id']},{}) do |res|
94
- res.first['hello'].should == 'world'
95
- res.first['count'].should == 1
96
- EM::Spec::Mongo.close
97
- end
98
- end
99
- end
100
-
101
- it 'should remove an object' do
102
- EM::Spec::Mongo.collection do |collection|
103
- obj = collection.insert('hello' => 'world')
104
- collection.remove('_id' => obj['_id'])
105
- collection.find({'hello' => "world"}) do |res|
106
- res.size.should == 0
107
- EM::Spec::Mongo.close
108
- end
109
- end
110
- end
111
-
112
- it 'should remove all objects' do
113
- EM::Spec::Mongo.collection do |collection|
114
- collection.insert('one' => 'one')
115
- collection.insert('two' => 'two')
116
- collection.remove
117
- collection.find do |res|
118
- res.size.should == 0
119
- EM::Spec::Mongo.close
120
- end
121
- end
122
- end
123
-
124
- it 'should insert a Time' do
125
- EM::Spec::Mongo.collection do |collection|
126
- t = Time.now.utc.freeze
127
- collection.insert('date' => t)
128
- collection.find do |res|
129
- res[0]['date'].to_s.should == t.to_s
130
- EM::Spec::Mongo.close
131
- end
132
- end
133
- end
134
-
135
- it 'should insert a complex object' do
136
- EM::Spec::Mongo.collection do |collection|
137
- obj = {
138
- 'array' => [1,2,3],
139
- 'float' => 123.456,
140
- 'hash' => {'boolean' => true},
141
- 'nil' => nil,
142
- 'symbol' => :name,
143
- 'string' => 'hello world',
144
- 'time' => Time.now.to_f,
145
- 'regex' => /abc$/ix
146
- }
147
- retobj = collection.insert(obj)
148
- collection.find({'_id' => obj['_id']}) do |ret|
149
- ret.size.should == 1
150
- ret[0].each_key do |key|
151
- ret[0][key].should == obj[key]
152
- end
153
- EM::Spec::Mongo.close
154
- end
155
-
156
- end
157
- end
158
-
159
- it 'should find an object using nested properties' do
160
- EM::Spec::Mongo.collection do |collection|
161
- collection.insert({
162
- 'name' => 'Google',
163
- 'address' => {
164
- 'city' => 'Mountain View',
165
- 'state' => 'California'}
166
- })
167
-
168
- collection.first('address.city' => 'Mountain View') do |res|
169
- res['name'].should == 'Google'
170
- EM::Spec::Mongo.close
171
- end
172
- end
173
- end
174
-
175
- it 'should find objects with specific values' do
176
- EM::Spec::Mongo.collection do |collection|
177
- @numbers.each do |num, word|
178
- collection.insert({'num' => num, 'word' => word})
179
- end
180
-
181
- collection.find({'num' => {'$in' => [1,3,5]}}) do |res|
182
- res.size.should == 3
183
- res.map{|r| r['num'] }.sort.should == [1,3,5]
184
- EM::Spec::Mongo.close
185
- end
186
- end
187
- end
188
-
189
- it 'should find objects greater than something' do
190
- EM::Spec::Mongo.collection do |collection|
191
- @numbers.each do |num, word|
192
- collection.insert('num' => num, 'word' => word)
193
- end
194
-
195
- collection.find({'num' => {'$gt' => 3}}) do |res|
196
- res.size.should == 6
197
- res.map{|r| r['num'] }.sort.should == [4,5,6,7,8,9]
198
- EM::Spec::Mongo.close
199
- end
200
- end
201
- end
202
-
203
- it 'should handle multiple pending queries' do
204
- EM::Spec::Mongo.collection do |collection|
205
- id = collection.insert("foo" => "bar")['_id']
206
- received = 0
207
-
208
- 10.times do |n|
209
- collection.first("_id" => id) do |res|
210
- received += 1
211
- EM::Spec::Mongo.close if received == 10
212
- end
213
- end
214
-
215
- end
216
- end
217
-
218
- end
@@ -1,48 +0,0 @@
1
- require File.expand_path('spec_helper', File.dirname(__FILE__))
2
-
3
- describe EMMongo::Connection do
4
- include EM::SpecHelper
5
-
6
- it 'should connect' do
7
- em do
8
- connection = EMMongo::Connection.new
9
- EM.next_tick do
10
- connection.should be_connected
11
- done
12
- end
13
- end
14
- end
15
-
16
- it 'should close' do
17
- em do
18
- connection = EMMongo::Connection.new
19
-
20
- EM.add_timer(1) do
21
- connection.should be_connected
22
- connection.close
23
- end
24
-
25
- EM.add_timer(2) do
26
- EM.next_tick do
27
- connection.should_not be_connected
28
- done
29
- end
30
- end
31
- end
32
- end
33
-
34
- it 'should instantiate a Database' do
35
- EM::Spec::Mongo.connection do |connection|
36
- db1 = connection.db
37
- db1.should be_kind_of(EM::Mongo::Database)
38
-
39
- db2 = connection.db('db2')
40
- db2.should be_kind_of(EM::Mongo::Database)
41
- db2.should_not == db1
42
-
43
- EM::Spec::Mongo.close
44
- end
45
- end
46
-
47
-
48
- end
data/spec/spec_helper.rb DELETED
@@ -1,43 +0,0 @@
1
- require File.expand_path('../lib/em-mongo', File.dirname(__FILE__))
2
-
3
- require "em-spec/rspec"
4
-
5
- module EM
6
- module Spec
7
- module Mongo
8
- extend EM::SpecHelper
9
-
10
- @@clean_collection_up = nil
11
-
12
- def self.close
13
- @@clean_collection_up.call if @@clean_collection_up
14
- done
15
- end
16
-
17
- def self.collection
18
- self.database do |database|
19
- database.collection.remove
20
- yield database.collection
21
- end
22
- end
23
-
24
- def self.database
25
- self.connection do |connection|
26
- yield connection.db
27
- end
28
- end
29
-
30
- def self.connection
31
- em do
32
- connection = EMMongo::Connection.new
33
- EM.next_tick do
34
- yield connection
35
- end
36
- end
37
- end
38
-
39
- end
40
- end
41
- end
42
-
43
- $stdout.sync = true