mongo 1.2.2 → 1.2.3

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,5 +1,12 @@
1
1
  # MongoDB Ruby Driver History
2
2
 
3
+ ### 1.2.3
4
+ 2011-2-22
5
+
6
+ * Updated docs for v1.8 map-reduce and ensure that :raw option
7
+ is used when no collection name is returned.
8
+ * Minor Collection#group doc update.
9
+
3
10
  ### 1.2.2
4
11
  2011-2-15
5
12
 
@@ -19,7 +19,7 @@
19
19
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
20
 
21
21
  module Mongo
22
- VERSION = "1.2.2"
22
+ VERSION = "1.2.3"
23
23
  end
24
24
 
25
25
  module Mongo
@@ -521,13 +521,18 @@ module Mongo
521
521
  # @option opts [Integer] :limit (nil) if passing a query, number of objects to return from the collection.
522
522
  # @option opts [String, BSON::Code] :finalize (nil) a javascript function to apply to the result set after the
523
523
  # map/reduce operation has finished.
524
- # @option opts [String] :out (nil) the name of the output collection. If specified, the collection will not be treated as temporary.
525
- # @option opts [Boolean] :keeptemp (false) if true, the generated collection will be persisted. default is false.
524
+ # @option opts [String] :out (nil) a valid output type. In versions of MongoDB prior to v1.7.6,
525
+ # this option takes the name of a collection for the output results. In versions 1.7.6 and later,
526
+ # this option specifies the output type. See the core docs for available output types.
527
+ # @option opts [Boolean] :keeptemp (false) if true, the generated collection will be persisted. The defualt
528
+ # is false. Note that this option has no effect is versions of MongoDB > v1.7.6.
526
529
  # @option opts [Boolean ] :verbose (false) if true, provides statistics on job execution time.
527
530
  # @option opts [Boolean] :raw (false) if true, return the raw result object from the map_reduce command, and not
528
- # the instantiated collection that's returned by default.
531
+ # the instantiated collection that's returned by default. Note if a collection name isn't returned in the
532
+ # map-reduce output (as, for example, when using :out => {:inline => 1}), then this option will be
533
+ # forced to true.
529
534
  #
530
- # @return [Collection] a collection containing the results of the operation.
535
+ # @return [Collection, Hash] a Mongo::Collection object or a Hash with the map-reduce command's results.
531
536
  #
532
537
  # @see http://www.mongodb.org/display/DOCS/MapReduce Offical MongoDB map/reduce documentation.
533
538
  #
@@ -548,7 +553,7 @@ module Mongo
548
553
  raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
549
554
  end
550
555
 
551
- if raw
556
+ if raw || !result["result"]
552
557
  result
553
558
  else
554
559
  @db[result["result"]]
@@ -576,7 +581,7 @@ module Mongo
576
581
  if opts.is_a?(Hash)
577
582
  return new_group(opts)
578
583
  else
579
- warn "Collection#group no longer take a list of paramters. This usage is deprecated." +
584
+ warn "Collection#group no longer take a list of parameters. This usage is deprecated." +
580
585
  "Check out the new API at http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method"
581
586
  end
582
587
 
@@ -0,0 +1,30 @@
1
+ # encoding:utf-8
2
+ require './test/test_helper'
3
+ require 'complex'
4
+ require 'bigdecimal'
5
+ require 'rational'
6
+
7
+ class BSONTest < Test::Unit::TestCase
8
+
9
+ include BSON
10
+
11
+ def setup
12
+ @encoder = BSON::BSON_CODER
13
+ end
14
+
15
+ def assert_doc_pass(doc, options={})
16
+ bson = @encoder.serialize(doc)
17
+ if options[:debug]
18
+ puts "DEBUGGING DOC:"
19
+ p bson.to_a
20
+ puts "DESERIALIZES TO:"
21
+ end
22
+ assert_equal @encoder.serialize(doc).to_a, bson.to_a
23
+ assert_equal doc, @encoder.deserialize(bson)
24
+ end
25
+
26
+ def test_string
27
+ assert_doc_pass({:a => "hello"})
28
+ end
29
+
30
+ end
@@ -465,6 +465,29 @@ class TestCollection < Test::Unit::TestCase
465
465
  assert res["counts"]
466
466
  assert res["timeMillis"]
467
467
  end
468
+
469
+ def test_map_reduce_with_collection_merge
470
+ @@test << {:user_id => 1}
471
+ @@test << {:user_id => 2}
472
+ output_collection = "test-map-coll"
473
+ m = Code.new("function() { emit(this.user_id, {count: 1}); }")
474
+ r = Code.new("function(k,vals) { var sum = 0;" +
475
+ " vals.forEach(function(v) { sum += v.count;} ); return {count: sum}; }")
476
+ res = @@test.map_reduce(m, r, :out => output_collection)
477
+
478
+ @@test.remove
479
+ @@test << {:user_id => 3}
480
+ res = @@test.map_reduce(m, r, :out => {:merge => output_collection})
481
+ assert res.find.to_a.any? {|doc| doc["_id"] == 3 && doc["value"]["count"] == 1}
482
+
483
+ @@test.remove
484
+ @@test << {:user_id => 3}
485
+ res = @@test.map_reduce(m, r, :out => {:reduce => output_collection})
486
+ assert res.find.to_a.any? {|doc| doc["_id"] == 3 && doc["value"]["count"] == 2}
487
+
488
+ res = @@test.map_reduce(m, r, :out => {:inline => 1})
489
+ assert res["results"]
490
+ end
468
491
  end
469
492
 
