jonbell-mongo 1.3.1.2

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 (88) hide show
  1. data/LICENSE.txt +190 -0
  2. data/README.md +333 -0
  3. data/Rakefile +215 -0
  4. data/bin/mongo_console +21 -0
  5. data/docs/CREDITS.md +123 -0
  6. data/docs/FAQ.md +116 -0
  7. data/docs/GridFS.md +158 -0
  8. data/docs/HISTORY.md +263 -0
  9. data/docs/RELEASES.md +33 -0
  10. data/docs/REPLICA_SETS.md +72 -0
  11. data/docs/TUTORIAL.md +247 -0
  12. data/docs/WRITE_CONCERN.md +28 -0
  13. data/lib/mongo.rb +97 -0
  14. data/lib/mongo/collection.rb +895 -0
  15. data/lib/mongo/connection.rb +926 -0
  16. data/lib/mongo/cursor.rb +474 -0
  17. data/lib/mongo/db.rb +617 -0
  18. data/lib/mongo/exceptions.rb +71 -0
  19. data/lib/mongo/gridfs/grid.rb +107 -0
  20. data/lib/mongo/gridfs/grid_ext.rb +57 -0
  21. data/lib/mongo/gridfs/grid_file_system.rb +146 -0
  22. data/lib/mongo/gridfs/grid_io.rb +485 -0
  23. data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
  24. data/lib/mongo/repl_set_connection.rb +356 -0
  25. data/lib/mongo/util/conversions.rb +89 -0
  26. data/lib/mongo/util/core_ext.rb +60 -0
  27. data/lib/mongo/util/pool.rb +177 -0
  28. data/lib/mongo/util/server_version.rb +71 -0
  29. data/lib/mongo/util/support.rb +82 -0
  30. data/lib/mongo/util/uri_parser.rb +185 -0
  31. data/mongo.gemspec +34 -0
  32. data/test/auxillary/1.4_features.rb +166 -0
  33. data/test/auxillary/authentication_test.rb +68 -0
  34. data/test/auxillary/autoreconnect_test.rb +41 -0
  35. data/test/auxillary/fork_test.rb +30 -0
  36. data/test/auxillary/repl_set_auth_test.rb +58 -0
  37. data/test/auxillary/slave_connection_test.rb +36 -0
  38. data/test/auxillary/threaded_authentication_test.rb +101 -0
  39. data/test/bson/binary_test.rb +15 -0
  40. data/test/bson/bson_test.rb +654 -0
  41. data/test/bson/byte_buffer_test.rb +208 -0
  42. data/test/bson/hash_with_indifferent_access_test.rb +38 -0
  43. data/test/bson/json_test.rb +17 -0
  44. data/test/bson/object_id_test.rb +154 -0
  45. data/test/bson/ordered_hash_test.rb +210 -0
  46. data/test/bson/timestamp_test.rb +24 -0
  47. data/test/collection_test.rb +910 -0
  48. data/test/connection_test.rb +324 -0
  49. data/test/conversions_test.rb +119 -0
  50. data/test/cursor_fail_test.rb +75 -0
  51. data/test/cursor_message_test.rb +43 -0
  52. data/test/cursor_test.rb +483 -0
  53. data/test/db_api_test.rb +738 -0
  54. data/test/db_connection_test.rb +15 -0
  55. data/test/db_test.rb +315 -0
  56. data/test/grid_file_system_test.rb +259 -0
  57. data/test/grid_io_test.rb +209 -0
  58. data/test/grid_test.rb +258 -0
  59. data/test/load/thin/load.rb +24 -0
  60. data/test/load/unicorn/load.rb +23 -0
  61. data/test/replica_sets/connect_test.rb +112 -0
  62. data/test/replica_sets/connection_string_test.rb +32 -0
  63. data/test/replica_sets/count_test.rb +35 -0
  64. data/test/replica_sets/insert_test.rb +53 -0
  65. data/test/replica_sets/pooled_insert_test.rb +55 -0
  66. data/test/replica_sets/query_secondaries.rb +108 -0
  67. data/test/replica_sets/query_test.rb +51 -0
  68. data/test/replica_sets/replication_ack_test.rb +66 -0
  69. data/test/replica_sets/rs_test_helper.rb +27 -0
  70. data/test/safe_test.rb +68 -0
  71. data/test/support/hash_with_indifferent_access.rb +186 -0
  72. data/test/support/keys.rb +45 -0
  73. data/test/support_test.rb +18 -0
  74. data/test/test_helper.rb +102 -0
  75. data/test/threading/threading_with_large_pool_test.rb +90 -0
  76. data/test/threading_test.rb +87 -0
  77. data/test/tools/auth_repl_set_manager.rb +14 -0
  78. data/test/tools/repl_set_manager.rb +266 -0
  79. data/test/unit/collection_test.rb +130 -0
  80. data/test/unit/connection_test.rb +85 -0
  81. data/test/unit/cursor_test.rb +109 -0
  82. data/test/unit/db_test.rb +94 -0
  83. data/test/unit/grid_test.rb +49 -0
  84. data/test/unit/pool_test.rb +9 -0
  85. data/test/unit/repl_set_connection_test.rb +59 -0
  86. data/test/unit/safe_test.rb +125 -0
  87. data/test/uri_test.rb +91 -0
  88. metadata +224 -0
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'mongo')
2
+ require 'logger'
3
+
4
+ $con = Mongo::Connection.new
5
+ $db = $con['foo']
6
+
7
+ class Load < Sinatra::Base
8
+
9
+ configure do
10
+ LOGGER = Logger.new("sinatra.log")
11
+ enable :logging, :dump_errors
12
+ set :raise_errors, true
13
+ end
14
+
15
+ get '/' do
16
+ 3.times do |n|
17
+ if (v=$db.eval("1 + #{n}")) != 1 + n
18
+ STDERR << "#{1 + n} expected but got #{v}"
19
+ raise StandardError, "#{1 + n} expected but got #{v}"
20
+ end
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'mongo')
2
+
3
+ $con = Mongo::Connection.new
4
+ $db = $con['foo']
5
+
6
+ class Load < Sinatra::Base
7
+
8
+ configure do
9
+ LOGGER = Logger.new("sinatra.log")
10
+ enable :logging, :dump_errors
11
+ set :raise_errors, true
12
+ end
13
+
14
+ get '/' do
15
+ 3.times do |n|
16
+ if (v=$db.eval("1 + #{n}")) != 1 + n
17
+ STDERR << "#{1 + n} expected but got #{v}"
18
+ raise StandardError, "#{1 + n} expected but got #{v}"
19
+ end
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,112 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running on RS.host,
5
+ # on ports TEST_PORT, RS.ports[1], and TEST + 2.
6
+ class ConnectTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ RS.restart_killed_nodes
11
+ end
12
+
13
+ def teardown
14
+ RS.restart_killed_nodes
15
+ end
16
+
17
+ def test_connect_with_deprecated_multi
18
+ @conn = Connection.multi([[RS.host, RS.ports[0]], [RS.host, RS.ports[1]]], :name => RS.name)
19
+ assert @conn.is_a?(ReplSetConnection)
20
+ assert @conn.connected?
21
+ end
22
+
23
+ def test_connect_bad_name
24
+ assert_raise_error(ReplicaSetConnectionError, "-wrong") do
25
+ ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
26
+ [RS.host, RS.ports[2]], :rs_name => RS.name + "-wrong")
27
+ end
28
+ end
29
+
30
+ def test_connect_timeout
31
+ passed = false
32
+ timeout = 3
33
+ begin
34
+ t0 = Time.now
35
+ ReplSetConnection.new(['192.169.169.1', 27017], :connect_timeout => timeout)
36
+ rescue OperationTimeout
37
+ passed = true
38
+ t1 = Time.now
39
+ end
40
+
41
+ assert passed
42
+ assert t1 - t0 < timeout + 1
43
+ end
44
+
45
+ def test_connect
46
+ @conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
47
+ [RS.host, RS.ports[2]], :name => RS.name)
48
+ assert @conn.connected?
49
+ assert @conn.read_primary?
50
+ assert @conn.primary?
51
+
52
+ assert_equal RS.primary, @conn.primary
53
+ assert_equal RS.secondaries.sort, @conn.secondaries.sort
54
+ assert_equal RS.arbiters.sort, @conn.arbiters.sort
55
+
56
+ @conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
57
+ :name => RS.name)
58
+ assert @conn.connected?
59
+ end
60
+
61
+ def test_host_port_accessors
62
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
63
+ [RS.host, RS.ports[2]], :name => RS.name)
64
+
65
+ assert_equal @conn.host, RS.primary[0]
66
+ assert_equal @conn.port, RS.primary[1]
67
+ end
68
+
69
+ def test_connect_with_primary_node_killed
70
+ node = RS.kill_primary
71
+
72
+ # Becuase we're killing the primary and trying to connect right away,
73
+ # this is going to fail right away.
74
+ assert_raise_error(ConnectionFailure, "Failed to connect to primary node") do
75
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
76
+ [RS.host, RS.ports[2]])
77
+ end
78
+
79
+ # This allows the secondary to come up as a primary
80
+ rescue_connection_failure do
81
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
82
+ [RS.host, RS.ports[2]])
83
+ end
84
+ end
85
+
86
+ def test_connect_with_secondary_node_killed
87
+ node = RS.kill_secondary
88
+
89
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
90
+ [RS.host, RS.ports[2]])
91
+ assert @conn.connected?
92
+ end
93
+
94
+ def test_connect_with_third_node_killed
95
+ RS.kill(RS.get_node_from_port(RS.ports[2]))
96
+
97
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
98
+ [RS.host, RS.ports[2]])
99
+ assert @conn.connected?
100
+ end
101
+
102
+ def test_connect_with_primary_stepped_down
103
+ RS.step_down_primary
104
+
105
+ rescue_connection_failure do
106
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
107
+ [RS.host, RS.ports[2]])
108
+ end
109
+ assert @conn.connected?
110
+ end
111
+
112
+ end
@@ -0,0 +1,32 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running on RS.host,
5
+ # on ports TEST_PORT, RS.ports[1], and TEST + 2.
6
+ class ConnectionStringTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ RS.restart_killed_nodes
11
+ end
12
+
13
+ def teardown
14
+ RS.restart_killed_nodes
15
+ end
16
+
17
+ def test_connect_with_connection_string
18
+ @conn = Connection.from_uri("mongodb://#{RS.host}:#{RS.ports[0]},#{RS.host}:#{RS.ports[1]}?replicaset=#{RS.name}")
19
+ assert @conn.is_a?(ReplSetConnection)
20
+ assert @conn.connected?
21
+ end
22
+
23
+ def test_connect_with_full_connection_string
24
+ @conn = Connection.from_uri("mongodb://#{RS.host}:#{RS.ports[0]},#{RS.host}:#{RS.ports[1]}?replicaset=#{RS.name};safe=true;w=2;fsync=true;slaveok=true")
25
+ assert @conn.is_a?(ReplSetConnection)
26
+ assert @conn.connected?
27
+ assert_equal 2, @conn.safe[:w]
28
+ assert @conn.safe[:fsync]
29
+ assert @conn.read_pool
30
+ end
31
+
32
+ end
@@ -0,0 +1,35 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running
5
+ # on the local host.
6
+ class ReplicaSetCountTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]], [RS.host, RS.ports[2]])
11
+ @db = @conn.db(MONGO_TEST_DB)
12
+ @db.drop_collection("test-sets")
13
+ @coll = @db.collection("test-sets")
14
+ end
15
+
16
+ def teardown
17
+ RS.restart_killed_nodes
18
+ end
19
+
20
+ def test_correct_count_after_insertion_reconnect
21
+ @coll.insert({:a => 20}, :safe => {:w => 2, :wtimeout => 10000})
22
+ assert_equal 1, @coll.count
23
+
24
+ # Kill the current master node
25
+ @node = RS.kill_primary
26
+
27
+ rescue_connection_failure do
28
+ @coll.insert({:a => 30}, :safe => true)
29
+ end
30
+
31
+ @coll.insert({:a => 40}, :safe => true)
32
+ assert_equal 3, @coll.count, "Second count failed"
33
+ end
34
+
35
+ end
@@ -0,0 +1,53 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running
5
+ # on the local host.
6
+ class ReplicaSetInsertTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ @conn = ReplSetConnection.new([TEST_HOST, RS.ports[0]], [TEST_HOST, RS.ports[1]], [TEST_HOST, RS.ports[2]])
11
+ @db = @conn.db(MONGO_TEST_DB)
12
+ @db.drop_collection("test-sets")
13
+ @coll = @db.collection("test-sets")
14
+ end
15
+
16
+ def teardown
17
+ RS.restart_killed_nodes
18
+ end
19
+
20
+ def test_insert
21
+ @coll.save({:a => 20}, :safe => true)
22
+
23
+ RS.kill_primary
24
+
25
+ rescue_connection_failure do
26
+ @coll.save({:a => 30}, :safe => true)
27
+ end
28
+
29
+ @coll.save({:a => 40}, :safe => true)
30
+ @coll.save({:a => 50}, :safe => true)
31
+ @coll.save({:a => 60}, :safe => true)
32
+ @coll.save({:a => 70}, :safe => true)
33
+
34
+ # Restart the old master and wait for sync
35
+ RS.restart_killed_nodes
36
+ sleep(1)
37
+ results = []
38
+
39
+ rescue_connection_failure do
40
+ @coll.find.each {|r| results << r}
41
+ [20, 30, 40, 50, 60, 70].each do |a|
42
+ assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
43
+ end
44
+ end
45
+
46
+ @coll.save({:a => 80}, :safe => true)
47
+ @coll.find.each {|r| results << r}
48
+ [20, 30, 40, 50, 60, 70, 80].each do |a|
49
+ assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a} on second find"
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,55 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running
5
+ # on the local host.
6
+ class ReplicaSetPooledInsertTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
11
+ [RS.host, RS.ports[2]], :pool_size => 10, :timeout => 5)
12
+ @db = @conn.db(MONGO_TEST_DB)
13
+ @db.drop_collection("test-sets")
14
+ @coll = @db.collection("test-sets")
15
+ end
16
+
17
+ def teardown
18
+ RS.restart_killed_nodes
19
+ end
20
+
21
+ def test_insert
22
+ expected_results = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
23
+ @coll.save({:a => -1}, :safe => true)
24
+
25
+ RS.kill_primary
26
+
27
+ threads = []
28
+ 10.times do |i|
29
+ threads[i] = Thread.new do
30
+ rescue_connection_failure do
31
+ @coll.save({:a => i}, :safe => true)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Restart the old master and wait for sync
37
+ RS.restart_killed_nodes
38
+ sleep(1)
39
+ results = []
40
+
41
+ rescue_connection_failure do
42
+ @coll.find.each {|r| results << r}
43
+ expected_results.each do |a|
44
+ assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
45
+ end
46
+ end
47
+
48
+ @coll.save({:a => 10}, :safe => true)
49
+ @coll.find.each {|r| results << r}
50
+ (expected_results + [10]).each do |a|
51
+ assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a} on second find"
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,108 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require './test/replica_sets/rs_test_helper'
3
+
4
+ # NOTE: This test expects a replica set of three nodes to be running
5
+ # on the local host.
6
+ class ReplicaSetQuerySecondariesTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def setup
10
+ @conn = ReplSetConnection.new([RS.host, RS.ports[0]], :read_secondary => true)
11
+ @db = @conn.db(MONGO_TEST_DB)
12
+ @db.drop_collection("test-sets")
13
+ end
14
+
15
+ def teardown
16
+ RS.restart_killed_nodes
17
+ end
18
+
19
+ def test_read_primary
20
+ rescue_connection_failure do
21
+ assert !@conn.read_primary?
22
+ assert !@conn.primary?
23
+ end
24
+ end
25
+
26
+ def test_con
27
+ assert @conn.primary_pool, "No primary pool!"
28
+ assert @conn.read_pool, "No read pool!"
29
+ assert @conn.primary_pool.port != @conn.read_pool.port,
30
+ "Primary port and read port at the same!"
31
+ end
32
+
33
+ def test_query_secondaries
34
+ @coll = @db.collection("test-sets", :safe => {:w => 3, :wtimeout => 20000})
35
+ @coll.save({:a => 20})
36
+ @coll.save({:a => 30})
37
+ @coll.save({:a => 40})
38
+ results = []
39
+ @coll.find.each {|r| results << r["a"]}
40
+ assert results.include?(20)
41
+ assert results.include?(30)
42
+ assert results.include?(40)
43
+
44
+ RS.kill_primary
45
+
46
+ results = []
47
+ rescue_connection_failure do
48
+ @coll.find.each {|r| results << r}
49
+ [20, 30, 40].each do |a|
50
+ assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
51
+ end
52
+ end
53
+ end
54
+
55
+ def test_kill_primary
56
+ @coll = @db.collection("test-sets", :safe => {:w => 3, :wtimeout => 10000})
57
+ @coll.save({:a => 20})
58
+ @coll.save({:a => 30})
59
+ assert_equal 2, @coll.find.to_a.length
60
+
61
+ # Should still be able to read immediately after killing master node
62
+ RS.kill_primary
63
+ assert_equal 2, @coll.find.to_a.length
64
+ rescue_connection_failure do
65
+ @coll.save({:a => 50}, :safe => {:w => 2, :wtimeout => 10000})
66
+ end
67
+ RS.restart_killed_nodes
68
+ @coll.save({:a => 50}, :safe => {:w => 2, :wtimeout => 10000})
69
+ assert_equal 4, @coll.find.to_a.length
70
+ end
71
+
72
+ def test_kill_secondary
73
+ @coll = @db.collection("test-sets", {:safe => {:w => 3, :wtimeout => 20000}})
74
+ @coll.save({:a => 20})
75
+ @coll.save({:a => 30})
76
+ assert_equal 2, @coll.find.to_a.length
77
+
78
+ read_node = RS.get_node_from_port(@conn.read_pool.port)
79
+ RS.kill(read_node)
80
+
81
+ # Should fail immediately on next read
82
+ old_read_pool_port = @conn.read_pool.port
83
+ assert_raise ConnectionFailure do
84
+ @coll.find.to_a.length
85
+ end
86
+
87
+ # Should eventually reconnect and be able to read
88
+ rescue_connection_failure do
89
+ length = @coll.find.to_a.length
90
+ assert_equal 2, length
91
+ end
92
+ new_read_pool_port = @conn.read_pool.port
93
+ assert old_read_pool_port != new_read_pool_port
94
+ end
95
+
96
+ def test_write_lots_of_data
97
+ @coll = @db.collection("test-sets", {:safe => {:w => 2}})
98
+
99
+ 6000.times do |n|
100
+ @coll.save({:a => n})
101
+ end
102
+
103
+ cursor = @coll.find()
104
+ cursor.next
105
+ cursor.close
106
+ end
107
+
108
+ end