mongo 1.10.0-java

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 (116) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/LICENSE +190 -0
  5. data/README.md +149 -0
  6. data/Rakefile +31 -0
  7. data/VERSION +1 -0
  8. data/bin/mongo_console +43 -0
  9. data/ext/jsasl/target/jsasl.jar +0 -0
  10. data/lib/mongo.rb +90 -0
  11. data/lib/mongo/bulk_write_collection_view.rb +380 -0
  12. data/lib/mongo/collection.rb +1164 -0
  13. data/lib/mongo/collection_writer.rb +364 -0
  14. data/lib/mongo/connection.rb +19 -0
  15. data/lib/mongo/connection/node.rb +239 -0
  16. data/lib/mongo/connection/pool.rb +347 -0
  17. data/lib/mongo/connection/pool_manager.rb +325 -0
  18. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  21. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  22. data/lib/mongo/connection/socket/tcp_socket.rb +86 -0
  23. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  24. data/lib/mongo/cursor.rb +719 -0
  25. data/lib/mongo/db.rb +735 -0
  26. data/lib/mongo/exception.rb +88 -0
  27. data/lib/mongo/functional.rb +21 -0
  28. data/lib/mongo/functional/authentication.rb +318 -0
  29. data/lib/mongo/functional/logging.rb +85 -0
  30. data/lib/mongo/functional/read_preference.rb +174 -0
  31. data/lib/mongo/functional/sasl_java.rb +48 -0
  32. data/lib/mongo/functional/uri_parser.rb +374 -0
  33. data/lib/mongo/functional/write_concern.rb +66 -0
  34. data/lib/mongo/gridfs.rb +18 -0
  35. data/lib/mongo/gridfs/grid.rb +112 -0
  36. data/lib/mongo/gridfs/grid_ext.rb +53 -0
  37. data/lib/mongo/gridfs/grid_file_system.rb +163 -0
  38. data/lib/mongo/gridfs/grid_io.rb +484 -0
  39. data/lib/mongo/legacy.rb +140 -0
  40. data/lib/mongo/mongo_client.rb +702 -0
  41. data/lib/mongo/mongo_replica_set_client.rb +523 -0
  42. data/lib/mongo/mongo_sharded_client.rb +159 -0
  43. data/lib/mongo/networking.rb +370 -0
  44. data/lib/mongo/utils.rb +19 -0
  45. data/lib/mongo/utils/conversions.rb +110 -0
  46. data/lib/mongo/utils/core_ext.rb +70 -0
  47. data/lib/mongo/utils/server_version.rb +69 -0
  48. data/lib/mongo/utils/support.rb +80 -0
  49. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  50. data/mongo.gemspec +36 -0
  51. data/test/functional/authentication_test.rb +35 -0
  52. data/test/functional/bulk_api_stress_test.rb +133 -0
  53. data/test/functional/bulk_write_collection_view_test.rb +1129 -0
  54. data/test/functional/client_test.rb +565 -0
  55. data/test/functional/collection_test.rb +2073 -0
  56. data/test/functional/collection_writer_test.rb +83 -0
  57. data/test/functional/conversions_test.rb +163 -0
  58. data/test/functional/cursor_fail_test.rb +63 -0
  59. data/test/functional/cursor_message_test.rb +57 -0
  60. data/test/functional/cursor_test.rb +625 -0
  61. data/test/functional/db_api_test.rb +819 -0
  62. data/test/functional/db_connection_test.rb +27 -0
  63. data/test/functional/db_test.rb +344 -0
  64. data/test/functional/grid_file_system_test.rb +285 -0
  65. data/test/functional/grid_io_test.rb +252 -0
  66. data/test/functional/grid_test.rb +273 -0
  67. data/test/functional/pool_test.rb +62 -0
  68. data/test/functional/safe_test.rb +98 -0
  69. data/test/functional/ssl_test.rb +29 -0
  70. data/test/functional/support_test.rb +62 -0
  71. data/test/functional/timeout_test.rb +58 -0
  72. data/test/functional/uri_test.rb +330 -0
  73. data/test/functional/write_concern_test.rb +118 -0
  74. data/test/helpers/general.rb +50 -0
  75. data/test/helpers/test_unit.rb +317 -0
  76. data/test/replica_set/authentication_test.rb +35 -0
  77. data/test/replica_set/basic_test.rb +174 -0
  78. data/test/replica_set/client_test.rb +341 -0
  79. data/test/replica_set/complex_connect_test.rb +77 -0
  80. data/test/replica_set/connection_test.rb +138 -0
  81. data/test/replica_set/count_test.rb +64 -0
  82. data/test/replica_set/cursor_test.rb +212 -0
  83. data/test/replica_set/insert_test.rb +140 -0
  84. data/test/replica_set/max_values_test.rb +145 -0
  85. data/test/replica_set/pinning_test.rb +55 -0
  86. data/test/replica_set/query_test.rb +73 -0
  87. data/test/replica_set/read_preference_test.rb +214 -0
  88. data/test/replica_set/refresh_test.rb +175 -0
  89. data/test/replica_set/replication_ack_test.rb +94 -0
  90. data/test/replica_set/ssl_test.rb +32 -0
  91. data/test/sharded_cluster/basic_test.rb +197 -0
  92. data/test/shared/authentication/basic_auth_shared.rb +286 -0
  93. data/test/shared/authentication/bulk_api_auth_shared.rb +259 -0
  94. data/test/shared/authentication/gssapi_shared.rb +164 -0
  95. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  96. data/test/shared/ssl_shared.rb +235 -0
  97. data/test/test_helper.rb +56 -0
  98. data/test/threading/basic_test.rb +120 -0
  99. data/test/tools/mongo_config.rb +608 -0
  100. data/test/tools/mongo_config_test.rb +160 -0
  101. data/test/unit/client_test.rb +347 -0
  102. data/test/unit/collection_test.rb +166 -0
  103. data/test/unit/connection_test.rb +325 -0
  104. data/test/unit/cursor_test.rb +299 -0
  105. data/test/unit/db_test.rb +136 -0
  106. data/test/unit/grid_test.rb +76 -0
  107. data/test/unit/mongo_sharded_client_test.rb +48 -0
  108. data/test/unit/node_test.rb +93 -0
  109. data/test/unit/pool_manager_test.rb +142 -0
  110. data/test/unit/read_pref_test.rb +115 -0
  111. data/test/unit/read_test.rb +159 -0
  112. data/test/unit/safe_test.rb +158 -0
  113. data/test/unit/sharding_pool_manager_test.rb +84 -0
  114. data/test/unit/write_concern_test.rb +175 -0
  115. metadata +260 -0
  116. metadata.gz.sig +0 -0