470
493
  if @@version > "1.3.0"
@@ -0,0 +1,21 @@
1
+ require './test/test_helper'
2
+ require 'logger'
3
+ require 'stringio'
4
+ require 'thread'
5
+
6
+ class TestPool < Test::Unit::TestCase
7
+
8
+ include Mongo
9
+ include BSON
10
+
11
+ def setup
12
+ @conn = standard_connection
13
+ end
14
+
15
+ def teardown
16
+ @conn[MONGO_TEST_DB].get_last_error
17
+ end
18
+
19
+ def test_foo
20
+ end
21
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
4
+ hash: 25
5
+ prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 2
10
- version: 1.2.2
9
+ - 3
10
+ version: 1.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jim Menard
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-02-15 00:00:00 -05:00
20
+ date: 2011-02-22 00:00:00 -05:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -28,12 +28,12 @@ dependencies:
28
28
  requirements:
29
29
  - - ">="
30
30
  - !ruby/object:Gem::Version
31
- hash: 27
31
+ hash: 25
32
32
  segments:
33
33
  - 1
34
34
  - 2
35
- - 2
36
- version: 1.2.2
35
+ - 3
36
+ version: 1.2.3
37
37
  type: :runtime
38
38
  version_requirements: *id001
39
39
  description: A Ruby driver for MongoDB. For more information about Mongo, see http://www.mongodb.org.
@@ -50,95 +50,88 @@ files:
50
50
  - mongo.gemspec
51
51
  - LICENSE.txt
52
52
  - lib/mongo.rb
53
- - lib/mongo/cursor.rb
54
53
  - lib/mongo/collection.rb
55
- - lib/mongo/exceptions.rb
56
54
  - lib/mongo/connection.rb
57
- - lib/mongo/repl_set_connection.rb
58
- - lib/mongo/test.rb
59
- - lib/mongo/gridfs/grid_io.rb
60
- - lib/mongo/gridfs/grid_file_system.rb
55
+ - lib/mongo/cursor.rb
56
+ - lib/mongo/db.rb
57
+ - lib/mongo/exceptions.rb
61
58
  - lib/mongo/gridfs/grid.rb
62
59
  - lib/mongo/gridfs/grid_ext.rb
60
+ - lib/mongo/gridfs/grid_file_system.rb
61
+ - lib/mongo/gridfs/grid_io.rb
63
62
  - lib/mongo/gridfs/grid_io_fix.rb
63
+ - lib/mongo/repl_set_connection.rb
64
64
  - lib/mongo/util/conversions.rb
65
- - lib/mongo/util/support.rb
66
- - lib/mongo/util/pool.rb
67
65
  - lib/mongo/util/core_ext.rb
66
+ - lib/mongo/util/pool.rb
68
67
  - lib/mongo/util/server_version.rb
68
+ - lib/mongo/util/support.rb
69
69
  - lib/mongo/util/uri_parser.rb
70
- - lib/mongo/db.rb
71
- - docs/HISTORY.md
72
- - docs/TUTORIAL.md
70
+ - docs/1.0_UPGRADE.md
73
71
  - docs/CREDITS.md
74
72
  - docs/FAQ.md
75
- - docs/REPLICA_SETS.md
76
- - docs/1.0_UPGRADE.md
77
73
  - docs/GridFS.md
74
+ - docs/HISTORY.md
75
+ - docs/REPLICA_SETS.md
76
+ - docs/TUTORIAL.md
78
77
  - docs/WRITE_CONCERN.md
79
78
  - bin/mongo_console
80
- - test/grid_file_system_test.rb
81
- - test/unit/db_test.rb
82
- - test/unit/repl_set_connection_test.rb
83
- - test/unit/collection_test.rb
84
- - test/unit/cursor_test.rb
85
- - test/unit/grid_test.rb
86
- - test/unit/connection_test.rb
87
- - test/unit/pool_test.rb
88
- - test/unit/safe_test.rb
89
- - test/db_test.rb
90
- - test/collection_test.rb
91
- - test/async/collection_test.rb
92
- - test/async/cursor_test.rb
93
- - test/async/connection_test.rb
94
- - test/async/worker_pool_test.rb
95
- - test/cursor_test.rb
96
- - test/load/unicorn/unicorn.rb
97
- - test/load/unicorn/load.rb
98
- - test/load/thin/load.rb
99
- - test/grid_test.rb
100
- - test/db_api_test.rb
101
- - test/auxillary/slave_connection_test.rb
102
- - test/auxillary/threaded_authentication_test.rb
79
+ - test/auxillary/1.4_features.rb
103
80
  - test/auxillary/authentication_test.rb
104
81
  - test/auxillary/autoreconnect_test.rb
105
82
  - test/auxillary/repl_set_auth_test.rb
106
- - test/auxillary/1.4_features.rb
107
- - test/conversions_test.rb
108
- - test/connection_test.rb
109
- - test/uri_test.rb
110
- - test/cursor_message_test.rb
111
- - test/tools/test.rb
112
- - test/tools/repl_set_manager.rb
113
- - test/tools/auth_repl_set_manager.rb
114
- - test/tools/load.rb
115
- - test/tools/sharding_manager.rb
116
- - test/cursor_fail_test.rb
117
- - test/threading/threading_with_large_pool_test.rb
118
- - test/test_helper.rb
119
- - test/grid_io_test.rb
120
- - test/bson/byte_buffer_test.rb
83
+ - test/auxillary/slave_connection_test.rb
84
+ - test/auxillary/threaded_authentication_test.rb
121
85
  - test/bson/binary_test.rb
