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

FakeTimecop #14915

Merged
merged 2 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions shared/test/fake_timecop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Allow tests to use a Timecop-like interface to pass real time, for special
# circumstances, like running against real Redis.
#
class FakeTimecop
def self.freeze
# No-op - included for interface compat. with Timecop.
end

def self.return
# No-op - included for interface compat. with Timecop.
end

#
# Pass real time - sleep for the given duration
# @param [Float] seconds - how long to sleep
#
def self.travel(seconds)
sleep seconds
end
end
49 changes: 16 additions & 33 deletions shared/test/test_redis_property_bag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

require_relative 'test_helper'
require 'fakeredis' unless use_real_redis?
require 'timecop' unless use_real_redis?
require 'timecop'
require_relative 'fake_timecop'
require_relative '../middleware/helpers/redis_property_bag'

class RedisPropertyBagTest < Minitest::Test
Expand Down Expand Up @@ -97,7 +98,8 @@ def test_delete_many
end

def test_expire
stop_time
timecop = use_real_redis? ? FakeTimecop : Timecop
timecop.freeze

# Set an expiration time
test_delay_seconds = 1
Expand All @@ -113,18 +115,19 @@ def test_expire
assert_equal({'foo1' => 'value1', 'foo2' => 'value2', 'foo3' => 'value3'}, bag.to_hash)

# Jump to just before expiration
time_travel test_delay_seconds - 0.1
timecop.travel test_delay_seconds - 0.1
assert_equal({'foo1' => 'value1', 'foo2' => 'value2', 'foo3' => 'value3'}, bag.to_hash)

# Jump forward in time to expiration
time_travel 0.2 # 0.1s margin of error for real redis
timecop.travel 0.2 # 0.1s margin of error for real redis
assert_equal({}, bag.to_hash)
ensure
resume_time
timecop.return
end

def test_deferred_expiration
stop_time
timecop = use_real_redis? ? FakeTimecop : Timecop
timecop.freeze

# Set an expiration time
test_delay_seconds = 1
Expand All @@ -140,57 +143,37 @@ def test_deferred_expiration
assert_equal({'foo1' => 'value1', 'foo2' => 'value2', 'foo3' => 'value3'}, bag.to_hash)

# Jump to just before expiration
time_travel test_delay_seconds - 0.1
timecop.travel test_delay_seconds - 0.1
assert_equal({'foo1' => 'value1', 'foo2' => 'value2', 'foo3' => 'value3'}, bag.to_hash)

# Perform a write to reset the expire time
bag.set('foo1', 'value4')
assert_equal({'foo1' => 'value4', 'foo2' => 'value2', 'foo3' => 'value3'}, bag.to_hash)

# Jump forward in time to original expiration
time_travel 0.1
timecop.travel 0.1
assert_equal({'foo1' => 'value4', 'foo2' => 'value2', 'foo3' => 'value3'}, bag.to_hash)

# Jump to just before new expiration
time_travel test_delay_seconds - 0.2
timecop.travel test_delay_seconds - 0.2
assert_equal({'foo1' => 'value4', 'foo2' => 'value2', 'foo3' => 'value3'}, bag.to_hash)

# Perform a delete to reset the expire time
bag.delete(['foo2'])
assert_equal({'foo1' => 'value4', 'foo3' => 'value3'}, bag.to_hash)

# Jump forward in time to original expiration
time_travel 0.1
timecop.travel 0.1
assert_equal({'foo1' => 'value4', 'foo3' => 'value3'}, bag.to_hash)

# Jump to just before new expiration
time_travel test_delay_seconds - 0.2
timecop.travel test_delay_seconds - 0.2
assert_equal({'foo1' => 'value4', 'foo3' => 'value3'}, bag.to_hash)

# Jump forward in time to new expiration
time_travel 0.2 # 0.1s margin of error for real redis
timecop.travel 0.2 # 0.1s margin of error for real redis
assert_equal({}, bag.to_hash)
ensure
resume_time
end

private

# Set of helpers for using Timecop if we're using fakeredis, or passing
# real time if we're using real redis.
def stop_time
Timecop.freeze unless use_real_redis?
end

def resume_time
Timecop.return unless use_real_redis?
end

def time_travel(seconds)
if use_real_redis?
sleep seconds
else
Timecop.travel seconds
end
timecop.return
end
end
52 changes: 18 additions & 34 deletions shared/test/test_redis_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

require_relative 'test_helper'
require 'fakeredis' unless use_real_redis?
require 'timecop' unless use_real_redis?
require 'timecop'
require_relative 'fake_timecop'
require 'helpers/null_pub_sub_api'
require 'helpers/redis_table'
require_relative 'spy_pub_sub_api'
Expand Down Expand Up @@ -96,13 +97,13 @@ def test_redis_tables

# Test getting multiple tables
table_map = RedisTable.get_tables(@redis, 'shard1', {'table' => 1, 'table2' => 1})
assert_equal(table_map.size, 2)
assert_equal(table_map['table'].size, 1)
assert_equal(table_map['table']['rows'].size, 2)
assert_equal(2, table_map.size)
assert_equal(1, table_map['table'].size)
assert_equal(2, table_map['table']['rows'].size)
assert_includes(table_map['table']['rows'], row1)
assert_includes(table_map['table']['rows'], row3)
assert_equal(table_map['table2'].size, 1)
assert_equal(table_map['table2']['rows'].size, 1)
assert_equal(1, table_map['table2'].size)
assert_equal(1, table_map['table2']['rows'].size)
assert_includes(table_map['table2']['rows'], table2_row1)

table_map = RedisTable.get_tables(@redis, 'shard1', {'table' => 3})
Expand Down Expand Up @@ -167,7 +168,8 @@ def test_delete_many
end

def test_expiration
stop_time
timecop = use_real_redis? ? FakeTimecop : Timecop
timecop.freeze

# Test with a 1-second expire time
expire_time = 1
Expand All @@ -180,7 +182,7 @@ def test_expiration
assert_equal [row1], table.to_a

# Jump to just before expiration
time_travel expire_time - margin_time
timecop.travel expire_time - margin_time
assert_equal [row1], table.to_a

# Reset expiration by inserting a new row
Expand All @@ -189,11 +191,11 @@ def test_expiration
assert_equal [row1, row2], table.to_a

# Jump to original expiration time - nothing should be deleted
time_travel margin_time
timecop.travel margin_time
assert_equal [row1, row2], table.to_a

# Jump to just before expiration again
time_travel expire_time - (2 * margin_time)
timecop.travel expire_time - (2 * margin_time)
assert_equal [row1, row2], table.to_a

# Reset expiration by updating a row
Expand All @@ -202,30 +204,30 @@ def test_expiration
assert_equal [row1, updated_row2], table.to_a

# Jump to next expiration time - nothing should be deleted
time_travel margin_time
timecop.travel margin_time
assert_equal [row1, updated_row2], table.to_a

# Jump to just before expiration a third time
time_travel expire_time - (2 * margin_time)
timecop.travel expire_time - (2 * margin_time)
assert_equal [row1, updated_row2], table.to_a

# Reset expiration by deleting a row
table.delete([1])
assert_equal [updated_row2], table.to_a

# Jump to expiration time - nothing should be deleted
time_travel margin_time
timecop.travel margin_time
assert_equal [updated_row2], table.to_a

# Jump to just before expiration a final time
time_travel expire_time - (2 * margin_time)
timecop.travel expire_time - (2 * margin_time)
assert_equal [updated_row2], table.to_a

# Jump to expiration time - this time, everything should be gone
time_travel margin_time
timecop.travel margin_time
assert_equal [], table.to_a
ensure
resume_time
timecop.return
end

def test_uuids
Expand Down Expand Up @@ -261,22 +263,4 @@ def test_uuids
def make_pubsub_event(channel, event, data)
{channel: channel, event: event, data: data}
end

# Set of helpers for using Timecop if we're using fakeredis, or passing
# real time if we're using real redis.
def stop_time
Timecop.freeze unless use_real_redis?
end

def resume_time
Timecop.return unless use_real_redis?
end

def time_travel(seconds)
if use_real_redis?
sleep seconds
else
Timecop.travel seconds
end
end
end