mongoid 7.1.7 → 7.1.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +1 -1
- data/Rakefile +31 -0
- data/lib/config/locales/en.yml +13 -0
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +1 -1
- data/lib/mongoid/association/proxy.rb +1 -1
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +1 -1
- data/lib/mongoid/association/referenced/has_many/proxy.rb +1 -1
- data/lib/mongoid/attributes.rb +8 -1
- data/lib/mongoid/config/environment.rb +9 -1
- data/lib/mongoid/contextual/atomic.rb +7 -2
- data/lib/mongoid/contextual/none.rb +3 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +2 -2
- data/lib/mongoid/criteria/queryable/storable.rb +4 -4
- data/lib/mongoid/criteria.rb +1 -1
- data/lib/mongoid/document.rb +3 -2
- data/lib/mongoid/errors/empty_config_file.rb +26 -0
- data/lib/mongoid/errors/invalid_config_file.rb +26 -0
- data/lib/mongoid/errors/mongoid_error.rb +1 -1
- data/lib/mongoid/errors.rb +2 -0
- data/lib/mongoid/interceptable.rb +1 -1
- data/lib/mongoid/persistence_context.rb +3 -1
- data/lib/mongoid/reloadable.rb +5 -0
- data/lib/mongoid/tasks/database.rb +1 -1
- data/lib/mongoid/validatable/associated.rb +1 -1
- data/lib/mongoid/validatable/presence.rb +3 -3
- data/lib/mongoid/validatable/uniqueness.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid.rb +1 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +8 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +1 -1
- data/spec/app/models/address.rb +4 -0
- data/spec/app/models/mop.rb +26 -0
- data/spec/app/models/person.rb +9 -0
- data/spec/integration/app_spec.rb +144 -87
- data/spec/integration/contextual/empty_spec.rb +142 -0
- data/spec/integration/document_spec.rb +21 -0
- data/spec/lite_spec_helper.rb +5 -5
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +17 -4
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +17 -0
- data/spec/mongoid/attributes_spec.rb +241 -0
- data/spec/mongoid/clients/factory_spec.rb +11 -0
- data/spec/mongoid/clients/options_spec.rb +11 -3
- data/spec/mongoid/config/environment_spec.rb +86 -8
- data/spec/mongoid/contextual/atomic_spec.rb +81 -29
- data/spec/mongoid/contextual/geo_near_spec.rb +1 -1
- data/spec/mongoid/criteria_spec.rb +4 -0
- data/spec/mongoid/document_query_spec.rb +51 -0
- data/spec/mongoid/document_spec.rb +21 -1
- data/spec/mongoid/errors/invalid_config_file_spec.rb +32 -0
- data/spec/mongoid/errors/mongoid_error_spec.rb +20 -8
- data/spec/mongoid/factory_spec.rb +2 -2
- data/spec/mongoid/persistable/savable_spec.rb +4 -4
- data/spec/mongoid/persistable/settable_spec.rb +30 -0
- data/spec/mongoid/persistable/updatable_spec.rb +2 -0
- data/spec/mongoid/persistable_spec.rb +2 -2
- data/spec/shared/bin/get-mongodb-download-url +17 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/cluster_config.rb +19 -4
- data/spec/shared/lib/mrss/constraints.rb +67 -12
- data/spec/shared/lib/mrss/docker_runner.rb +10 -1
- data/spec/shared/lib/mrss/event_subscriber.rb +200 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +16 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +84 -33
- data/spec/shared/lib/mrss/spec_organizer.rb +32 -2
- data/spec/shared/lib/mrss/utils.rb +15 -0
- data/spec/shared/share/Dockerfile.erb +126 -32
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/server.sh +123 -26
- data/spec/shared/shlib/set_env.sh +4 -1
- data/spec/spec_helper.rb +3 -1
- data/spec/support/constraints.rb +0 -226
- data/spec/support/spec_config.rb +8 -0
- data.tar.gz.sig +0 -0
- metadata +555 -503
- metadata.gz.sig +0 -0
- data/spec/support/child_process_helper.rb +0 -76
- data/spec/support/lite_constraints.rb +0 -22
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
desired_version, arch = ARGV
|
4
|
+
if arch.nil?
|
5
|
+
STDERR.puts "Usage: get-mongodb-download-url desired-version arch"
|
6
|
+
exit 1
|
7
|
+
end
|
8
|
+
|
9
|
+
$: << File.join(File.dirname(__FILE__), '../lib')
|
10
|
+
require 'mrss/server_version_registry'
|
11
|
+
|
12
|
+
begin
|
13
|
+
puts Mrss::ServerVersionRegistry.new(desired_version, arch).download_url
|
14
|
+
rescue Mrss::ServerVersionRegistry::Error => exc
|
15
|
+
STDERR.puts "Error: #{exc}"
|
16
|
+
exit 2
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'aws-sdk-s3'
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: s3-copy options"
|
9
|
+
|
10
|
+
opts.on("-r", "--region=REGION", "AWS region to use (default us-east-1)") do |v|
|
11
|
+
options[:region] = v
|
12
|
+
end
|
13
|
+
|
14
|
+
opts.on("-p", "--param=KEY=VALUE", "Specify parameter for new files") do |v|
|
15
|
+
options[:params] ||= {}
|
16
|
+
k, v = v.split('=', 2)
|
17
|
+
options[:params][k.to_sym] = v
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on("-f", "--from=BUCKET:PATH", "Bucket name and key (or path) to copy from") do |v|
|
21
|
+
options[:from] = v
|
22
|
+
end
|
23
|
+
|
24
|
+
opts.on("-t", "--to=BUCKET:PATH", "Bucket name and key (or path) to write to (may be specified more than once)") do |v|
|
25
|
+
options[:to] ||= []
|
26
|
+
options[:to] << v
|
27
|
+
end
|
28
|
+
end.parse!
|
29
|
+
|
30
|
+
ENV['AWS_REGION'] ||= options[:region] || 'us-east-1'
|
31
|
+
|
32
|
+
bucket, key = options.fetch(:from).split(':', 2)
|
33
|
+
|
34
|
+
s3 = Aws::S3::Client.new
|
35
|
+
|
36
|
+
options.fetch(:to).each do |dest|
|
37
|
+
STDERR.puts "Copying to #{dest}"
|
38
|
+
dbucket, dkey = dest.split(':', 2)
|
39
|
+
s3.copy_object(
|
40
|
+
bucket: dbucket,
|
41
|
+
key: dkey,
|
42
|
+
copy_source: "/#{bucket}/#{key}",
|
43
|
+
**options[:params] || {},
|
44
|
+
)
|
45
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'aws-sdk-s3'
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: s3-upload options"
|
9
|
+
|
10
|
+
opts.on("-r", "--region=REGION", "AWS region to use (default us-east-1)") do |v|
|
11
|
+
options[:region] = v
|
12
|
+
end
|
13
|
+
|
14
|
+
opts.on("-p", "--param=KEY=VALUE", "Specify parameter for S3 upload") do |v|
|
15
|
+
options[:params] ||= {}
|
16
|
+
k, v = v.split('=', 2)
|
17
|
+
options[:params][k.to_sym] = v
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on("-f", "--file=PATH", "Path to the file to upload, - to upload standard input") do |v|
|
21
|
+
options[:file] = v
|
22
|
+
end
|
23
|
+
|
24
|
+
opts.on("-w", "--write=BUCKET:PATH", "Bucket name and key (or path) to upload to") do |v|
|
25
|
+
options[:write] = v
|
26
|
+
end
|
27
|
+
|
28
|
+
opts.on("-c", "--copy=BUCKET:PATH", "Bucket name and key (or path) to copy to (may be specified more than once)") do |v|
|
29
|
+
options[:copy] ||= []
|
30
|
+
options[:copy] << v
|
31
|
+
end
|
32
|
+
end.parse!
|
33
|
+
|
34
|
+
ENV['AWS_REGION'] ||= options[:region] || 'us-east-1'
|
35
|
+
|
36
|
+
def upload(f, options)
|
37
|
+
s3 = Aws::S3::Client.new
|
38
|
+
write = options.fetch(:write)
|
39
|
+
STDERR.puts "Writing #{write}"
|
40
|
+
bucket, key = write.split(':', 2)
|
41
|
+
s3.put_object(
|
42
|
+
body: f.read,
|
43
|
+
bucket: bucket,
|
44
|
+
key: key,
|
45
|
+
**options[:params] || {},
|
46
|
+
)
|
47
|
+
if copy = options[:copy]
|
48
|
+
copy.each do |dest|
|
49
|
+
STDERR.puts "Copying to #{dest}"
|
50
|
+
dbucket, dkey = dest.split(':', 2)
|
51
|
+
s3.copy_object(
|
52
|
+
bucket: dbucket,
|
53
|
+
key: dkey,
|
54
|
+
copy_source: "/#{bucket}/#{key}",
|
55
|
+
**options[:params] || {},
|
56
|
+
)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
if options[:file] == '-'
|
62
|
+
upload(STDIN, options)
|
63
|
+
elsif options[:file]
|
64
|
+
File.open(options[:file]) do |f|
|
65
|
+
upload(f, options)
|
66
|
+
end
|
67
|
+
else
|
68
|
+
upload(STDIN, options)
|
69
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
1
4
|
# ClusterConfig requires ClientRegistry class provided by the host project.
|
2
5
|
|
3
6
|
require 'singleton'
|
@@ -12,6 +15,11 @@ module Mrss
|
|
12
15
|
@single_server
|
13
16
|
end
|
14
17
|
|
18
|
+
def sharded_ish?
|
19
|
+
determine_cluster_config
|
20
|
+
@topology == :sharded || @topology == :load_balanced
|
21
|
+
end
|
22
|
+
|
15
23
|
def replica_set_name
|
16
24
|
determine_cluster_config
|
17
25
|
@replica_set_name
|
@@ -45,7 +53,7 @@ module Mrss
|
|
45
53
|
raise "Deployment server version not known - check that connection to deployment succeeded"
|
46
54
|
end
|
47
55
|
|
48
|
-
if server_version >= '3.4' &&
|
56
|
+
if server_version >= '3.4' && !sharded_ish?
|
49
57
|
fcv
|
50
58
|
else
|
51
59
|
if short_server_version == '4.1'
|
@@ -82,6 +90,11 @@ module Mrss
|
|
82
90
|
@primary_description
|
83
91
|
end
|
84
92
|
|
93
|
+
def server_parameters
|
94
|
+
determine_cluster_config
|
95
|
+
@server_parameters
|
96
|
+
end
|
97
|
+
|
85
98
|
# Try running a command on the admin database to see if the mongod was
|
86
99
|
# started with auth.
|
87
100
|
def auth_enabled?
|
@@ -107,7 +120,7 @@ module Mrss
|
|
107
120
|
:mmapv1
|
108
121
|
else
|
109
122
|
client = ClientRegistry.instance.global_client('root_authorized')
|
110
|
-
if
|
123
|
+
if sharded_ish?
|
111
124
|
shards = client.use(:admin).command(listShards: 1).first
|
112
125
|
if shards['shards'].empty?
|
113
126
|
raise 'Shards are empty'
|
@@ -196,8 +209,10 @@ module Mrss
|
|
196
209
|
@server_version = build_info['version']
|
197
210
|
@enterprise = build_info['modules'] && build_info['modules'].include?('enterprise')
|
198
211
|
|
199
|
-
|
200
|
-
|
212
|
+
@server_parameters = client.use(:admin).command(getParameter: '*').first
|
213
|
+
|
214
|
+
if !sharded_ish? && short_server_version >= '3.4'
|
215
|
+
rv = @server_parameters['featureCompatibilityVersion']
|
201
216
|
@fcv = rv['version'] || rv
|
202
217
|
end
|
203
218
|
end
|
@@ -52,7 +52,7 @@ module Mrss
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def require_topology(*topologies)
|
55
|
-
invalid_topologies = topologies - [:single, :replica_set, :sharded]
|
55
|
+
invalid_topologies = topologies - [:single, :replica_set, :sharded, :load_balanced]
|
56
56
|
|
57
57
|
unless invalid_topologies.empty?
|
58
58
|
raise ArgumentError, "Invalid topologies requested: #{invalid_topologies.join(', ')}"
|
@@ -82,7 +82,7 @@ module Mrss
|
|
82
82
|
unless ClusterConfig.instance.server_version >= '4.0'
|
83
83
|
skip 'Transactions tests in a replica set topology require server 4.0+'
|
84
84
|
end
|
85
|
-
when :sharded
|
85
|
+
when :sharded, :load_balanced
|
86
86
|
unless ClusterConfig.instance.server_version >= '4.2'
|
87
87
|
skip 'Transactions tests in a sharded cluster topology require server 4.2+'
|
88
88
|
end
|
@@ -113,6 +113,14 @@ module Mrss
|
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
+
def require_retry_writes
|
117
|
+
before(:all) do
|
118
|
+
unless SpecConfig.instance.retry_writes?
|
119
|
+
skip "Retry writes is disabled"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
116
124
|
def require_no_retry_writes
|
117
125
|
before(:all) do
|
118
126
|
if SpecConfig.instance.retry_writes?
|
@@ -172,8 +180,8 @@ module Mrss
|
|
172
180
|
skip "Zstd compression is enabled"
|
173
181
|
end
|
174
182
|
end
|
175
|
-
end
|
176
|
-
|
183
|
+
end
|
184
|
+
|
177
185
|
def require_no_compression
|
178
186
|
before(:all) do
|
179
187
|
if SpecConfig.instance.compressors
|
@@ -241,31 +249,52 @@ module Mrss
|
|
241
249
|
# (mongos 4.0+ overrides the write concern)
|
242
250
|
def require_set_write_concern
|
243
251
|
before(:all) do
|
244
|
-
if ClusterConfig.instance.topology
|
252
|
+
if %i(sharded load_balanced).include?(ClusterConfig.instance.topology) &&
|
253
|
+
ClusterConfig.instance.short_server_version >= '4.0'
|
254
|
+
then
|
245
255
|
skip "mongos 4.0+ overrides write concern"
|
246
256
|
end
|
247
257
|
end
|
248
258
|
end
|
249
259
|
|
250
|
-
def
|
260
|
+
def require_multi_mongos
|
251
261
|
before(:all) do
|
252
262
|
if ClusterConfig.instance.topology == :sharded && SpecConfig.instance.addresses.length == 1
|
253
|
-
skip 'Test requires a minimum of two
|
263
|
+
skip 'Test requires a minimum of two mongoses if run in sharded topology'
|
264
|
+
end
|
265
|
+
|
266
|
+
if ClusterConfig.instance.topology == :load_balanced && SpecConfig.instance.single_mongos?
|
267
|
+
skip 'Test requires a minimum of two mongoses if run in load-balanced topology'
|
254
268
|
end
|
255
269
|
end
|
256
270
|
end
|
257
271
|
|
258
|
-
|
272
|
+
# In sharded topology operations are distributed to the mongoses.
|
273
|
+
# When we set fail points, the fail point may be set on one mongos and
|
274
|
+
# operation may be executed on another mongos, causing failures.
|
275
|
+
# Tests that are not setting targeted fail points should utilize this
|
276
|
+
# method to restrict themselves to single mongos.
|
277
|
+
#
|
278
|
+
# In load-balanced topology, the same problem can happen when there is
|
279
|
+
# more than one mongos behind the load balancer.
|
280
|
+
def require_no_multi_mongos
|
259
281
|
before(:all) do
|
260
282
|
if ClusterConfig.instance.topology == :sharded && SpecConfig.instance.addresses.length > 1
|
261
|
-
skip 'Test requires a single
|
283
|
+
skip 'Test requires a single mongos if run in sharded topology'
|
284
|
+
end
|
285
|
+
if ClusterConfig.instance.topology == :load_balanced && !SpecConfig.instance.single_mongos?
|
286
|
+
skip 'Test requires a single mongos, as indicated by SINGLE_MONGOS=1 environment variable, if run in load-balanced topology'
|
262
287
|
end
|
263
288
|
end
|
264
289
|
end
|
265
290
|
|
291
|
+
alias :require_no_multi_shard :require_no_multi_mongos
|
292
|
+
|
266
293
|
def require_wired_tiger
|
267
294
|
before(:all) do
|
268
|
-
|
295
|
+
# Storage detection fails for serverless instances. However, it is safe to
|
296
|
+
# assume that a serverless instance uses WiredTiger Storage Engine.
|
297
|
+
if !SpecConfig.instance.serverless? && ClusterConfig.instance.storage_engine != :wired_tiger
|
269
298
|
skip 'Test requires WiredTiger storage engine'
|
270
299
|
end
|
271
300
|
end
|
@@ -274,7 +303,9 @@ module Mrss
|
|
274
303
|
def require_wired_tiger_on_36
|
275
304
|
before(:all) do
|
276
305
|
if ClusterConfig.instance.short_server_version >= '3.6'
|
277
|
-
|
306
|
+
# Storage detection fails for serverless instances. However, it is safe to
|
307
|
+
# assume that a serverless instance uses WiredTiger Storage Engine.
|
308
|
+
if !SpecConfig.instance.serverless? && ClusterConfig.instance.storage_engine != :wired_tiger
|
278
309
|
skip 'Test requires WiredTiger storage engine on 3.6+ servers'
|
279
310
|
end
|
280
311
|
end
|
@@ -283,7 +314,7 @@ module Mrss
|
|
283
314
|
|
284
315
|
def require_mmapv1
|
285
316
|
before(:all) do
|
286
|
-
if ClusterConfig.instance.storage_engine != :mmapv1
|
317
|
+
if SpecConfig.instance.serverless? || ClusterConfig.instance.storage_engine != :mmapv1
|
287
318
|
skip 'Test requires MMAPv1 storage engine'
|
288
319
|
end
|
289
320
|
end
|
@@ -326,5 +357,29 @@ module Mrss
|
|
326
357
|
end
|
327
358
|
end
|
328
359
|
end
|
360
|
+
|
361
|
+
def require_required_api_version
|
362
|
+
before(:all) do
|
363
|
+
unless ENV['API_VERSION_REQUIRED'] == '1'
|
364
|
+
skip 'Set API_VERSION_REQUIRED=1 to run this test'
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
def require_no_required_api_version
|
370
|
+
before(:all) do
|
371
|
+
if ENV['API_VERSION_REQUIRED'] == '1'
|
372
|
+
skip 'Cannot have API_VERSION_REQUIRED=1 to run this test'
|
373
|
+
end
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
def require_unix_socket
|
378
|
+
before(:all) do
|
379
|
+
if ENV['TOPOLOGY'] == 'load-balanced'
|
380
|
+
skip 'Load balancer does not listen on Unix sockets'
|
381
|
+
end
|
382
|
+
end
|
383
|
+
end
|
329
384
|
end
|
330
385
|
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
1
4
|
require 'optparse'
|
2
5
|
require 'erb'
|
3
6
|
autoload :Dotenv, 'dotenv'
|
@@ -108,7 +111,7 @@ module Mrss
|
|
108
111
|
'.'])
|
109
112
|
end
|
110
113
|
|
111
|
-
BASE_TEST_COMMAND = %w(docker run -i --tmpfs /tmpfs:exec).freeze
|
114
|
+
BASE_TEST_COMMAND = %w(docker run --rm -i --tmpfs /tmpfs:exec).freeze
|
112
115
|
|
113
116
|
def run_tests
|
114
117
|
run_command(BASE_TEST_COMMAND + tty_arg + extra_env + [image_tag] +
|
@@ -176,9 +179,11 @@ module Mrss
|
|
176
179
|
BASE_IMAGES = {
|
177
180
|
'debian81' => 'debian:jessie',
|
178
181
|
'debian92' => 'debian:stretch',
|
182
|
+
'debian10' => 'debian:buster',
|
179
183
|
'ubuntu1404' => 'ubuntu:trusty',
|
180
184
|
'ubuntu1604' => 'ubuntu:xenial',
|
181
185
|
'ubuntu1804' => 'ubuntu:bionic',
|
186
|
+
'ubuntu2004' => 'ubuntu:focal',
|
182
187
|
'rhel62' => 'centos:6',
|
183
188
|
'rhel70' => 'centos:7',
|
184
189
|
}.freeze
|
@@ -195,6 +200,10 @@ module Mrss
|
|
195
200
|
ruby == 'ruby-head'
|
196
201
|
end
|
197
202
|
|
203
|
+
def system_ruby?
|
204
|
+
%w(1 true yes).include?(@env['SYSTEM_RUBY']&.downcase)
|
205
|
+
end
|
206
|
+
|
198
207
|
def server_version
|
199
208
|
@env['MONGODB_VERSION']
|
200
209
|
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mrss
|
4
|
+
# Test event subscriber.
|
5
|
+
class EventSubscriber
|
6
|
+
|
7
|
+
# The mappings of event names to types.
|
8
|
+
MAPPINGS = {
|
9
|
+
'topology_opening_event' => Mongo::Monitoring::Event::TopologyOpening,
|
10
|
+
'topology_description_changed_event' => Mongo::Monitoring::Event::TopologyChanged,
|
11
|
+
'topology_closed_event' => Mongo::Monitoring::Event::TopologyClosed,
|
12
|
+
'server_opening_event' => Mongo::Monitoring::Event::ServerOpening,
|
13
|
+
'server_description_changed_event' => Mongo::Monitoring::Event::ServerDescriptionChanged,
|
14
|
+
'server_closed_event' => Mongo::Monitoring::Event::ServerClosed
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
attr_reader :all_events
|
18
|
+
|
19
|
+
attr_reader :started_events
|
20
|
+
|
21
|
+
attr_reader :succeeded_events
|
22
|
+
|
23
|
+
attr_reader :failed_events
|
24
|
+
|
25
|
+
attr_reader :published_events
|
26
|
+
|
27
|
+
# @param [ String ] name Optional name for the event subscriber.
|
28
|
+
def initialize(name: nil)
|
29
|
+
@mutex = Mutex.new
|
30
|
+
clear_events!
|
31
|
+
@name = name
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
%Q`#<EventSubscriber:#{@name ? "\"#{@name}\"" : '%x' % object_id} \
|
36
|
+
started=#{started_events.length} \
|
37
|
+
succeeded=#{succeeded_events.length} \
|
38
|
+
failed=#{failed_events.length} \
|
39
|
+
published=#{published_events.length}>`
|
40
|
+
end
|
41
|
+
|
42
|
+
alias :inspect :to_s
|
43
|
+
|
44
|
+
# Event retrieval
|
45
|
+
|
46
|
+
def select_started_events(cls)
|
47
|
+
started_events.select do |event|
|
48
|
+
event.is_a?(cls)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def select_succeeded_events(cls)
|
53
|
+
succeeded_events.select do |event|
|
54
|
+
event.is_a?(cls)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def select_completed_events(*classes)
|
59
|
+
(succeeded_events + failed_events).select do |event|
|
60
|
+
classes.any? { |c| c === event }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def select_published_events(cls)
|
65
|
+
published_events.select do |event|
|
66
|
+
event.is_a?(cls)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Filters command started events for the specified command name.
|
71
|
+
def command_started_events(command_name)
|
72
|
+
started_events.select do |event|
|
73
|
+
event.command[command_name]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def non_auth_command_started_events
|
78
|
+
started_events.reject do |event|
|
79
|
+
%w(authenticate getnonce saslSstart saslContinue).any? do |cmd|
|
80
|
+
event.command[cmd]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Locates command stated events for the specified command name,
|
86
|
+
# asserts that there is exactly one such event, and returns it.
|
87
|
+
def single_command_started_event(command_name, include_auth: false)
|
88
|
+
events = if include_auth
|
89
|
+
started_events
|
90
|
+
else
|
91
|
+
non_auth_command_started_events
|
92
|
+
end
|
93
|
+
events.select! do |event|
|
94
|
+
event.command[command_name]
|
95
|
+
end
|
96
|
+
if events.length != 1
|
97
|
+
raise "Expected a single #{command_name} event but we have #{events.length}"
|
98
|
+
end
|
99
|
+
events.first
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
# Get the first succeeded event published for the name, and then delete it.
|
104
|
+
#
|
105
|
+
# @param [ String ] name The event name.
|
106
|
+
#
|
107
|
+
# @return [ Event ] The matching event.
|
108
|
+
def first_event(name)
|
109
|
+
cls = MAPPINGS[name]
|
110
|
+
if cls.nil?
|
111
|
+
raise ArgumentError, "Bogus event name #{name}"
|
112
|
+
end
|
113
|
+
matching = succeeded_events.find do |event|
|
114
|
+
cls === event
|
115
|
+
end
|
116
|
+
succeeded_events.delete(matching)
|
117
|
+
matching
|
118
|
+
end
|
119
|
+
|
120
|
+
# Event recording
|
121
|
+
|
122
|
+
# Cache the started event.
|
123
|
+
#
|
124
|
+
# @param [ Event ] event The event.
|
125
|
+
def started(event)
|
126
|
+
@mutex.synchronize do
|
127
|
+
started_events << event
|
128
|
+
all_events << event
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Cache the succeeded event.
|
133
|
+
#
|
134
|
+
# @param [ Event ] event The event.
|
135
|
+
def succeeded(event)
|
136
|
+
@mutex.synchronize do
|
137
|
+
succeeded_events << event
|
138
|
+
all_events << event
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# Cache the failed event.
|
143
|
+
#
|
144
|
+
# @param [ Event ] event The event.
|
145
|
+
def failed(event)
|
146
|
+
@mutex.synchronize do
|
147
|
+
failed_events << event
|
148
|
+
all_events << event
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def published(event)
|
153
|
+
@mutex.synchronize do
|
154
|
+
published_events << event
|
155
|
+
all_events << event
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Clear all cached events.
|
160
|
+
def clear_events!
|
161
|
+
@all_events = []
|
162
|
+
@started_events = []
|
163
|
+
@succeeded_events = []
|
164
|
+
@failed_events = []
|
165
|
+
@published_events = []
|
166
|
+
self
|
167
|
+
end
|
168
|
+
end
|
169
|
+
# Only handles succeeded events correctly.
|
170
|
+
class PhasedEventSubscriber < EventSubscriber
|
171
|
+
def initialize
|
172
|
+
super
|
173
|
+
@phase_events = {}
|
174
|
+
end
|
175
|
+
|
176
|
+
def phase_finished(phase_index)
|
177
|
+
@phase_events[phase_index] = succeeded_events
|
178
|
+
@succeeded_events = []
|
179
|
+
end
|
180
|
+
|
181
|
+
def phase_events(phase_index)
|
182
|
+
@phase_events[phase_index]
|
183
|
+
end
|
184
|
+
|
185
|
+
def event_count
|
186
|
+
@phase_events.inject(0) do |sum, event|
|
187
|
+
sum + event.length
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
class VerboseEventSubscriber < EventSubscriber
|
193
|
+
%w(started succeeded failed published).each do |meth|
|
194
|
+
define_method(meth) do |event|
|
195
|
+
puts event.summary
|
196
|
+
super(event)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|