122
- - test/bson/object_id_test.rb
123
- - test/bson/json_test.rb
86
+ - test/bson/bson_string_test.rb
124
87
  - test/bson/bson_test.rb
125
- - test/bson/ordered_hash_test.rb
88
+ - test/bson/byte_buffer_test.rb
126
89
  - test/bson/hash_with_indifferent_access_test.rb
127
- - test/support/keys.rb
128
- - test/support/hash_with_indifferent_access.rb
90
+ - test/bson/json_test.rb
91
+ - test/bson/object_id_test.rb
92
+ - test/bson/ordered_hash_test.rb
93
+ - test/collection_test.rb
94
+ - test/connection_test.rb
95
+ - test/conversions_test.rb
96
+ - test/cursor_fail_test.rb
97
+ - test/cursor_message_test.rb
98
+ - test/cursor_test.rb
99
+ - test/db_api_test.rb
129
100
  - test/db_connection_test.rb
130
- - test/replica_sets/rs_test_helper.rb
131
- - test/replica_sets/pooled_insert_test.rb
101
+ - test/db_test.rb
102
+ - test/grid_file_system_test.rb
103
+ - test/grid_io_test.rb
104
+ - test/grid_test.rb
105
+ - test/load/thin/load.rb
106
+ - test/load/unicorn/load.rb
107
+ - test/pool_test.rb
108
+ - test/replica_sets/connect_test.rb
109
+ - test/replica_sets/connection_string_test.rb
132
110
  - test/replica_sets/count_test.rb
133
- - test/replica_sets/replication_ack_test.rb
111
+ - test/replica_sets/insert_test.rb
112
+ - test/replica_sets/pooled_insert_test.rb
134
113
  - test/replica_sets/query_secondaries.rb
135
114
  - test/replica_sets/query_test.rb
136
- - test/replica_sets/connection_string_test.rb
137
- - test/replica_sets/insert_test.rb
138
- - test/replica_sets/connect_test.rb
115
+ - test/replica_sets/replication_ack_test.rb
116
+ - test/replica_sets/rs_test_helper.rb
139
117
  - test/safe_test.rb
118
+ - test/support/hash_with_indifferent_access.rb
119
+ - test/support/keys.rb
140
120
  - test/support_test.rb
121
+ - test/test_helper.rb
122
+ - test/threading/threading_with_large_pool_test.rb
141
123
  - test/threading_test.rb
124
+ - test/tools/auth_repl_set_manager.rb
125
+ - test/tools/repl_set_manager.rb
126
+ - test/unit/collection_test.rb
127
+ - test/unit/connection_test.rb
128
+ - test/unit/cursor_test.rb
129
+ - test/unit/db_test.rb
130
+ - test/unit/grid_test.rb
131
+ - test/unit/pool_test.rb
132
+ - test/unit/repl_set_connection_test.rb
133
+ - test/unit/safe_test.rb
134
+ - test/uri_test.rb
142
135
  has_rdoc: true
143
136
  homepage: http://www.mongodb.org
144
137
  licenses: []
@@ -171,70 +164,64 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
164
  requirements: []
172
165
 
173
166
  rubyforge_project:
174
- rubygems_version: 1.4.1
167
+ rubygems_version: 1.3.7
175
168
  signing_key:
176
169
  specification_version: 3
177
170
  summary: Ruby driver for the MongoDB
178
171
  test_files:
179
- - test/grid_file_system_test.rb
180
- - test/unit/db_test.rb
181
- - test/unit/repl_set_connection_test.rb
182
- - test/unit/collection_test.rb
183
- - test/unit/cursor_test.rb
184
- - test/unit/grid_test.rb
185
- - test/unit/connection_test.rb
186
- - test/unit/pool_test.rb
187
- - test/unit/safe_test.rb
188
- - test/db_test.rb
189
- - test/collection_test.rb
190
- - test/async/collection_test.rb
191
- - test/async/cursor_test.rb
192
- - test/async/connection_test.rb
193
- - test/async/worker_pool_test.rb
194
- - test/cursor_test.rb
195
- - test/load/unicorn/unicorn.rb
196
- - test/load/unicorn/load.rb
197
- - test/load/thin/load.rb
198
- - test/grid_test.rb
199
- - test/db_api_test.rb
200
- - test/auxillary/slave_connection_test.rb
201
- - test/auxillary/threaded_authentication_test.rb
172
+ - test/auxillary/1.4_features.rb
202
173
  - test/auxillary/authentication_test.rb
203
174
  - test/auxillary/autoreconnect_test.rb
204
175
  - test/auxillary/repl_set_auth_test.rb
205
- - test/auxillary/1.4_features.rb
206
- - test/conversions_test.rb
207
- - test/connection_test.rb
208
- - test/uri_test.rb
209
- - test/cursor_message_test.rb
210
- - test/tools/test.rb
211
- - test/tools/repl_set_manager.rb
212
- - test/tools/auth_repl_set_manager.rb
213
- - test/tools/load.rb
214
- - test/tools/sharding_manager.rb
215
- - test/cursor_fail_test.rb
216
- - test/threading/threading_with_large_pool_test.rb
217
- - test/test_helper.rb
218
- - test/grid_io_test.rb
219
- - test/bson/byte_buffer_test.rb
176
+ - test/auxillary/slave_connection_test.rb
177
+ - test/auxillary/threaded_authentication_test.rb
220
178
  - test/bson/binary_test.rb
221
- - test/bson/object_id_test.rb
222
- - test/bson/json_test.rb
179
+ - test/bson/bson_string_test.rb
223
180
  - test/bson/bson_test.rb
