Skip to content

Commit 2d4d6e3

Browse files
Fix Mongoid CI (#5986) (#5993)
1 parent c67919a commit 2d4d6e3

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

.evergreen/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ buildvariants:
664664
ruby: ["ruby-3.1"]
665665
mongodb-version: "6.0"
666666
topology: ['replica-set', 'sharded-cluster']
667-
os: debian11
667+
os: ubuntu-22.04
668668
display_name: "${ruby}, ${driver}, ${mongodb-version}, ${topology}"
669669
tasks:
670670
- name: "test"

.evergreen/config/variants.yml.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ buildvariants:
6060
ruby: ["ruby-3.1"]
6161
mongodb-version: "6.0"
6262
topology: ['replica-set', 'sharded-cluster']
63-
os: debian11
63+
os: ubuntu-22.04
6464
display_name: "${ruby}, ${driver}, ${mongodb-version}, ${topology}"
6565
tasks:
6666
- name: "test"

spec/support/expectations.rb

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@
22

33
module Mongoid
44
module Expectations
5-
6-
def connection_class
7-
if defined?(Mongo::Server::ConnectionBase)
8-
Mongo::Server::ConnectionBase
9-
else
10-
# Pre-2.8 drivers
11-
Mongo::Server::Connection
5+
# Previously this method used RSpec::Mocks with .exactly.times(n).and_call_original,
6+
# which stopped working reliably in Ruby 3.3. Now we directly wrap the target method.
7+
def expect_query(number)
8+
if %i[ sharded load-balanced ].include?(ClusterConfig.instance.topology) && number > 0
9+
skip 'This spec requires replica set or standalone topology'
1210
end
13-
end
1411

15-
def expect_query(number)
16-
rv = nil
17-
RSpec::Mocks.with_temporary_scope do
18-
if number > 0
19-
expect_any_instance_of(connection_class).to receive(:command_started).exactly(number).times.and_call_original
20-
else
21-
expect_any_instance_of(connection_class).not_to receive(:command_started)
12+
klass = Mongo::Server::ConnectionBase
13+
original_method = klass.instance_method(:command_started)
14+
query_count = 0
15+
16+
begin
17+
klass.define_method(:command_started) do |*args, **kwargs|
18+
query_count += 1
19+
original_method.bind_call(self, *args, **kwargs)
2220
end
23-
rv = yield
21+
22+
result = yield
23+
expect(query_count).to eq(number)
24+
result
25+
ensure
26+
klass.remove_method(:command_started)
27+
klass.define_method(:command_started, original_method)
2428
end
25-
rv
2629
end
2730

2831
def expect_no_queries(&block)

0 commit comments

Comments
 (0)