mongo 1.3.0 → 1.12.5

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.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/{LICENSE.txt → LICENSE} +1 -1
  4. data/README.md +122 -271
  5. data/Rakefile +25 -209
  6. data/VERSION +1 -0
  7. data/bin/mongo_console +31 -9
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +576 -269
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/connection/node.rb +249 -0
  12. data/lib/mongo/connection/pool.rb +340 -0
  13. data/lib/mongo/connection/pool_manager.rb +320 -0
  14. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  15. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  16. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  17. data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
  18. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection.rb +7 -875
  21. data/lib/mongo/cursor.rb +403 -117
  22. data/lib/mongo/db.rb +444 -243
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/functional/logging.rb +85 -0
  26. data/lib/mongo/functional/read_preference.rb +183 -0
  27. data/lib/mongo/functional/scram.rb +556 -0
  28. data/lib/mongo/functional/uri_parser.rb +409 -0
  29. data/lib/mongo/functional/write_concern.rb +66 -0
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +30 -24
  32. data/lib/mongo/gridfs/grid_ext.rb +6 -10
  33. data/lib/mongo/gridfs/grid_file_system.rb +38 -20
  34. data/lib/mongo/gridfs/grid_io.rb +84 -75
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +140 -0
  37. data/lib/mongo/mongo_client.rb +697 -0
  38. data/lib/mongo/mongo_replica_set_client.rb +535 -0
  39. data/lib/mongo/mongo_sharded_client.rb +159 -0
  40. data/lib/mongo/networking.rb +372 -0
  41. data/lib/mongo/{util → utils}/conversions.rb +29 -8
  42. data/lib/mongo/{util → utils}/core_ext.rb +28 -18
  43. data/lib/mongo/{util → utils}/server_version.rb +4 -6
  44. data/lib/mongo/{util → utils}/support.rb +29 -31
  45. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  46. data/lib/mongo/utils.rb +19 -0
  47. data/lib/mongo.rb +51 -50
  48. data/mongo.gemspec +29 -32
  49. data/test/functional/authentication_test.rb +39 -0
  50. data/test/functional/bulk_api_stress_test.rb +133 -0
  51. data/test/functional/bulk_write_collection_view_test.rb +1198 -0
  52. data/test/functional/client_test.rb +627 -0
  53. data/test/functional/collection_test.rb +2175 -0
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
  56. data/test/functional/cursor_fail_test.rb +57 -0
  57. data/test/functional/cursor_message_test.rb +56 -0
  58. data/test/functional/cursor_test.rb +683 -0
  59. data/test/functional/db_api_test.rb +835 -0
  60. data/test/functional/db_connection_test.rb +25 -0
  61. data/test/functional/db_test.rb +348 -0
  62. data/test/functional/grid_file_system_test.rb +285 -0
  63. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
  64. data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
  65. data/test/functional/pool_test.rb +136 -0
  66. data/test/functional/safe_test.rb +98 -0
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +62 -0
  69. data/test/functional/timeout_test.rb +60 -0
  70. data/test/functional/uri_test.rb +446 -0
  71. data/test/functional/write_concern_test.rb +118 -0
  72. data/test/helpers/general.rb +50 -0
  73. data/test/helpers/test_unit.rb +476 -0
  74. data/test/replica_set/authentication_test.rb +37 -0
  75. data/test/replica_set/basic_test.rb +189 -0
  76. data/test/replica_set/client_test.rb +393 -0
  77. data/test/replica_set/connection_test.rb +138 -0
  78. data/test/replica_set/count_test.rb +66 -0
  79. data/test/replica_set/cursor_test.rb +220 -0
  80. data/test/replica_set/insert_test.rb +157 -0
  81. data/test/replica_set/max_values_test.rb +151 -0
  82. data/test/replica_set/pinning_test.rb +105 -0
  83. data/test/replica_set/query_test.rb +73 -0
  84. data/test/replica_set/read_preference_test.rb +219 -0
  85. data/test/replica_set/refresh_test.rb +211 -0
  86. data/test/replica_set/replication_ack_test.rb +95 -0
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +203 -0
  89. data/test/shared/authentication/basic_auth_shared.rb +260 -0
  90. data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
  91. data/test/shared/authentication/gssapi_shared.rb +176 -0
  92. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  93. data/test/shared/authentication/scram_shared.rb +92 -0
  94. data/test/shared/ssl_shared.rb +235 -0
  95. data/test/test_helper.rb +53 -94
  96. data/test/threading/basic_test.rb +120 -0
  97. data/test/tools/mongo_config.rb +708 -0
  98. data/test/tools/mongo_config_test.rb +160 -0
  99. data/test/unit/client_test.rb +381 -0
  100. data/test/unit/collection_test.rb +89 -53
  101. data/test/unit/connection_test.rb +282 -32
  102. data/test/unit/cursor_test.rb +206 -8
  103. data/test/unit/db_test.rb +55 -13
  104. data/test/unit/grid_test.rb +43 -16
  105. data/test/unit/mongo_sharded_client_test.rb +48 -0
  106. data/test/unit/node_test.rb +93 -0
  107. data/test/unit/pool_manager_test.rb +111 -0
  108. data/test/unit/read_pref_test.rb +406 -0
  109. data/test/unit/read_test.rb +159 -0
  110. data/test/unit/safe_test.rb +69 -36
  111. data/test/unit/sharding_pool_manager_test.rb +84 -0
  112. data/test/unit/write_concern_test.rb +175 -0
  113. data.tar.gz.sig +3 -0
  114. metadata +227 -216
  115. metadata.gz.sig +0 -0
  116. data/docs/CREDITS.md +0 -123
  117. data/docs/FAQ.md +0 -116
  118. data/docs/GridFS.md +0 -158
  119. data/docs/HISTORY.md +0 -244
  120. data/docs/RELEASES.md +0 -33
  121. data/docs/REPLICA_SETS.md +0 -72
  122. data/docs/TUTORIAL.md +0 -247
  123. data/docs/WRITE_CONCERN.md +0 -28
  124. data/lib/mongo/exceptions.rb +0 -71
  125. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  126. data/lib/mongo/repl_set_connection.rb +0 -342
  127. data/lib/mongo/test.rb +0 -20
  128. data/lib/mongo/util/pool.rb +0 -177
  129. data/lib/mongo/util/uri_parser.rb +0 -185
  130. data/test/async/collection_test.rb +0 -224
  131. data/test/async/connection_test.rb +0 -24
  132. data/test/async/cursor_test.rb +0 -162
  133. data/test/async/worker_pool_test.rb +0 -99
  134. data/test/auxillary/1.4_features.rb +0 -166
  135. data/test/auxillary/authentication_test.rb +0 -68
  136. data/test/auxillary/autoreconnect_test.rb +0 -41
  137. data/test/auxillary/fork_test.rb +0 -30
  138. data/test/auxillary/repl_set_auth_test.rb +0 -58
  139. data/test/auxillary/slave_connection_test.rb +0 -36
  140. data/test/auxillary/threaded_authentication_test.rb +0 -101
  141. data/test/bson/binary_test.rb +0 -15
  142. data/test/bson/bson_test.rb +0 -649
  143. data/test/bson/byte_buffer_test.rb +0 -208
  144. data/test/bson/hash_with_indifferent_access_test.rb +0 -38
  145. data/test/bson/json_test.rb +0 -17
  146. data/test/bson/object_id_test.rb +0 -154
  147. data/test/bson/ordered_hash_test.rb +0 -204
  148. data/test/bson/timestamp_test.rb +0 -24
  149. data/test/collection_test.rb +0 -910
  150. data/test/connection_test.rb +0 -309
  151. data/test/cursor_fail_test.rb +0 -75
  152. data/test/cursor_message_test.rb +0 -43
  153. data/test/cursor_test.rb +0 -483
  154. data/test/db_api_test.rb +0 -726
  155. data/test/db_connection_test.rb +0 -15
  156. data/test/db_test.rb +0 -287
  157. data/test/grid_file_system_test.rb +0 -243
  158. data/test/load/resque/load.rb +0 -21
  159. data/test/load/resque/processor.rb +0 -26
  160. data/test/load/thin/load.rb +0 -24
  161. data/test/load/unicorn/load.rb +0 -23
  162. data/test/load/unicorn/unicorn.rb +0 -29
  163. data/test/replica_sets/connect_test.rb +0 -94
  164. data/test/replica_sets/connection_string_test.rb +0 -32
  165. data/test/replica_sets/count_test.rb +0 -35
  166. data/test/replica_sets/insert_test.rb +0 -53
  167. data/test/replica_sets/pooled_insert_test.rb +0 -55
  168. data/test/replica_sets/query_secondaries.rb +0 -96
  169. data/test/replica_sets/query_test.rb +0 -51
  170. data/test/replica_sets/replication_ack_test.rb +0 -66
  171. data/test/replica_sets/rs_test_helper.rb +0 -27
  172. data/test/safe_test.rb +0 -68
  173. data/test/support/hash_with_indifferent_access.rb +0 -186
  174. data/test/support/keys.rb +0 -45
  175. data/test/support_test.rb +0 -18
  176. data/test/threading/threading_with_large_pool_test.rb +0 -90
  177. data/test/threading_test.rb +0 -87
  178. data/test/tools/auth_repl_set_manager.rb +0 -14
  179. data/test/tools/load.rb +0 -58
  180. data/test/tools/repl_set_manager.rb +0 -266
  181. data/test/tools/sharding_manager.rb +0 -202
  182. data/test/tools/test.rb +0 -4
  183. data/test/unit/pool_test.rb +0 -9
  184. data/test/unit/repl_set_connection_test.rb +0 -59
  185. data/test/uri_test.rb +0 -91
