mongo 1.10.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/LICENSE +190 -0
- data/README.md +149 -0
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/bin/mongo_console +43 -0
- data/ext/jsasl/target/jsasl.jar +0 -0
- data/lib/mongo.rb +90 -0
- data/lib/mongo/bulk_write_collection_view.rb +380 -0
- data/lib/mongo/collection.rb +1164 -0
- data/lib/mongo/collection_writer.rb +364 -0
- data/lib/mongo/connection.rb +19 -0
- data/lib/mongo/connection/node.rb +239 -0
- data/lib/mongo/connection/pool.rb +347 -0
- data/lib/mongo/connection/pool_manager.rb +325 -0
- data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
- data/lib/mongo/connection/socket.rb +18 -0
- data/lib/mongo/connection/socket/socket_util.rb +37 -0
- data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
- data/lib/mongo/connection/socket/tcp_socket.rb +86 -0
- data/lib/mongo/connection/socket/unix_socket.rb +39 -0
- data/lib/mongo/cursor.rb +719 -0
- data/lib/mongo/db.rb +735 -0
- data/lib/mongo/exception.rb +88 -0
- data/lib/mongo/functional.rb +21 -0
- data/lib/mongo/functional/authentication.rb +318 -0
- data/lib/mongo/functional/logging.rb +85 -0
- data/lib/mongo/functional/read_preference.rb +174 -0
- data/lib/mongo/functional/sasl_java.rb +48 -0
- data/lib/mongo/functional/uri_parser.rb +374 -0
- data/lib/mongo/functional/write_concern.rb +66 -0
- data/lib/mongo/gridfs.rb +18 -0
- data/lib/mongo/gridfs/grid.rb +112 -0
- data/lib/mongo/gridfs/grid_ext.rb +53 -0
- data/lib/mongo/gridfs/grid_file_system.rb +163 -0
- data/lib/mongo/gridfs/grid_io.rb +484 -0
- data/lib/mongo/legacy.rb +140 -0
- data/lib/mongo/mongo_client.rb +702 -0
- data/lib/mongo/mongo_replica_set_client.rb +523 -0
- data/lib/mongo/mongo_sharded_client.rb +159 -0
- data/lib/mongo/networking.rb +370 -0
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo/utils/conversions.rb +110 -0
- data/lib/mongo/utils/core_ext.rb +70 -0
- data/lib/mongo/utils/server_version.rb +69 -0
- data/lib/mongo/utils/support.rb +80 -0
- data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
- data/mongo.gemspec +36 -0
- data/test/functional/authentication_test.rb +35 -0
- data/test/functional/bulk_api_stress_test.rb +133 -0
- data/test/functional/bulk_write_collection_view_test.rb +1129 -0
- data/test/functional/client_test.rb +565 -0
- data/test/functional/collection_test.rb +2073 -0
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/functional/conversions_test.rb +163 -0
- data/test/functional/cursor_fail_test.rb +63 -0
- data/test/functional/cursor_message_test.rb +57 -0
- data/test/functional/cursor_test.rb +625 -0
- data/test/functional/db_api_test.rb +819 -0
- data/test/functional/db_connection_test.rb +27 -0
- data/test/functional/db_test.rb +344 -0
- data/test/functional/grid_file_system_test.rb +285 -0
- data/test/functional/grid_io_test.rb +252 -0
- data/test/functional/grid_test.rb +273 -0
- data/test/functional/pool_test.rb +62 -0
- data/test/functional/safe_test.rb +98 -0
- data/test/functional/ssl_test.rb +29 -0
- data/test/functional/support_test.rb +62 -0
- data/test/functional/timeout_test.rb +58 -0
- data/test/functional/uri_test.rb +330 -0
- data/test/functional/write_concern_test.rb +118 -0
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +317 -0
- data/test/replica_set/authentication_test.rb +35 -0
- data/test/replica_set/basic_test.rb +174 -0
- data/test/replica_set/client_test.rb +341 -0
- data/test/replica_set/complex_connect_test.rb +77 -0
- data/test/replica_set/connection_test.rb +138 -0
- data/test/replica_set/count_test.rb +64 -0
- data/test/replica_set/cursor_test.rb +212 -0
- data/test/replica_set/insert_test.rb +140 -0
- data/test/replica_set/max_values_test.rb +145 -0
- data/test/replica_set/pinning_test.rb +55 -0
- data/test/replica_set/query_test.rb +73 -0
- data/test/replica_set/read_preference_test.rb +214 -0
- data/test/replica_set/refresh_test.rb +175 -0
- data/test/replica_set/replication_ack_test.rb +94 -0
- data/test/replica_set/ssl_test.rb +32 -0
- data/test/sharded_cluster/basic_test.rb +197 -0
- data/test/shared/authentication/basic_auth_shared.rb +286 -0
- data/test/shared/authentication/bulk_api_auth_shared.rb +259 -0
- data/test/shared/authentication/gssapi_shared.rb +164 -0
- data/test/shared/authentication/sasl_plain_shared.rb +96 -0
- data/test/shared/ssl_shared.rb +235 -0
- data/test/test_helper.rb +56 -0
- data/test/threading/basic_test.rb +120 -0
- data/test/tools/mongo_config.rb +608 -0
- data/test/tools/mongo_config_test.rb +160 -0
- data/test/unit/client_test.rb +347 -0
- data/test/unit/collection_test.rb +166 -0
- data/test/unit/connection_test.rb +325 -0
- data/test/unit/cursor_test.rb +299 -0
- data/test/unit/db_test.rb +136 -0
- data/test/unit/grid_test.rb +76 -0
- data/test/unit/mongo_sharded_client_test.rb +48 -0
- data/test/unit/node_test.rb +93 -0
- data/test/unit/pool_manager_test.rb +142 -0
- data/test/unit/read_pref_test.rb +115 -0
- data/test/unit/read_test.rb +159 -0
- data/test/unit/safe_test.rb +158 -0
- data/test/unit/sharding_pool_manager_test.rb +84 -0
- data/test/unit/write_concern_test.rb +175 -0
- metadata +260 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
class ComplexConnectTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
ensure_cluster(:rs)
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown
|
24
|
+
@client.close if defined?(@conn) && @conn
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_complex_connect
|
28
|
+
host = @rs.servers.first.host
|
29
|
+
primary = MongoClient.new(host, @rs.primary.port)
|
30
|
+
|
31
|
+
@client = MongoReplicaSetClient.new([
|
32
|
+
@rs.servers[2].host_port,
|
33
|
+
@rs.servers[1].host_port,
|
34
|
+
@rs.servers[0].host_port
|
35
|
+
])
|
36
|
+
|
37
|
+
version = @client.server_version
|
38
|
+
|
39
|
+
@client[TEST_DB]['complext-connect-test'].insert({:a => 1})
|
40
|
+
assert @client[TEST_DB]['complext-connect-test'].find_one
|
41
|
+
|
42
|
+
config = primary['local']['system.replset'].find_one
|
43
|
+
old_config = config.dup
|
44
|
+
config['version'] += 1
|
45
|
+
|
46
|
+
# eliminate exception: can't find self in new replset config
|
47
|
+
port_to_delete = @rs.servers.collect(&:port).find{|port| port != primary.port}.to_s
|
48
|
+
|
49
|
+
config['members'].delete_if do |member|
|
50
|
+
member['host'].include?(port_to_delete)
|
51
|
+
end
|
52
|
+
|
53
|
+
assert_raise ConnectionFailure do
|
54
|
+
primary['admin'].command({:replSetReconfig => config})
|
55
|
+
end
|
56
|
+
@rs.start
|
57
|
+
|
58
|
+
assert_raise ConnectionFailure do
|
59
|
+
perform_step_down(primary)
|
60
|
+
end
|
61
|
+
|
62
|
+
# isMaster is currently broken in 2.1+ when called on removed nodes
|
63
|
+
puts version
|
64
|
+
if version < "2.1"
|
65
|
+
rescue_connection_failure do
|
66
|
+
assert @client[TEST_DB]['complext-connect-test'].find_one
|
67
|
+
end
|
68
|
+
|
69
|
+
assert @client[TEST_DB]['complext-connect-test'].find_one
|
70
|
+
end
|
71
|
+
|
72
|
+
primary = MongoClient.new(host, @rs.primary.port)
|
73
|
+
assert_raise ConnectionFailure do
|
74
|
+
primary['admin'].command({:replSetReconfig => old_config})
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
class ReplicaSetConnectionTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
ensure_cluster(:rs)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_connect_with_deprecated_multi
|
24
|
+
silently do
|
25
|
+
@connection = Connection.multi(@rs.repl_set_seeds_old, :name => @rs.repl_set_name)
|
26
|
+
end
|
27
|
+
assert !@connection.nil?
|
28
|
+
assert @connection.connected?
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_connect_bad_name
|
32
|
+
assert_raise_error(ReplicaSetConnectionError, "-wrong") do
|
33
|
+
@connection = ReplSetConnection.new(@rs.repl_set_seeds, :safe => true, :name => @rs.repl_set_name + "-wrong")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_connect_with_first_secondary_node_terminated
|
38
|
+
@rs.secondaries.first.stop
|
39
|
+
|
40
|
+
rescue_connection_failure do
|
41
|
+
@connection = ReplSetConnection.new @rs.repl_set_seeds
|
42
|
+
end
|
43
|
+
assert @connection.connected?
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_connect_with_last_secondary_node_terminated
|
47
|
+
@rs.secondaries.last.stop
|
48
|
+
|
49
|
+
rescue_connection_failure do
|
50
|
+
@connection = ReplSetConnection.new @rs.repl_set_seeds
|
51
|
+
end
|
52
|
+
assert @connection.connected?
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_connect_with_connection_string
|
56
|
+
@connection = Connection.from_uri("mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}")
|
57
|
+
assert !@connection.nil?
|
58
|
+
assert @connection.connected?
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_connect_with_connection_string_in_env_var
|
62
|
+
uri = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}"
|
63
|
+
with_preserved_env_uri(uri) do
|
64
|
+
@connection = ReplSetConnection.new
|
65
|
+
assert !@connection.nil?
|
66
|
+
assert_equal 3, @connection.seeds.length
|
67
|
+
assert_equal @rs.replicas[0].host, @connection.seeds[0][0]
|
68
|
+
assert_equal @rs.replicas[1].host, @connection.seeds[1][0]
|
69
|
+
assert_equal @rs.replicas[2].host, @connection.seeds[2][0]
|
70
|
+
assert_equal @rs.replicas[0].port, @connection.seeds[0][1]
|
71
|
+
assert_equal @rs.replicas[1].port, @connection.seeds[1][1]
|
72
|
+
assert_equal @rs.replicas[2].port, @connection.seeds[2][1]
|
73
|
+
assert_equal @rs.repl_set_name, @connection.replica_set_name
|
74
|
+
assert @connection.connected?
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_connect_with_connection_string_in_implicit_mongodb_uri
|
79
|
+
uri = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name}"
|
80
|
+
with_preserved_env_uri(uri) do
|
81
|
+
@connection = Connection.from_uri
|
82
|
+
assert !@connection.nil?
|
83
|
+
assert_equal 3, @connection.seeds.length
|
84
|
+
assert_equal @rs.replicas[0].host, @connection.seeds[0][0]
|
85
|
+
assert_equal @rs.replicas[1].host, @connection.seeds[1][0]
|
86
|
+
assert_equal @rs.replicas[2].host, @connection.seeds[2][0]
|
87
|
+
assert_equal @rs.replicas[0].port, @connection.seeds[0][1]
|
88
|
+
assert_equal @rs.replicas[1].port, @connection.seeds[1][1]
|
89
|
+
assert_equal @rs.replicas[2].port, @connection.seeds[2][1]
|
90
|
+
assert_equal @rs.repl_set_name, @connection.replica_set_name
|
91
|
+
assert @connection.connected?
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_connect_with_new_seed_format
|
96
|
+
@connection = ReplSetConnection.new @rs.repl_set_seeds
|
97
|
+
assert @connection.connected?
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_connect_with_old_seed_format
|
101
|
+
silently do
|
102
|
+
@connection = ReplSetConnection.new(@rs.repl_set_seeds_old)
|
103
|
+
end
|
104
|
+
assert @connection.connected?
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_connect_with_full_connection_string
|
108
|
+
@connection = Connection.from_uri("mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true")
|
109
|
+
assert !@connection.nil?
|
110
|
+
assert @connection.connected?
|
111
|
+
assert_equal 2, @connection.write_concern[:w]
|
112
|
+
assert @connection.write_concern[:fsync]
|
113
|
+
assert @connection.read_pool
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_connect_with_full_connection_string_in_env_var
|
117
|
+
uri = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true"
|
118
|
+
with_preserved_env_uri(uri) do
|
119
|
+
@connection = ReplSetConnection.new
|
120
|
+
assert !@connection.nil?
|
121
|
+
assert @connection.connected?
|
122
|
+
assert_equal 2, @connection.write_concern[:w]
|
123
|
+
assert @connection.write_concern[:fsync]
|
124
|
+
assert @connection.read_pool
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_connect_options_override_env_var
|
129
|
+
uri = "mongodb://#{@rs.repl_set_seeds_uri}?replicaset=#{@rs.repl_set_name};safe=true;w=2;fsync=true;slaveok=true"
|
130
|
+
with_preserved_env_uri(uri) do
|
131
|
+
@connection = ReplSetConnection.new({:safe => {:w => 1}})
|
132
|
+
assert !@connection.nil?
|
133
|
+
assert @connection.connected?
|
134
|
+
assert_equal 1, @connection.write_concern[:w]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
class ReplicaSetCountTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
ensure_cluster(:rs)
|
21
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :read => :primary_preferred)
|
22
|
+
assert @client.primary_pool
|
23
|
+
@primary = MongoClient.new(@client.primary_pool.host, @client.primary_pool.port)
|
24
|
+
@db = @client.db(TEST_DB)
|
25
|
+
@db.drop_collection("test-sets")
|
26
|
+
@coll = @db.collection("test-sets")
|
27
|
+
end
|
28
|
+
|
29
|
+
def teardown
|
30
|
+
@client.close if @conn
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_correct_count_after_insertion_reconnect
|
34
|
+
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
|
35
|
+
assert_equal 1, @coll.count
|
36
|
+
|
37
|
+
# Kill the current master node
|
38
|
+
@rs.primary.stop
|
39
|
+
|
40
|
+
rescue_connection_failure do
|
41
|
+
@coll.insert({:a => 30})
|
42
|
+
end
|
43
|
+
|
44
|
+
@coll.insert({:a => 40})
|
45
|
+
assert_equal 3, @coll.count, "Second count failed"
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_count_command_sent_to_primary
|
49
|
+
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
|
50
|
+
count_before = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
|
51
|
+
assert_equal 1, @coll.count
|
52
|
+
count_after = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
|
53
|
+
assert_equal 2, count_after - count_before
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_count_with_read
|
57
|
+
@coll.insert({:a => 20}, :w => 3, :wtimeout => 10000)
|
58
|
+
count_before = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
|
59
|
+
assert_equal 1, @coll.count(:read => :secondary)
|
60
|
+
assert_equal 1, @coll.find({}, :read => :secondary).count()
|
61
|
+
count_after = @primary['admin'].command({:serverStatus => 1})['opcounters']['command']
|
62
|
+
assert_equal 1, count_after - count_before
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,212 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
class ReplicaSetCursorTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
ensure_cluster(:rs)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_get_more_primary
|
24
|
+
setup_client(:primary)
|
25
|
+
cursor_get_more_test(:primary)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_get_more_secondary
|
29
|
+
setup_client(:secondary)
|
30
|
+
cursor_get_more_test(:secondary)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_close_primary
|
34
|
+
setup_client(:primary)
|
35
|
+
kill_cursor_test(:primary)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_close_secondary
|
39
|
+
setup_client(:secondary)
|
40
|
+
kill_cursor_test(:secondary)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_cursors_get_closed
|
44
|
+
setup_client
|
45
|
+
assert_cursors_on_members
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_cursors_get_closed_secondary
|
49
|
+
setup_client(:secondary)
|
50
|
+
assert_cursors_on_members(:secondary)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_cursors_get_closed_secondary_query
|
54
|
+
setup_client(:primary)
|
55
|
+
assert_cursors_on_members(:secondary)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_intervening_query_secondary
|
59
|
+
setup_client(:primary)
|
60
|
+
refresh_while_iterating(:secondary)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def setup_client(read=:primary)
|
66
|
+
route_read ||= read
|
67
|
+
# Setup ReplicaSet Connection
|
68
|
+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :read => read)
|
69
|
+
|
70
|
+
@db = @client.db(TEST_DB)
|
71
|
+
@db.drop_collection("cursor_tests")
|
72
|
+
@coll = @db.collection("cursor_tests")
|
73
|
+
insert_docs
|
74
|
+
|
75
|
+
# Setup Direct Connections
|
76
|
+
@primary = Mongo::MongoClient.new(*@client.manager.primary)
|
77
|
+
end
|
78
|
+
|
79
|
+
def insert_docs
|
80
|
+
@n_docs = 102 # batch size is 101
|
81
|
+
@n_docs.times do |i|
|
82
|
+
@coll.insert({ "x" => i }, :w => 3)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def set_read_client_and_tag(read)
|
87
|
+
read_opts = {:read => read}
|
88
|
+
@tag = (0...3).map{|i|i.to_s}.detect do |tag|
|
89
|
+
begin
|
90
|
+
read_opts[:tag_sets] = [{:node => tag}] unless read == :primary
|
91
|
+
cursor = @coll.find({}, read_opts)
|
92
|
+
cursor.next
|
93
|
+
pool = cursor.instance_variable_get(:@pool)
|
94
|
+
cursor.close
|
95
|
+
@read = Mongo::MongoClient.new(pool.host, pool.port, :slave_ok => true)
|
96
|
+
tag
|
97
|
+
rescue Mongo::ConnectionFailure
|
98
|
+
false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def route_query(read)
|
104
|
+
read_opts = {:read => read}
|
105
|
+
read_opts[:tag_sets] = [{:node => @tag}] unless read == :primary
|
106
|
+
object_id = BSON::ObjectId.new
|
107
|
+
read_opts[:comment] = object_id
|
108
|
+
|
109
|
+
# set profiling level to 2 on client and member to which the query will be routed
|
110
|
+
@client.db(TEST_DB).profiling_level = :all
|
111
|
+
@client.secondaries.each do |node|
|
112
|
+
node = Mongo::MongoClient.new(node[0], node[1], :slave_ok => true)
|
113
|
+
node.db(TEST_DB).profiling_level = :all
|
114
|
+
end
|
115
|
+
|
116
|
+
@cursor = @coll.find({}, read_opts)
|
117
|
+
@cursor.next
|
118
|
+
|
119
|
+
# on client and other members set profiling level to 0
|
120
|
+
@client.db(TEST_DB).profiling_level = :off
|
121
|
+
@client.secondaries.each do |node|
|
122
|
+
node = Mongo::MongoClient.new(node[0], node[1], :slave_ok => true)
|
123
|
+
node.db(TEST_DB).profiling_level = :off
|
124
|
+
end
|
125
|
+
# do a query on system.profile of the reader to see if it was used for the query
|
126
|
+
profiled_queries = @read.db(TEST_DB).collection('system.profile').find({
|
127
|
+
'ns' => "#{TEST_DB}.cursor_tests", "query.$comment" => object_id })
|
128
|
+
|
129
|
+
assert_equal 1, profiled_queries.count
|
130
|
+
end
|
131
|
+
|
132
|
+
# batch from send_initial_query is 101 documents
|
133
|
+
# check that you get n_docs back from the query, with the same port
|
134
|
+
def cursor_get_more_test(read=:primary)
|
135
|
+
set_read_client_and_tag(read)
|
136
|
+
10.times do
|
137
|
+
# assert that the query went to the correct member
|
138
|
+
route_query(read)
|
139
|
+
docs_count = 1
|
140
|
+
port = @cursor.instance_variable_get(:@pool).port
|
141
|
+
assert @cursor.alive?
|
142
|
+
while @cursor.has_next?
|
143
|
+
docs_count += 1
|
144
|
+
@cursor.next
|
145
|
+
assert_equal port, @cursor.instance_variable_get(:@pool).port
|
146
|
+
end
|
147
|
+
assert !@cursor.alive?
|
148
|
+
assert_equal @n_docs, docs_count
|
149
|
+
@cursor.close #cursor is already closed
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# batch from get_more can be huge, so close after send_initial_query
|
154
|
+
def kill_cursor_test(read=:primary)
|
155
|
+
set_read_client_and_tag(read)
|
156
|
+
10.times do
|
157
|
+
# assert that the query went to the correct member
|
158
|
+
route_query(read)
|
159
|
+
cursor_id = @cursor.cursor_id
|
160
|
+
cursor_clone = @cursor.clone
|
161
|
+
assert_equal cursor_id, cursor_clone.cursor_id
|
162
|
+
assert @cursor.instance_variable_get(:@pool)
|
163
|
+
# .next was called once already and leave one for get more
|
164
|
+
(@n_docs-2).times { @cursor.next }
|
165
|
+
@cursor.close
|
166
|
+
# an exception confirms the cursor has indeed been closed
|
167
|
+
assert_raise Mongo::OperationFailure do
|
168
|
+
cursor_clone.next
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def assert_cursors_on_members(read=:primary)
|
174
|
+
set_read_client_and_tag(read)
|
175
|
+
# assert that the query went to the correct member
|
176
|
+
route_query(read)
|
177
|
+
cursor_id = @cursor.cursor_id
|
178
|
+
cursor_clone = @cursor.clone
|
179
|
+
assert_equal cursor_id, cursor_clone.cursor_id
|
180
|
+
assert @cursor.instance_variable_get(:@pool)
|
181
|
+
port = @cursor.instance_variable_get(:@pool).port
|
182
|
+
while @cursor.has_next?
|
183
|
+
@cursor.next
|
184
|
+
assert_equal port, @cursor.instance_variable_get(:@pool).port
|
185
|
+
end
|
186
|
+
# an exception confirms the cursor has indeed been closed after query
|
187
|
+
assert_raise Mongo::OperationFailure do
|
188
|
+
cursor_clone.next
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def refresh_while_iterating(read)
|
193
|
+
set_read_client_and_tag(read)
|
194
|
+
|
195
|
+
read_opts = {:read => read}
|
196
|
+
read_opts[:tag_sets] = [{:node => @tag}]
|
197
|
+
read_opts[:batch_size] = 2
|
198
|
+
cursor = @coll.find({}, read_opts)
|
199
|
+
|
200
|
+
2.times { cursor.next }
|
201
|
+
port = cursor.instance_variable_get(:@pool).port
|
202
|
+
host = cursor.instance_variable_get(:@pool).host
|
203
|
+
# Refresh connection
|
204
|
+
@client.refresh
|
205
|
+
assert_nothing_raised do
|
206
|
+
cursor.next
|
207
|
+
end
|
208
|
+
|
209
|
+
assert_equal port, cursor.instance_variable_get(:@pool).port
|
210
|
+
assert_equal host, cursor.instance_variable_get(:@pool).host
|
211
|
+
end
|
212
|
+
end
|