Skip to content

Commit e24bc83

Browse files
authored
RUBY-3097 add/use monotonic time for deadlines (#5491)
* RUBY-3097 add/use monotonic time for deadlines * remove license and fix tests
1 parent 4d7dcfc commit e24bc83

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

lib/mongoid.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
require 'concurrent-ruby'
1616

1717
require "mongo"
18-
require 'mongo/active_support'
18+
require "mongo/active_support"
1919

2020
require "mongoid/version"
2121
require "mongoid/deprecable"
@@ -27,6 +27,7 @@
2727
require "mongoid/tasks/database"
2828
require "mongoid/query_cache"
2929
require "mongoid/warnings"
30+
require "mongoid/utils"
3031

3132
# If we are using Rails then we will include the Mongoid railtie. This has all
3233
# the nifty initializers that Mongoid needs.

lib/mongoid/utils.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module Mongoid
4+
5+
# @api private
6+
module Utils
7+
8+
# This function should be used if you need to measure time.
9+
# @example Calculate elapsed time.
10+
# starting = Utils.monotonic_time
11+
# # do something time consuming
12+
# ending = Utils.monotonic_time
13+
# puts "It took #{(ending - starting).to_i} seconds"
14+
#
15+
# @see https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/
16+
#
17+
# @return [Float] seconds according to monotonic clock
18+
module_function def monotonic_time
19+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
20+
end
21+
end
22+
end

lib/mongoid/warnings.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def warning(id, message)
2323

2424
warning :geo_haystack_deprecated, 'The geoHaystack type is deprecated.'
2525
warning :as_json_compact_deprecated, '#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.'
26-
warning :symbol_type_deprecated, 'The BSON Symbol type is deprecated by MongoDB. Please use String or StringifiedSymbol field types instead of the Symbol field type'
26+
warning :symbol_type_deprecated, 'The BSON Symbol type is deprecated by MongoDB. Please use String or StringifiedSymbol field types instead of the Symbol field type.'
2727
warning :legacy_readonly, 'The readonly! method will only mark the document readonly when the legacy_readonly feature flag is switched off.'
2828
end
2929
end

spec/integration/app_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def clean_env
326326
end
327327

328328
def wait_for_port(port, timeout, process)
329-
deadline = Time.now + timeout
329+
deadline = Mongoid::Utils.monotonic_time + timeout
330330
loop do
331331
begin
332332
Socket.tcp('localhost', port, nil, nil, connect_timeout: 0.5) do |socket|
@@ -336,7 +336,7 @@ def wait_for_port(port, timeout, process)
336336
unless process.alive?
337337
raise "Process #{process} died while waiting for port #{port}"
338338
end
339-
if Time.now > deadline
339+
if Mongoid::Utils.monotonic_time > deadline
340340
raise
341341
end
342342
end

0 commit comments

Comments
 (0)