@@ -1,185 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # --
4
- # Copyright (C) 2008-2011 10gen Inc.
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- # ++
18
-
19
- module Mongo
20
- class URIParser
21
-
22
- DEFAULT_PORT = 27017
23
- MONGODB_URI_MATCHER = /(([-.\w]+):([^@]+)@)?([-.\w]+)(:([\w]+))?(\/([-\w]+))?/
24
- MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]"
25
- SPEC_ATTRS = [:nodes, :auths]
26
- OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync]
27
-
28
- OPT_VALID = {:connect => lambda {|arg| ['direct', 'replicaset'].include?(arg)},
29
- :replicaset => lambda {|arg| arg.length > 0},
30
- :slaveok => lambda {|arg| ['true', 'false'].include?(arg)},
31
- :safe => lambda {|arg| ['true', 'false'].include?(arg)},
32
- :w => lambda {|arg| arg =~ /^\d+$/ },
33
- :wtimeout => lambda {|arg| arg =~ /^\d+$/ },
34
- :fsync => lambda {|arg| ['true', 'false'].include?(arg)}
35
- }
36
-
37
- OPT_ERR = {:connect => "must be 'direct' or 'replicaset'",
38
- :replicaset => "must be a string containing the name of the replica set to connect to",
39
- :slaveok => "must be 'true' or 'false'",
40
- :safe => "must be 'true' or 'false'",
41
- :w => "must be an integer specifying number of nodes to replica to",
42
- :wtimeout => "must be an integer specifying milliseconds",
43
- :fsync => "must be 'true' or 'false'"
44
- }
45
-
46
- OPT_CONV = {:connect => lambda {|arg| arg},
47
- :replicaset => lambda {|arg| arg},
48
- :slaveok => lambda {|arg| arg == 'true' ? true : false},
49
- :safe => lambda {|arg| arg == 'true' ? true : false},
50
- :w => lambda {|arg| arg.to_i},
51
- :wtimeout => lambda {|arg| arg.to_i},
52
- :fsync => lambda {|arg| arg == 'true' ? true : false}
53
- }
54
-
55
- attr_reader :nodes, :auths, :connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync
56
-
57
- # Parse a MongoDB URI. This method is used by Connection.from_uri.
58
- # Returns an array of nodes and an array of db authorizations, if applicable.
59
- #
60
- # Note: passwords can contain any character except for a ','.
61
- #
62
- # @core connections
63
- def initialize(string)
64
- if string =~ /^mongodb:\/\//
65
- string = string[10..-1]
66
- else
67
- raise MongoArgumentError, "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}"
68
- end
69
-
70
- hosts, opts = string.split('?')
71
- parse_hosts(hosts)
72
- parse_options(opts)
73
- configure_connect
74
- end
75
-
76
- def connection_options
77
- opts = {}
78
-
79
- if (@w || @wtimeout || @fsync) && !@safe
80
- raise MongoArgumentError, "Safe must be true if w, wtimeout, or fsync is specified"
81
- end
82
-
83
- if @safe
84
- if @w || @wtimeout || @fsync
85
- safe_opts = {}
86
- safe_opts[:w] = @w if @w
87
- safe_opts[:wtimeout] = @wtimeout if @wtimeout
88
- safe_opts[:fsync] = @fsync if @fsync
89
- else
90
- safe_opts = true
91
- end
92
-
93
- opts[:safe] = safe_opts
94
- end
95
-
96
- if @slaveok
97
- if @connect == 'direct'
98
- opts[:slave_ok] = true
99
- else
100
- opts[:read_secondary] = true
101
- end
102
- end
103
-
104
- opts[:rs_name] = @replicaset if @replicaset
105
-
106
- opts
107
- end
108
-
109
- private
110
-
111
- def parse_hosts(hosts)
112
- @nodes = []
113
- @auths = []
114
- specs = hosts.split(',')
115
- specs.each do |spec|
116
- matches = MONGODB_URI_MATCHER.match(spec)
117
- if !matches
118
- raise MongoArgumentError, "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}"
119
- end
120
-
121
- uname = matches[2]
122
- pwd = matches[3]
123
- host = matches[4]
124
- port = matches[6] || DEFAULT_PORT
125
- if !(port.to_s =~ /^\d+$/)
126
- raise MongoArgumentError, "Invalid port #{port}; port must be specified as digits."
127
- end
128
- port = port.to_i
129
- db = matches[8]
130
-
131
- if uname && pwd && db
132
- auths << {'db_name' => db, 'username' => uname, 'password' => pwd}
133
- elsif uname || pwd || db
134
- raise MongoArgumentError, "MongoDB URI must include all three of username, password, " +
135
- "and db if any one of these is specified."
136
- end
137
-
138
- @nodes << [host, port]
139
- end
140
- end
141
-
142
- # This method uses the lambdas defined in OPT_VALID and OPT_CONV to validate
143
- # and convert the given options.
144
- def parse_options(opts)
145
- # initialize instance variables for available options
146
- OPT_VALID.keys.each { |k| instance_variable_set("@#{k}", nil) }
147
-
148
- return unless opts
149
-
150
- separator = opts.include?('&') ? '&' : ';'
151
- opts.split(separator).each do |attr|
152
- key, value = attr.split('=')
153
- key = key.to_sym
154
- value = value.strip.downcase
155
- if !OPT_ATTRS.include?(key)
156
- raise MongoArgumentError, "Invalid Mongo URI option #{key}"
157
- end
158
-
159
- if OPT_VALID[key].call(value)
160
- instance_variable_set("@#{key}", OPT_CONV[key].call(value))
161
- else
162
- raise MongoArgumentError, "Invalid value for #{key}: #{OPT_ERR[key]}"
163
- end
164
- end
165
- end
166
-
167
- def configure_connect
168
- if @nodes.length > 1 && !@connect
169
- @connect = 'replicaset'
170
- end
171
-
172
- if !@connect
173
- if @nodes.length > 1
174
- @connect = 'replicaset'
175
- else
176
- @connect = 'direct'
177
- end
178
- end
179
-
180
- if @connect == 'direct' && @replicaset
181
- raise MongoArgumentError, "If specifying a replica set name, please also specify that connect=replicaset"
182
- end
183
- end
184
- end
185
- 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