@@ -0,0 +1,48 @@
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 MongoShardedClientUnitTest < Test::Unit::TestCase
18
+ include Mongo
19
+
20
+ def test_initialize_with_single_mongos_uri
21
+ uri = "mongodb://localhost:27017"
22
+ with_preserved_env_uri(uri) do
23
+ client = MongoShardedClient.new(:connect => false)
24
+ assert_equal [[ "localhost", 27017 ]], client.seeds
25
+ end
26
+ end
27
+
28
+ def test_initialize_with_multiple_mongos_uris
29
+ uri = "mongodb://localhost:27017,localhost:27018"
30
+ with_preserved_env_uri(uri) do
31
+ client = MongoShardedClient.new(:connect => false)
32
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
33
+ end
34
+ end
35
+
36
+ def test_from_uri_with_string
37
+ client = MongoShardedClient.from_uri("mongodb://localhost:27017,localhost:27018", :connect => false)
38
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
39
+ end
40
+
41
+ def test_from_uri_with_env_variable
42
+ uri = "mongodb://localhost:27017,localhost:27018"
43
+ with_preserved_env_uri(uri) do
44
+ client = MongoShardedClient.from_uri(nil, :connect => false)
45
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,93 @@
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 NodeUnitTest < Test::Unit::TestCase
18
+
19
+ def setup
20
+ @client = stub()
21
+ manager = mock('pool_manager')
22
+ manager.stubs(:update_max_sizes)
23
+ @client.stubs(:local_manager).returns(manager)
24
+ end
25
+
26
+ should "refuse to connect to node without 'hosts' key" do
27
+ tcp = mock()
28
+ node = Node.new(@client, ['localhost', 27017])
29
+ tcp.stubs(:new).returns(new_mock_socket)
30
+ @client.stubs(:socket_class).returns(tcp)
31
+
32
+ admin_db = new_mock_db
33
+ admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1})
34
+ @client.stubs(:[]).with('admin').returns(admin_db)
35
+ @client.stubs(:op_timeout).returns(nil)
36
+ @client.stubs(:connect_timeout).returns(nil)
37
+ @client.expects(:log)
38
+ @client.expects(:mongos?).returns(false)
39
+ @client.stubs(:socket_opts)
40
+
41
+ assert node.connect
42
+ node.config
43
+ end
44
+
45
+ should "load a node from an array" do
46
+ node = Node.new(@client, ['power.level.com', 9001])
47
+ assert_equal 'power.level.com', node.host
48
+ assert_equal 9001, node.port
49
+ assert_equal 'power.level.com:9001', node.address
50
+ end
51
+
52
+ should "should default the port for an array" do
53
+ node = Node.new(@client, ['power.level.com'])
54
+ assert_equal 'power.level.com', node.host
55
+ assert_equal MongoClient::DEFAULT_PORT, node.port
56
+ assert_equal "power.level.com:#{MongoClient::DEFAULT_PORT}", node.address
57
+ end
58
+
59
+ should "load a node from a string" do
60
+ node = Node.new(@client, 'localhost:1234')
61
+ assert_equal 'localhost', node.host
62
+ assert_equal 1234, node.port
63
+ assert_equal 'localhost:1234', node.address
64
+ end
65
+
66
+ should "should default the port for a string" do
67
+ node = Node.new(@client, '192.168.0.1')
68
+ assert_equal '192.168.0.1', node.host
69
+ assert_equal MongoClient::DEFAULT_PORT, node.port
70
+ assert_equal "192.168.0.1:#{MongoClient::DEFAULT_PORT}", node.address
71
+ end
72
+
73
+ should "two nodes with the same address should be equal" do
74
+ assert_equal Node.new(@client, '192.168.0.1'),
75
+ Node.new(@client, ['192.168.0.1', MongoClient::DEFAULT_PORT])
76
+ end
77
+
78
+ should "two nodes with the same address should have the same hash" do
79
+ assert_equal Node.new(@client, '192.168.0.1').hash,
80
+ Node.new(@client, ['192.168.0.1', MongoClient::DEFAULT_PORT]).hash
81
+ end
82
+
83
+ should "two nodes with different addresses should not be equal" do
84
+ assert_not_equal Node.new(@client, '192.168.0.2'),
85
+ Node.new(@client, ['192.168.0.1', MongoClient::DEFAULT_PORT])
86
+ end
87
+
88
+ should "two nodes with the same address should have the same hash negate" do
89
+ assert_not_equal Node.new(@client, '192.168.0.1').hash,
90
+ Node.new(@client, '1239.33.4.2393:29949').hash
91
+ end
92
+
93
+ end
@@ -0,0 +1,142 @@
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
+ include Mongo
17
+
18
+ class PoolManagerUnitTest < Test::Unit::TestCase
19
+
20
+ context "Initialization: " do
21
+
22
+ setup do
23
+ TCPSocket.stubs(:new).returns(new_mock_socket)
24
+ @db = new_mock_db
25
+
26
+ @client = stub("MongoClient")
27
+ @client.stubs(:connect_timeout).returns(5)
28
+ @client.stubs(:op_timeout).returns(5)
29
+ @client.stubs(:pool_size).returns(2)
30
+ @client.stubs(:pool_timeout).returns(100)
31
+ @client.stubs(:seeds).returns(['localhost:30000'])
32
+ @client.stubs(:socket_class).returns(TCPSocket)
33
+ @client.stubs(:mongos?).returns(false)
34
+ @client.stubs(:[]).returns(@db)
35
+ @client.stubs(:socket_opts)
36
+
37
+ @client.stubs(:replica_set_name).returns(nil)
38
+ @client.stubs(:log)
39
+ @arbiters = ['localhost:27020']
40
+ @hosts = [
41
+ 'localhost:27017',
42
+ 'localhost:27018',
43
+ 'localhost:27019',
44
+ 'localhost:27020'
45
+ ]
46
+
47
+ @ismaster = {
48
+ 'hosts' => @hosts,
49
+ 'arbiters' => @arbiters,
50
+ 'maxBsonObjectSize' => 1024,
51
+ 'maxMessageSizeBytes' => 1024 * 2.5,
52
+ 'maxWireVersion' => 1,
53
+ 'minWireVersion' => 0
54
+ }
55
+ end
56
+
57
+ should "populate pools correctly" do
58
+
59
+ @db.stubs(:command).returns(
60
+ # First call to get a socket.
61
+ @ismaster.merge({'ismaster' => true}),
62
+
63
+ # Subsequent calls to configure pools.
64
+ @ismaster.merge({'ismaster' => true}),
65
+ @ismaster.merge({'secondary' => true, 'maxBsonObjectSize' => 500}),
66
+ @ismaster.merge({'secondary' => true, 'maxMessageSizeBytes' => 700}),
67
+ @ismaster.merge({'arbiterOnly' => true})
68
+ )
69
+
70
+ seeds = [['localhost', 27017]]
71
+ manager = Mongo::PoolManager.new(@client, seeds)
72
+ @client.stubs(:local_manager).returns(manager)
73
+ manager.connect
74
+
75
+ assert_equal ['localhost', 27017], manager.primary
76
+ assert_equal 27017, manager.primary_pool.port
77
+ assert_equal 2, manager.secondaries.length
78
+ assert_equal [27018, 27019], manager.secondary_pools.map(&:port).sort
79
+ assert_equal [['localhost', 27020]], manager.arbiters
80
+ assert_equal 500, manager.max_bson_size
81
+ assert_equal 700, manager.max_message_size
82
+ end
83
+
84
+ should "populate pools with single unqueryable seed" do
85
+
86
+ @db.stubs(:command).returns(
87
+ # First call to recovering node
88
+ @ismaster.merge({'ismaster' => false, 'secondary' => false}),
89
+
90
+ # Subsequent calls to configure pools.
91
+ @ismaster.merge({'ismaster' => false, 'secondary' => false}),
92
+ @ismaster.merge({'ismaster' => true}),
93
+ @ismaster.merge({'secondary' => true}),
94
+ @ismaster.merge({'arbiterOnly' => true})
95
+ )
96
+
97
+ seeds = [['localhost', 27017]]
98
+ manager = PoolManager.new(@client, seeds)
99
+ @client.stubs(:local_manager).returns(manager)
100
+ manager.connect
101
+
102
+ assert_equal ['localhost', 27018], manager.primary
103
+ assert_equal 27018, manager.primary_pool.port
104
+ assert_equal 1, manager.secondaries.length
105
+ assert_equal 27019, manager.secondary_pools[0].port
106
+ assert_equal [['localhost', 27020]], manager.arbiters
107
+ end
108
+
109
+ should "return clones of pool lists" do
110
+
111
+ @db.stubs(:command).returns(
112
+ # First call to get a socket.
113
+ @ismaster.merge({'ismaster' => true}),
114
+
115
+ # Subsequent calls to configure pools.
116
+ @ismaster.merge({'ismaster' => true}),
117
+ @ismaster.merge({'secondary' => true, 'maxBsonObjectSize' => 500}),
118
+ @ismaster.merge({'secondary' => true, 'maxMessageSizeBytes' => 700}),
119
+ @ismaster.merge({'arbiterOnly' => true})
120
+ )
121
+
122
+ seeds = [['localhost', 27017], ['localhost', 27018]]
123
+ manager = Mongo::PoolManager.new(@client, seeds)
124
+ @client.stubs(:local_manager).returns(manager)
125
+ manager.connect
126
+
127
+ assert_not_equal manager.instance_variable_get(:@arbiters).object_id, manager.arbiters.object_id
128
+ assert_not_equal manager.instance_variable_get(:@secondaries).object_id, manager.secondaries.object_id
129
+ assert_not_equal manager.instance_variable_get(:@secondary_pools).object_id, manager.secondary_pools.object_id
130
+ assert_not_equal manager.instance_variable_get(:@hosts).object_id, manager.hosts.object_id
131
+ assert_not_equal manager.instance_variable_get(:@pools).object_id, manager.pools.object_id
132
+
133
+ assert_not_equal manager.instance_variable_get(:@arbiters).object_id, manager.state_snapshot[:arbiters].object_id
134
+ assert_not_equal manager.instance_variable_get(:@secondaries).object_id, manager.state_snapshot[:secondaries].object_id
135
+ assert_not_equal manager.instance_variable_get(:@secondary_pools).object_id, manager.state_snapshot[:secondary_pools].object_id
136
+ assert_not_equal manager.instance_variable_get(:@hosts).object_id, manager.state_snapshot[:hosts].object_id
137
+ assert_not_equal manager.instance_variable_get(:@pools).object_id, manager.state_snapshot[:pools].object_id
138
+ end
139
+
140
+ end
141
+
142
+ end
@@ -0,0 +1,115 @@
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 ReadPreferenceUnitTest < Test::Unit::TestCase
18
+
19
+ include ReadPreference
20
+
21
+ def setup
22
+ mock_pool = mock()
23
+ mock_pool.stubs(:ping_time).returns(Pool::MAX_PING_TIME)
24
+
25
+ stubs(:primary_pool).returns(mock_pool)
26
+ stubs(:secondary_pools).returns([mock_pool])
27
+ stubs(:pools).returns([mock_pool])
28
+ end
29
+
30
+ def test_select_pool
31
+ ReadPreference::READ_PREFERENCES.map do |rp|
32
+ assert select_pool({:mode => rp, :tags => [], :latency => 15})
33
+ end
34
+ end
35
+
36
+ def test_sok_mapreduce_out_string_returns_false
37
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
38
+ 'out', 'new-test-collection']
39
+ assert_equal false, ReadPreference::secondary_ok?(command)
40
+ end
41
+
42
+ def test_sok_mapreduce_replace_collection_returns_false
43
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
44
+ 'out', BSON::OrderedHash['replace', 'new-test-collection']]
45
+ assert_equal false, ReadPreference::secondary_ok?(command)
46
+ end
47
+
48
+ def test_sok_mapreduce_inline_collection_returns_false
49
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
50
+ 'out', 'inline']
51
+ assert_equal false, ReadPreference::secondary_ok?(command)
52
+ end
53
+
54
+ def test_sok_inline_symbol_mapreduce_returns_true
55
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
56
+ 'out', BSON::OrderedHash[:inline, 'true']]
57
+ assert_equal true, ReadPreference::secondary_ok?(command)
58
+ end
59
+
60
+ def test_sok_inline_string_mapreduce_returns_true
61
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
62
+ 'out', BSON::OrderedHash['inline', 'true']]
63
+ assert_equal true, ReadPreference::secondary_ok?(command)
64
+ end
65
+
66
+ def test_sok_count_true
67
+ command = BSON::OrderedHash['count', 'test-collection',
68
+ 'query', BSON::OrderedHash['a', 'b']]
69
+ assert_equal true, ReadPreference::secondary_ok?(command)
70
+ end
71
+
72
+ def test_sok_server_status_returns_false
73
+ command = BSON::OrderedHash['serverStatus', 1]
74
+ assert_equal false, ReadPreference::secondary_ok?(command)
75
+ end
76
+
77
+ def test_cmd_reroute_with_secondary
78
+ ReadPreference::expects(:warn).with(regexp_matches(/rerouted to primary/))
79
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
80
+ 'out', 'new-test-collection']
81
+ assert_equal :primary, ReadPreference::cmd_read_pref(:secondary, command)
82
+ end
83
+
84
+ def test_find_and_modify_reroute_with_secondary
85
+ ReadPreference::expects(:warn).with(regexp_matches(/rerouted to primary/))
86
+ command = BSON::OrderedHash['findAndModify', 'test-collection',
87
+ 'query', {}]
88
+ assert_equal :primary, ReadPreference::cmd_read_pref(:secondary, command)
89
+ end
90
+
91
+ def test_cmd_no_reroute_with_secondary
92
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
93
+ 'out', BSON::OrderedHash['inline', 'true']]
94
+ assert_equal :secondary, ReadPreference::cmd_read_pref(:secondary, command)
95
+ end
96
+
97
+ def test_cmd_no_reroute_with_primary
98
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
99
+ 'out', 'new-test-collection']
100
+ assert_equal :primary, ReadPreference::cmd_read_pref(:primary, command)
101
+ end
102
+
103
+ def test_cmd_no_reroute_with_primary_secondary_ok
104
+ command = BSON::OrderedHash['mapreduce', 'test-collection',
105
+ 'out', BSON::OrderedHash['inline', 'true']]
106
+ assert_equal :primary, ReadPreference::cmd_read_pref(:primary, command)
107
+ end
108
+
109
+ def test_parallel_scan_secondary_ok
110
+ command = BSON::OrderedHash['parallelCollectionScan', 'test-collection',
111
+ 'numCursors', 3]
112
+ assert_equal true, ReadPreference::secondary_ok?(command)
113
+ end
114
+
115
+ end
@@ -0,0 +1,159 @@
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 ReadUnitTest < Test::Unit::TestCase
18
+
19
+ context "Read mode on standard connection: " do
20
+ setup do
21
+ @read = :secondary
22
+ @client = MongoClient.new('localhost', 27017, :read => @read, :connect => false)
23
+ end
24
+
25
+ end
26
+
27
+ context "Read preferences on replica set connection: " do
28
+ setup do
29
+ @read = :secondary_preferred
30
+ @acceptable_latency = 100
31
+ @tags = {"dc" => "Tyler", "rack" => "Brock"}
32
+ @bad_tags = {"wow" => "cool"}
33
+ @client = MongoReplicaSetClient.new(
34
+ ['localhost:27017'],
35
+ :read => @read,
36
+ :tag_sets => @tags,
37
+ :secondary_acceptable_latency_ms => @acceptable_latency,
38
+ :connect => false
39
+ )
40
+ end
41
+
42
+ should "store read preference on MongoClient" do
43
+ assert_equal @read, @client.read
44
+ assert_equal @tags, @client.tag_sets
45
+ assert_equal @acceptable_latency, @client.acceptable_latency
46
+ end
47
+
48
+ should "propogate to DB" do
49
+ db = @client[TEST_DB]
50
+ assert_equal @read, db.read
51
+ assert_equal @tags, db.tag_sets
52
+ assert_equal @acceptable_latency, db.acceptable_latency
53
+
54
+ db = @client.db(TEST_DB)
55
+ assert_equal @read, db.read
56
+ assert_equal @tags, db.tag_sets
57
+ assert_equal @acceptable_latency, db.acceptable_latency
58
+
59
+ db = DB.new(TEST_DB, @client)
60
+ assert_equal @read, db.read
61
+ assert_equal @tags, db.tag_sets
62
+ assert_equal @acceptable_latency, db.acceptable_latency
63
+ end
64
+
65
+ should "allow db override" do
66
+ db = DB.new(TEST_DB, @client, :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
67
+ assert_equal :primary, db.read
68
+ assert_equal @bad_tags, db.tag_sets
69
+ assert_equal 25, db.acceptable_latency
70
+
71
+ db = @client.db(TEST_DB, :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
72
+ assert_equal :primary, db.read
73
+ assert_equal @bad_tags, db.tag_sets
74
+ assert_equal 25, db.acceptable_latency
75
+ end
76
+
77
+ context "on DB: " do
78
+ setup do
79
+ @db = @client[TEST_DB]
80
+ end
81
+
82
+ should "propogate to collection" do
83
+ col = @db.collection('read-unit-test')
84
+ assert_equal @read, col.read
85
+ assert_equal @tags, col.tag_sets
86
+ assert_equal @acceptable_latency, col.acceptable_latency
87
+
88
+ col = @db['read-unit-test']
89
+ assert_equal @read, col.read
90
+ assert_equal @tags, col.tag_sets
91
+ assert_equal @acceptable_latency, col.acceptable_latency
92
+
93
+ col = Collection.new('read-unit-test', @db)
94
+ assert_equal @read, col.read
95
+ assert_equal @tags, col.tag_sets
96
+ assert_equal @acceptable_latency, col.acceptable_latency
97
+ end
98
+
99
+ should "allow override on collection" do
100
+ col = @db.collection('read-unit-test', :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
101
+ assert_equal :primary, col.read
102
+ assert_equal @bad_tags, col.tag_sets
103
+ assert_equal 25, col.acceptable_latency
104
+
105
+ col = Collection.new('read-unit-test', @db, :read => :primary, :tag_sets => @bad_tags, :acceptable_latency => 25)
106
+ assert_equal :primary, col.read
107
+ assert_equal @bad_tags, col.tag_sets
108
+ assert_equal 25, col.acceptable_latency
109
+ end
110
+ end
111
+
112
+ context "on read mode ops" do
113
+ setup do
114
+ @col = @client[TEST_DB]['read-unit-test']
115
+ @mock_socket = new_mock_socket
116
+ end
117
+
118
+ should "use default value on query" do
119
+ @cursor = @col.find({:a => 1})
120
+ sock = new_mock_socket
121
+ read_pool = stub(:checkin => true)
122
+ @client.stubs(:read_pool).returns(read_pool)
123
+ local_manager = PoolManager.new(@client, @client.seeds)
124
+ @client.stubs(:local_manager).returns(local_manager)
125
+ primary_pool = stub(:checkin => true)
126
+ sock.stubs(:pool).returns(primary_pool)
127
+ @client.stubs(:primary_pool).returns(primary_pool)
128
+ @client.expects(:checkout_reader).returns(sock)
129
+ @client.expects(:receive_message).with do |o, m, l, s, c, r|
130
+ r == nil
131
+ end.returns([[], 0, 0])
132
+
133
+ @cursor.next
134
+ end
135
+
136
+ should "allow override default value on query" do
137
+ @cursor = @col.find({:a => 1}, :read => :primary)
138
+ sock = new_mock_socket
139
+ local_manager = PoolManager.new(@client, @client.seeds)
140
+ @client.stubs(:local_manager).returns(local_manager)
141
+ primary_pool = stub(:checkin => true)
142
+ sock.stubs(:pool).returns(primary_pool)
143
+ @client.stubs(:primary_pool).returns(primary_pool)
144
+ @client.expects(:checkout_reader).returns(sock)
145
+ @client.expects(:receive_message).with do |o, m, l, s, c, r|
146
+ r == nil
147
+ end.returns([[], 0, 0])
148
+
149
+ @cursor.next
150
+ end
151
+
152
+ should "allow override alternate value on query" do
153
+ assert_raise MongoArgumentError do
154
+ @col.find_one({:a => 1}, :read => {:dc => "ny"})
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end