Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactions between Lolex, Timecop and the Cache in HyperSpec #452

Open
catmando opened this issue Feb 23, 2023 · 0 comments
Open

Interactions between Lolex, Timecop and the Cache in HyperSpec #452

catmando opened this issue Feb 23, 2023 · 0 comments

Comments

@catmando
Copy link
Contributor

Hyperspec uses the Filecache gem, which uses Time.now. But if Timecop freezes time in a spec, then Time.now is not returning the system time, and the Filecache does not work. This should be fixable by wrapping calls to the Filecache with the timecop return method (and a block.) But this does not in general work because Hyperspec tries to communicate the "return" to the client, even if the client is not running.

The following series of patches fixes the issue, and can be incorporated into the next point release:

module HyperSpec
  module Internal
    module Controller
      class << self
        # wrap all calls with Timecop.return so the file cache gets the true system time
        def cache_read(key)
          Timecop.return { file_cache.get(key) }
        end

        def cache_write(key, value)
          Timecop.return { file_cache.set(key, value) }
        end

        def cache_delete(key)
          Timecop.return { file_cache.delete(key) }
        rescue StandardError
          nil
        end
      end
    end
  end
end

class Lolex
  # store all lolex updates unless hyper_spec is mounted
  def self.evaluate_ruby(&block)
    if @capybara_page&.instance_variable_get('@hyper_spec_mounted')
      @capybara_page.internal_evaluate_ruby(yield)
    else
      pending_evaluations << block
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant