1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
Description: add full test_helper from MondoDB github repo
Author: Cédric Boutillier <boutil@debian.org>
Bug: https://jira.mongodb.org/browse/RUBY-570
Last-Updated: 2013-03-20
--- /dev/null
+++ b/test/test_helper.rb
@@ -0,0 +1,204 @@
+require 'rubygems'
+# SimpleCov must load before our code - A coverage report summary line will print after each test suite
+if RUBY_VERSION >= '1.9.0' && RUBY_ENGINE == 'ruby'
+ if ENV.key?('COVERAGE')
+ require 'simplecov'
+ SimpleCov.start do
+ add_group "Mongo", 'lib/mongo'
+ add_group "BSON", 'lib/bson'
+ add_filter "/test/"
+ merge_timeout 3600
+ command_name ENV['SIMPLECOV_COMMAND_NAME'] if ENV.has_key?('SIMPLECOV_COMMAND_NAME')
+ end
+ end
+end
+gem 'test-unit' # Do NOT remove this line - gem version is needed for Test::Unit::TestCase.shutdown
+require 'test/unit'
+require 'tools/mongo_config'
+
+class Test::Unit::TestCase
+
+ TEST_DATA = File.join(File.dirname(__FILE__), 'data')
+
+ def ensure_cluster(kind=nil, opts={})
+ @@cluster ||= nil
+
+ unless @@cluster
+ if kind == :rs
+ cluster_opts = Mongo::Config::DEFAULT_REPLICA_SET.dup
+ else
+ cluster_opts = Mongo::Config::DEFAULT_SHARDED_SIMPLE.dup
+ end
+
+ cluster_opts.merge!(opts)
+
+ dbpath = ENV['DBPATH'] || 'data'
+ cluster_opts.merge!(:dbpath => dbpath)
+
+ #debug 1, opts
+ config = Mongo::Config.cluster(cluster_opts)
+ #debug 1, config
+ @@cluster = Mongo::Config::ClusterManager.new(config)
+
+ Test::Unit::TestCase.class_eval do
+ @@force_shutdown = false
+
+ def self.shutdown
+ if @@force_shutdown || /rake_test_loader/ !~ $0
+ @@cluster.stop
+ @@cluster.clobber
+ end
+ end
+ end
+ end
+
+ @@cluster.start
+ instance_variable_set("@#{kind}", @@cluster)
+ end
+
+ # Generic code for rescuing connection failures and retrying operations.
+ # This could be combined with some timeout functionality.
+ def rescue_connection_failure(max_retries=30)
+ retries = 0
+ begin
+ yield
+ rescue Mongo::ConnectionFailure => ex
+ #puts "Rescue attempt #{retries}: from #{ex}"
+ retries += 1
+ raise ex if retries > max_retries
+ sleep(2)
+ retry
+ end
+ end
+end
+
+def silently
+ warn_level = $VERBOSE
+ $VERBOSE = nil
+ begin
+ result = yield
+ ensure
+ $VERBOSE = warn_level
+ end
+ result
+end
+
+begin
+ silently { require 'shoulda' }
+ silently { require 'mocha/setup' }
+rescue LoadError
+ puts <<MSG
+
+This test suite requires shoulda and mocha.
+You can install them as follows:
+ gem install shoulda
+ gem install mocha
+
+MSG
+ exit
+end
+
+unless defined? MONGO_TEST_DB
+ MONGO_TEST_DB = 'ruby-test-db'
+end
+
+unless defined? TEST_PORT
+ TEST_PORT = ENV['MONGO_RUBY_DRIVER_PORT'] ? ENV['MONGO_RUBY_DRIVER_PORT'].to_i : Mongo::MongoClient::DEFAULT_PORT
+end
+
+unless defined? TEST_HOST
+ TEST_HOST = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
+end
+
+class Test::Unit::TestCase
+ include Mongo
+ include BSON
+
+ def self.standard_connection(options={}, legacy=false)
+ if legacy
+ Connection.new(TEST_HOST, TEST_PORT, options)
+ else
+ MongoClient.new(TEST_HOST, TEST_PORT, options)
+ end
+ end
+
+ def standard_connection(options={}, legacy=false)
+ self.class.standard_connection(options, legacy)
+ end
+
+ def self.host_port
+ "#{mongo_host}:#{mongo_port}"
+ end
+
+ def self.mongo_host
+ TEST_HOST
+ end
+
+ def self.mongo_port
+ TEST_PORT
+ end
+
+ def host_port
+ self.class.host_port
+ end
+
+ def mongo_host
+ self.class.mongo_host
+ end
+
+ def mongo_port
+ self.class.mongo_port
+ end
+
+ def method_name
+ caller[0]=~/`(.*?)'/
+ $1
+ end
+
+ def step_down_command
+ # Adding force=true to avoid 'no secondaries within 10 seconds of my optime' errors
+ step_down_command = BSON::OrderedHash.new
+ step_down_command[:replSetStepDown] = 5
+ step_down_command[:force] = true
+ step_down_command
+ end
+
+ def new_mock_socket(host='localhost', port=27017)
+ socket = Object.new
+ socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
+ socket.stubs(:close)
+ socket.stubs(:closed?)
+ socket.stubs(:checkin)
+ socket.stubs(:pool)
+ socket
+ end
+
+ def new_mock_unix_socket(sockfile='/tmp/mongod.sock')
+ socket = Object.new
+ socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP)
+ socket.stubs(:close)
+ socket.stubs(:closed?)
+ socket
+ end
+
+ def new_mock_db
+ Object.new
+ end
+
+ def assert_raise_error(klass, message=nil)
+ begin
+ yield
+ rescue => e
+ if klass.to_s != e.class.to_s
+ flunk "Expected exception class #{klass} but got #{e.class}.\n #{e.backtrace}"
+ end
+
+ if message && !e.message.include?(message)
+ p e.backtrace
+ flunk "#{e.message} does not include #{message}.\n#{e.backtrace}"
+ end
+ else
+ flunk "Expected assertion #{klass} but none was raised."
+ end
+ end
+end
|