224
- - test/bson/ordered_hash_test.rb
181
+ - test/bson/byte_buffer_test.rb
225
182
  - test/bson/hash_with_indifferent_access_test.rb
226
- - test/support/keys.rb
227
- - test/support/hash_with_indifferent_access.rb
183
+ - test/bson/json_test.rb
184
+ - test/bson/object_id_test.rb
185
+ - test/bson/ordered_hash_test.rb
186
+ - test/collection_test.rb
187
+ - test/connection_test.rb
188
+ - test/conversions_test.rb
189
+ - test/cursor_fail_test.rb
190
+ - test/cursor_message_test.rb
191
+ - test/cursor_test.rb
192
+ - test/db_api_test.rb
228
193
  - test/db_connection_test.rb
229
- - test/replica_sets/rs_test_helper.rb
230
- - test/replica_sets/pooled_insert_test.rb
194
+ - test/db_test.rb
195
+ - test/grid_file_system_test.rb
196
+ - test/grid_io_test.rb
197
+ - test/grid_test.rb
198
+ - test/load/thin/load.rb
199
+ - test/load/unicorn/load.rb
200
+ - test/pool_test.rb
201
+ - test/replica_sets/connect_test.rb
202
+ - test/replica_sets/connection_string_test.rb
231
203
  - test/replica_sets/count_test.rb
232
- - test/replica_sets/replication_ack_test.rb
204
+ - test/replica_sets/insert_test.rb
205
+ - test/replica_sets/pooled_insert_test.rb
233
206
  - test/replica_sets/query_secondaries.rb
234
207
  - test/replica_sets/query_test.rb
235
- - test/replica_sets/connection_string_test.rb
236
- - test/replica_sets/insert_test.rb
237
- - test/replica_sets/connect_test.rb
208
+ - test/replica_sets/replication_ack_test.rb
209
+ - test/replica_sets/rs_test_helper.rb
238
210
  - test/safe_test.rb
211
+ - test/support/hash_with_indifferent_access.rb
212
+ - test/support/keys.rb
239
213
  - test/support_test.rb
214
+ - test/test_helper.rb
215
+ - test/threading/threading_with_large_pool_test.rb
240
216
  - test/threading_test.rb
217
+ - test/tools/auth_repl_set_manager.rb
218
+ - test/tools/repl_set_manager.rb
219
+ - test/unit/collection_test.rb
220
+ - test/unit/connection_test.rb
221
+ - test/unit/cursor_test.rb
222
+ - test/unit/db_test.rb
223
+ - test/unit/grid_test.rb
224
+ - test/unit/pool_test.rb
225
+ - test/unit/repl_set_connection_test.rb
226
+ - test/unit/safe_test.rb
227
+ - test/uri_test.rb
@@ -1,20 +0,0 @@
1
-
2
- class Foo
3
-
4
- def zed
5
- puts "Foo"
6
- end
7
-
8
- end
9
-
10
- class Bar < Foo
11
-
12
- def zed(n=nil)
13
- if n.nil?
14
- puts "Bar"
15
- else
16
- super()
17
- end
18
- end
19
-
20
- end
@@ -1,224 +0,0 @@
1
- require 'test/test_helper'
2
-
3
- class TestCollection < Test::Unit::TestCase
4
- @@connection ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
5
- @@db = @@connection.db(MONGO_TEST_DB)
6
- @@test = @@db.collection("test")
7
- @@version = @@connection.server_version
8
-
9
- def setup
10
- @@test.remove
11
- end
12
-
13
- def wait_for_async
14
- sleep 0.2
15
- end
16
-
17
- def test_async_update
18
- id1 = @@test.save("x" => 5)
19
- failsafe = mock('failsafe will get called in block', :call => true)
20
-
21
- @@test.update({}, {"$inc" => {"x" => 1}}, :async => true) do |error, result|
22
- assert_nil error
23
- assert result
24
- failsafe.call
25
- end
26
- wait_for_async
27
-
28
- assert_equal 1, @@test.count()
29
- assert_equal 6, @@test.find_one(:_id => id1)["x"]
30
- end
31
-
32
- if @@version >= "1.1.3"
33
- def test_async_multi_update
34
- failsafe = mock('failsafe will get called in block', :call => true)
35
-
36
- @@test.save("num" => 10)
37
- @@test.save("num" => 10)
38
- @@test.save("num" => 10)
39
- assert_equal 3, @@test.count
40
-
41
- @@test.update({"num" => 10}, {"$set" => {"num" => 100}}, :multi => true, :async => true) do |error, result|
42
- assert_nil error
43
- assert result
44
- failsafe.call
45
- end
46
- wait_for_async
47
- end
48
- end
49
-
50
- def test_async_upsert
51
- failsafe = mock('failsafe will get called in block')
52
- failsafe.expects(:call).times(2)
53
-
54
- @@test.update({"page" => "/"}, {"$inc" => {"count" => 1}}, :upsert => true, :async => true) do |error, result|
55
- assert_nil error
56
- assert result
57
- failsafe.call
58
- end
59
-
60
- @@test.update({"page" => "/"}, {"$inc" => {"count" => 1}}, :upsert => true, :async => true) do |error, result|
61
- assert_nil error
62
- assert result
63
- failsafe.call
64
- end
65
- wait_for_async
66
-
67
- assert_equal 1, @@test.count()
68
- assert_equal 2, @@test.find_one()["count"]
69
- end
70
-
71
- def test_async_save
72
- failsafe = mock('failsafe will get called in block', :call => true)
73
-
74
- # note that the first parameter has explicit curly brackets around
75
- # the hash; without those brackets as a delimiter, the :async key is
76
- # viewed as part of the required +document+ parameter
77
- @@test.save({"hello" => "world"}, :async => true) do |error, result|
78
- assert_nil error
79
- assert result
80
- failsafe.call
81
- end
82
- wait_for_async
83
-
84
- assert_equal "world", @@test.find_one()["hello"]
85
- end
86
-
87
- def test_async_save_with_exception
88
- failsafe = mock('failsafe will get called in block', :call => true)
89
-
90
- @@test.create_index("hello", :unique => true)
91
- @@test.save("hello" => "world")
92
-
93
- # all async calls on collections occur in :safe mode
94
- @@test.save({"hello" => "world"}, :async => true) do |error, result|
95
- assert error
96
- assert error.instance_of?(OperationFailure)
97
- assert_nil result
98
- failsafe.call
99
- end
100
- wait_for_async
101
-
102
- assert_equal 1, @@test.count()
103
- @@test.drop
104
- end
105
-
106
- def test_async_remove
107
- failsafe = mock('failsafe will get called in block', :call => true)
108
-
109
- @conn = Connection.new
110
- @db = @conn[MONGO_TEST_DB]
111
- @test = @db['test-async-remove']
112
- @test.save({:a => 50})
113
- @test.remove({}, :async => true) do |error, result|
114
- assert_nil error
115
- assert result
116
- failsafe.call
117
- end
118
- wait_for_async
119
-
120
- @test.drop
121
- end
122
-
123
- def test_async_count
124
- failsafe = mock('failsafe will get called in block', :call => true)
125
-
126
- @@test.drop
127
-
128
- @@test.save("x" => 1)
129
- @@test.save("x" => 2)
130
-
131
- @@test.count(:async => true) do |error, result|
132
- assert_nil error
133
- assert_equal 2, result
134
- failsafe.call
135
- end
136
- wait_for_async
137
- end
138
-
139
- # Note: #size is just an alias for #count.
140
- def test_async_size
141
- failsafe = mock('failsafe will get called in block', :call => true)
142
-
143
- @@test.drop
144
-
145
- @@test.save("x" => 1)
146
- @@test.save("x" => 2)
147
-
148
- @@test.size(:async => true) do |error, result|
149
- assert_nil error
150
- assert_equal 2, result
151
- failsafe.call
152
- end
153
- wait_for_async
154
- end
155
-
156
- def test_async_find_one
157
- failsafe = mock('failsafe will get called in block', :call => true)
158
-
159
- id = @@test.save("hello" => "world", "foo" => "bar")
160
-
161
- @@test.find_one({}, :async => true) do |error, result|
162
- assert_nil error
163
- assert_equal @@test.find_one(id), result
164
- failsafe.call
165
- end
166
- wait_for_async
167
- end
168
-
169
- def test_async_insert
170
- failsafe = mock('failsafe will get called in block', :call => true)
171
-
172
- doc = {"hello" => "world"}
173
- @@test.insert(doc, :async => true) do |error, result|
174
- assert_nil error
175
- assert result
176
- failsafe.call
177
- end
178
- wait_for_async
179
-
180
- assert_equal 1, @@test.count
181
- end
182
-
183
- def test_async_find
184
- assert_raise RuntimeError do
185
- @@test.find({}, :async => true)
186
- end
187
- end
188
-
189
- if @@version > "1.3.0"
190
- def test_async_find_and_modify
191
- failsafe = mock('failsafe will get called in block', :call => true)
192
-
193
- @@test << { :a => 1, :processed => false }
194
- @@test << { :a => 2, :processed => false }
195
- @@test << { :a => 3, :processed => false }
196
-
197
- @@test.find_and_modify(:query => {}, :sort => [['a', -1]], :update => {"$set" => {:processed => true}}, :async => true) do |error, result|
198
- assert_nil error
199
- assert result
200
- failsafe.call
201
- end
202
- wait_for_async
203
-
204
- assert @@test.find_one({:a => 3})['processed']
205
- end
206
-
207
- def test_async_find_and_modify_with_invalid_options
208
- failsafe = mock('failsafe will get called in block', :call => true)
209
-
210
- @@test << { :a => 1, :processed => false }
211
- @@test << { :a => 2, :processed => false }
212
- @@test << { :a => 3, :processed => false }
213
-
214
- @@test.find_and_modify(:blimey => {}, :async => true) do |error, result|
215
- assert error
216
- assert error.instance_of?(OperationFailure)
217
- assert_nil result
218
- failsafe.call
219
- end
220
- wait_for_async
221
- end
222
- end
223
-
224
- end
@@ -1,24 +0,0 @@
1
- require 'test/test_helper'
2
- include Mongo
3
-
4
- class ConnectionTest < Test::Unit::TestCase
5
- context "Initialization: " do
6
-
7
- context "given async connection options" do
8
-
9
- should "default the workers pool to 1" do
10
- Async::WorkerPool.expects(:new).with(1)
11
-
12
- Connection.new('localhost', 27017)
13
- end
14
-
15
- should "override the workers pool size with the :worker_pool_size key" do
16
- size = 6
17
- Async::WorkerPool.expects(:new).with(size)
18
-
19
- Connection.new('localhost', 27017, :worker_pool_size => size)
20
- end
21
- end # context 'given async connection options'
22
-
23
- end # context 'Initialization'
24
- end
@@ -1,162 +0,0 @@
1
- require 'test/test_helper'
2
- require 'logger'
3
-
4
- class CursorTest < Test::Unit::TestCase
5
-
6
- include Mongo
7
-
8
- @@connection = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
9
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
10
- @@db = @@connection.db(MONGO_TEST_DB)
11
- @@coll = @@db.collection('test')
12
- @@version = @@connection.server_version
13
-
14
- def wait_for_async
15
- sleep 0.2
16
- end
17
-
18
- def setup
19
- @@coll.remove
20
- @@coll.insert('a' => 1) # collection not created until it's used
21
- @@coll_full_name = "#{MONGO_TEST_DB}.test"
22
- end
23
-
24
- def test_async_explain
25
- failsafe = mock('failsafe will get called in block', :call => true)
26
-
27
- cursor = @@coll.find('a' => 1)
28
-
29
- cursor.explain(:async => true) do |error, result|
30
- assert_not_nil result['cursor']
31
- assert_kind_of Numeric, result['n']
32
- assert_kind_of Numeric, result['millis']
33
- assert_kind_of Numeric, result['nscanned']
34
- failsafe.call
35
- end
36
- wait_for_async
37
- end
38
-
39
- def test_async_count
40
- failsafe = mock('failsafe will get called in block')
41
- failsafe.expects(:call).times(3)
42
-
43
- @@coll.remove
44
-
45
- @@coll.find.count(:async => true) do |error, count|
46
- assert_equal 0, count
47
- failsafe.call
48
- end
49
- wait_for_async
50
-
51
- 10.times do |i|
52
- @@coll.save("x" => i)
53
- end
54
-
55
- @@coll.find.count(:async => true) do |error, count|
56
- assert_equal 10, count
57
- failsafe.call
58
- end
59
- wait_for_async
60
-
61
- @@coll.find({"x" => 1}).count(:async => true) do |error, count|
62
- assert_equal 1, count
63
- failsafe.call
64
- end
65
- wait_for_async
66
- end
67
-
68
- def test_async_close
69
- failsafe = mock('failsafe will get called in block', :call => true)
70
-
71
- @@coll.remove
72
- cursor = @@coll.find
73
-
74
- cursor.close(:async => true) do |error, result|
75
- assert_nil error
76
- assert result
77
- assert cursor.closed?
78
- failsafe.call
79
- end
80
- wait_for_async
81
- end
82
-
83
- def test_async_has_next
84
- failsafe = mock('failsafe will get called in block', :call => true)
85
-
86
- @@coll.remove
87
- 200.times do |n|
88
- @@coll.save("x" => n)
89
- end
90
-
91
- cursor = @@coll.find
92
- cursor.has_next?(:async => true) do |error, result|
93
- assert_nil error
94
- assert result
95
- failsafe.call
96
- end
97
- wait_for_async
98
- end
99
-
100
- def test_async_next_document
101
- failsafe = mock('failsafe will get called in block')
102
- failsafe.expects(:call).times(2)
103
-
104
- @@coll.remove
105
- 200.times do |n|
106
- @@coll.save("x" => n)
107
- end
108
-
109
- cursor = @@coll.find
110
- cursor.next_document(:async => true) do |error, result|
111
- assert_nil error
112
- assert result
113
- failsafe.call
114
- end
115
- wait_for_async
116
-
117
- callback = Proc.new do |error, result|
118
- assert_nil error
119
- assert result
120
- failsafe.call
121
- end
122
-
123
- cursor.next_document(:async => true, :callback => callback)
124
- wait_for_async
125
- end
126
-
127
- def test_async_to_a
128
- failsafe = mock('failsafe will get called in block')
129
- failsafe.expects(:call)
130
-
131
- @@coll.remove
132
- total = 200
133
- total.times do |n|
134
- @@coll.save("x" => n)
135
- end
136
-
137
- cursor = @@coll.find
138
- cursor.to_a(:async => true) do |error, result|
139
- assert_nil error
140
- assert_equal total, result.size
141
- failsafe.call
142
- end
143
- wait_for_async
144
- end
145
-
146
- def test_async_each
147
- @@coll.remove
148
- total = 200
149
- total.times do |n|
150
- @@coll.save("x" => n)
151
- end
152
-
153
- cursor = @@coll.find
154
- count = 0
155
- cursor.each(:async => true) do |error, result|
156
- count += 1
157
- end
158
- wait_for_async
159
-
160
- assert_equal total, count
161
- end
162
- end
@@ -1,99 +0,0 @@
1
- require 'test/test_helper'
2
- include Mongo
3
-
4
- class WorkerPoolTest < Test::Unit::TestCase
5
- context "Initialization: " do
6
-
7
- def wait_for_async
8
- sleep 0.2
9
- end
10
-
11
- setup do
12
- def new_mock_queue
13
- stub_everything('queue')
14
- end
15
-
16
- def new_mock_thread
17
- stub_everything('thread')
18
- end
19
- end
20
-
21
- context "given a size" do
22
- setup do
23
- @size = 5
24
- end
25
-
26
- should "allocate a Thread 'size' times" do
27
- Queue.stubs(:new).returns(new_mock_queue)
28
- Thread.expects(:new).times(@size).returns(new_mock_thread)
29
- Async::WorkerPool.new @size
30
- end
31
-
32
- should "set 'abort_on_exception' for each current thread" do
33
- Queue.stubs(:new).returns(new_mock_queue)
34
- thread = new_mock_thread
35
- Thread.stubs(:new).returns(thread)
36
-
37
- thread.expects(:abort_on_exception=).with(true).times(@size)
38
-
39
- Async::WorkerPool.new @size
40
- end
41
-
42
- should "save each thread into the workers queue" do
43
- assert_equal @size, Async::WorkerPool.new(@size).workers.size
44
- end
45
-
46
- end # context 'given a size'
47
-
48
-
49
- context "given a job" do
50
- setup do
51
- @pool = Async::WorkerPool.new 1
52
- @command = stub_everything('command')
53
- @cmd_args = stub_everything('command args')
54
- @callback = stub_everything('callback')
55
- end
56
-
57
- should "remove nils from the command args array and pass the results to the callback" do
58
- args = [nil, @cmd_args]
59
- @command.expects(:call).with(@cmd_args).returns(2)
60
- @callback.expects(:call).with(nil, 2)
61
-
62
- @pool.enqueue @command, args, @callback
63
- wait_for_async
64
- end
65
-
66
- should "execute the original command with args and pass the results to the callback" do
67
- @cmd_args.expects(:compact).returns(@cmd_args)
68
- @command.expects(:call).with(@cmd_args).returns(2)
69
- @callback.expects(:call).with(nil, 2)
70
-
71
- @pool.enqueue @command, @cmd_args, @callback
72
- wait_for_async
73
- end
74
-
75
- should "capture any exceptions and pass them to the callback" do
76
- args = [@cmd_args]
77
- error = StandardError.new
78
- @command.expects(:call).with(@cmd_args).raises(error)
79
- @callback.expects(:call).with(error, nil)
80
-
81
- @pool.enqueue @command, args, @callback
82
- wait_for_async
83
- end
84
-
85
- should "abort the thread when the callback raises an exception" do
86
- args = [@cmd_args]
87
- error = StandardError.new
88
- @callback.expects(:call).raises(error)
89
-
90
- assert_raises(StandardError) do
91
- @pool.enqueue @command, args, @callback
92
- wait_for_async
93
- end
94
- end
95
- end # context 'given a job'
96
-
97
-
98
- end
99
- end
@@ -1,29 +0,0 @@
1
- # set path to app that will be used to configure unicorn,
2
- # # note the trailing slash in this example
3
- @dir = "/home/kyle/work/10gen/ruby-driver/test/load/"
4
-
5
- worker_processes 10
6
- working_directory @dir
7
-
8
- preload_app true
9
-
10
- timeout 30
11
-
12
- # Specify path to socket unicorn listens to,
13
- # we will use this in our nginx.conf later
14
- listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64
15
-
16
- # Set process id path
17
- pid "#{@dir}tmp/pids/unicorn.pid"
18
-
19
- # # Set log file paths
20
- stderr_path "#{@dir}log/unicorn.stderr.log"
21
- stdout_path "#{@dir}log/unicorn.stdout.log"
22
-
23
- # NOTE: You need this when using forking web servers!
24
- after_fork do |server, worker|
25
- $con.close if $con
26
- $con = Mongo::Connection.new
27
- $db = $con['foo']
28
- STDERR << "FORKED #{server} #{worker}"
29
- end
@@ -1,58 +0,0 @@
1
- require 'rubygems'
2
- require 'mongo'
3
- require 'sharding_manager'
4
-
5
- class MongoLoader
6
-
7
- def initialize
8
- @mongo = Mongo::Connection.new("localhost", 50000)
9
- @data = BSON::Binary.new(File.open("tools.gz").read)
10
- @count = 0
11
- @manager = ShardingManager.new(:config_count => 3)
12
- @manager.start_cluster
13
- end
14
-
15
- def kill
16
- @manager.kill_random
17
- end
18
-
19
- def restart
20
- @manager.restart_killed_nodes
21
- end
22
-
23
- def run
24
- Thread.new do
25
- ("a".."z").each do |p|
26
- seed(p)
27
- end
28
- end
29
- end
30
-
31
- def seed(prefix)
32
- @queue = []
33
- 1000.times do |n|
34
- id = BSON::OrderedHash.new
35
- id[:p] = prefix
36
- id[:c] = n
37
- @queue << {:tid => id, :data => @data}
38
- end
39
-
40
- while @queue.length > 0 do
41
- begin
42
- doc = @queue.pop
43
- @mongo['app']['photos'].insert(doc, :safe => {:w => 3})
44
- @count += 1
45
- p @count
46
- rescue StandardError => e
47
- p e
48
- p @count
49
- @queue.push(doc)
50
- @count -= 1
51
- sleep(10)
52
- retry
53
- end
54
- end
55
- end
56
- end
57
-
58
- @m = MongoLoader.new
@@ -1,202 +0,0 @@
1
- require 'repl_set_manager'
2
- require 'thread'
3
-
4
- class ShardingManager
5
-
6
- attr_accessor :shards
7
-
8
- def initialize(opts={})
9
- @durable = opts.fetch(:durable, true)
10
- @host = "localhost"
11
-
12
- @mongos_port = opts[:mongos_port] || 50000
13
- @config_port = opts[:config_port] || 40000
14
- @shard_start_port = opts[:start_shard_port] || 30000
15
- @path = File.join(File.expand_path(File.dirname(__FILE__)), "data")
16
- system("rm -rf #{@path}")
17
-
18
- @shard_count = 2
19
- @mongos_count = 1
20
- @config_count = opts.fetch(:config_count, 1)
21
- if ![1, 3].include?(@config_count)
22
- raise ArgumentError, "Must specify 1 or 3 config servers."
23
- end
24
-
25
- @config_servers = {}
26
- @mongos_servers = {}
27
- @shards = []
28
- @ports = []
29
- end
30
-
31
- def kill_random
32
- shard_to_kill = rand(@shard_count)
33
- @shards[shard_to_kill].kill_primary
34
- end
35
-
36
- def restart_killed
37
- threads = []
38
- @shards.each do |k, shard|
39
- threads << Thread.new do
40
- shard.restart_killed_nodes
41
- end
42
- end
43
- end
44
-
45
- def start_cluster
46
- start_sharding_components
47
- start_mongos_servers
48
- configure_cluster
49
- end
50
-
51
- def configure_cluster
52
- add_shards
53
- enable_sharding
54
- shard_collection
55
- end
56
-
57
- def enable_sharding
58
- mongos['admin'].command({:enablesharding => "app"})
59
- end
60
-
61
- def shard_collection
62
- cmd = BSON::OrderedHash.new
63
- cmd[:shardcollection] = "app.photos"
64
- cmd[:key] = {:tid => 1}
65
- p mongos['admin'].command(cmd)
66
- end
67
-
68
- def add_shards
69
- @shards.each do |shard|
70
- cmd = {:addshard => shard.shard_string}
71
- p cmd
72
- p mongos['admin'].command(cmd)
73
- end
74
- p mongos['admin'].command({:listshards => 1})
75
- end
76
-
77
- def mongos
78
- attempt do
79
- @mongos ||= Mongo::Connection.new(@host, @mongos_servers[0]['port'])
80
- end
81
- end
82
-
83
- private
84
-
85
- def start_sharding_components
86
- system("killall mongos")
87
-
88
- threads = []
89
- threads << Thread.new do
90
- start_shards
91
- end
92
-
93
- threads << Thread.new do
94
- start_config_servers
95
- end
96
- threads.each {|t| t.join}
97
- puts "\nShards and config servers up!"
98
- end
99
-
100
- def start_shards
101
- threads = []
102
- @shard_count.times do |n|
103
- threads << Thread.new do
104
- port = @shard_start_port + n * 100
105
- shard = ReplSetManager.new(:arbiter_count => 0, :secondary_count => 2,
106
- :passive_count => 0, :start_port => port, :durable => @durable,
107
- :name => "shard-#{n}")
108
- shard.start_set
109
- shard.ensure_up
110
- @shards << shard
111
- end
112
- end
113
- threads.each {|t| t.join}
114
- end
115
-
116
- def start_config_servers
117
- @config_count.times do |n|
118
- @config_servers[n] ||= {}
119
- port = @config_port + n
120
- @ports << port
121
- @config_servers[n]['port'] = port
122
- @config_servers[n]['db_path'] = get_path("config-#{port}")
123
- @config_servers[n]['log_path'] = get_path("log-config-#{port}")
124
- system("rm -rf #{@config_servers[n]['db_path']}")
125
- system("mkdir -p #{@config_servers[n]['db_path']}")
126
-
127
- @config_servers[n]['start'] = start_config_cmd(n)
128
-
129
- start(@config_servers, n)
130
- end
131
- end
132
-
133
- def start_mongos_servers
134
- @mongos_count.times do |n|
135
- @mongos_servers[n] ||= {}
136
- port = @mongos_port + n
137
- @ports << port
138
- @mongos_servers[n]['port'] = port
139
- @mongos_servers[n]['db_path'] = get_path("mongos-#{port}")
140
- @mongos_servers[n]['pidfile_path'] = File.join(@mongos_servers[n]['db_path'], "mongod.lock")
141
- @mongos_servers[n]['log_path'] = get_path("log-mongos-#{port}")
142
- system("rm -rf #{@mongos_servers[n]['db_path']}")
143
- system("mkdir -p #{@mongos_servers[n]['db_path']}")
144
-
145
- @mongos_servers[n]['start'] = start_mongos_cmd(n)
146
-
147
- start(@mongos_servers, n)
148
- end
149
- end
150
-
151
- def start_config_cmd(n)
152
- cmd = "mongod --configsvr --logpath '#{@config_servers[n]['log_path']}' " +
153
- " --dbpath #{@config_servers[n]['db_path']} --port #{@config_servers[n]['port']} --fork"
154
- cmd += " --dur" if @durable
155
- cmd
156
- end
157
-
158
- def start_mongos_cmd(n)
159
- "mongos --configdb #{config_db_string} --logpath '#{@mongos_servers[n]['log_path']}' " +
160
- "--pidfilepath #{@mongos_servers[n]['pidfile_path']} --port #{@mongos_servers[n]['port']} --fork"
161
- end
162
-
163
- def config_db_string
164
- @config_servers.map do |k, v|
165
- "#{@host}:#{v['port']}"
166
- end.join(',')
167
- end
168
-
169
- def start(set, node)
170
- system(set[node]['start'])
171
- set[node]['up'] = true
172
- sleep(0.5)
173
- set[node]['pid'] = File.open(File.join(set[node]['db_path'], 'mongod.lock')).read.strip
174
- end
175
- alias :restart :start
176
-
177
- private
178
-
179
- def cleanup_config
180
- end
181
-
182
- def get_path(name)
183
- File.join(@path, name)
184
- end
185
-
186
- # TODO: put this into a shared module
187
- def attempt
188
- raise "No block given!" unless block_given?
189
- count = 0
190
-
191
- while count < 50 do
192
- begin
193
- return yield
194
- rescue Mongo::OperationFailure, Mongo::ConnectionFailure
195
- sleep(1)
196
- count += 1
197
- end
198
- end
199
-
200
- raise exception
201
- end
202
- end
@@ -1,4 +0,0 @@
1
- require 'sharding_manager'
2
-
3
- m = ShardingManager.new(:config_count => 3)
4
- m.start_cluster