Open qnm opened 3 years ago
Transferred this issue to this repo because the deadline logic for client libraries lives in the gapic-common gem. I think it is possible to make the gem compatible with timecop by using Process.clock_gettime
rather than Time.now
. It may, however, make testing the gem more difficult because... we can no longer mock Time.now
from our tests. 🤷🏻
For anyone else who runs into this problem, I just ran into this trying to start work on a move to Cloud Spanner (using the local emulator, btw) in a Rails app and ended up with a similar solution in my test suite.
I wasn't able to get the precise fix in #942 working as a runtime patch, but this has the same effect.
In a file named spec/support/spanner/ext/gapic_rpc_call.rb
, I redefined Time
with just the Time.now
method overridden:
# frozen_string_literal: true
# Fix for the "Timecop breaks spanner transactions" issue
#
# https://github.com/googleapis/gapic-generator-ruby/pull/942
# https://github.com/googleapis/gapic-generator-ruby/issues/941
module Gapic
class ServiceStub
class RpcCall
# redefine Time in the scope of this class to prevent the Time.now override bug
class Time < ::Time
def self.now
at Process.clock_gettime Process::CLOCK_REALTIME
end
end
end
end
end
I then required the file from spec/spec_helper.rb
.
The same problem occurs with Timecop.freeze
and Rails' built-in travel_to
, etc time methods.
OS: ruby:3.0.5-slim-bullseye docker image
Ruby version: 3.0.5
Gem name and version: gapic-common (0.20.0)
Thanks for stopping by to let us know something could be better!
PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.
Please run down the following list and make sure you've tried the usual "quick fixes":
If you are still having issues, please be sure to include as much information as possible:
Environment details
Steps to reproduce
Timecop.freeze(DateTime.parse('2021-04-23 10:00:00 +1100'))
Google::Cloud::DeadlineExceededError
timecop
is a gem commonly used to simulate time in the past. In my case I'm using it to ensure that relative time report parameters are fixed in my specs.Code example
Full backtrace
Making sure to follow these steps will guarantee the quickest resolution possible.
Thanks!