1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/test/cache/cache_entry_test.rb
Jean Boussier 9de17ac4a4 Allow to set cache expiry as an absolute timestamp
Sometime it can be useful to set a cache entry expiry
not relative to current time, but as an absolute timestamps,
e.g.:

  - If you want to cache an API token that was provided to
    you with a precise expiry time.
  - If you want to cache something until a precise cutoff
    time, e.g. `expires_at: Time.now.at_end_of_hour`

This leaves the `@created_at` variable in a weird state,
but this is to avoid breaking the binary format.
2021-04-06 10:49:05 +02:00

22 lines
758 B
Ruby

# frozen_string_literal: true
require_relative "../abstract_unit"
require "active_support/cache"
class CacheEntryTest < ActiveSupport::TestCase
def test_expired
entry = ActiveSupport::Cache::Entry.new("value")
assert_not entry.expired?, "entry not expired"
entry = ActiveSupport::Cache::Entry.new("value", expires_in: 60)
assert_not entry.expired?, "entry not expired"
Time.stub(:now, Time.at(entry.expires_at + 1)) do
assert entry.expired?, "entry is expired"
end
end
def test_initialize_with_expires_at
entry = ActiveSupport::Cache::Entry.new("value", expires_in: 60)
clone = ActiveSupport::Cache::Entry.new("value", expires_at: entry.expires_at)
assert_equal entry.expires_at, clone.expires_at
end
end