Skip to content

Commit

Permalink
Update tests to remove Timecop for checking lru values
Browse files Browse the repository at this point in the history
  • Loading branch information
miry committed Nov 12, 2022
1 parent 010ea9d commit b7fcb45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
7 changes: 5 additions & 2 deletions lib/semian/lru_hash.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "thread"

class LRUHash
# This LRU (Least Recently Used) hash will allow
# the cleaning of resources as time goes on.
Expand Down Expand Up @@ -57,9 +59,9 @@ def initialize(max_size: Semian.maximum_lru_size, min_time: Semian.minimum_lru_t
@table = {}
@lock =
if Semian.thread_safe?
Mutex.new
Thread::Mutex.new
else
NoopMutex.new
LRUHash::NoopMutex.new
end
end

Expand Down Expand Up @@ -137,6 +139,7 @@ def clear_unused_resources
@table.each do |_, resource|
payload[:examined] += 1

puts "resource.updated_at: #{resource.updated_at}"
# The update times of the resources in the LRU are monotonically increasing,
# time, so we can stop looking once we find the first resource with an
# update time after the stop_time.
Expand Down
41 changes: 18 additions & 23 deletions test/lru_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def test_clean_instrumentation

def test_monotonically_increasing
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
lru_hash = LRUHash.new(max_size: 0, min_time: 3)

notification = 0
subscriber = Semian.subscribe do |event, _resource, _scope, _adapter, payload|
Expand All @@ -169,38 +170,32 @@ def test_monotonically_increasing
end
end

assert_monotonic = lambda do
assert_monotonic = lambda do |lru_hash|
previous_timestamp = start_time

@lru_hash.keys.zip(@lru_hash.values).each do |key, val|
lru_hash.keys.zip(lru_hash.values).each do |key, val|
assert(val.updated_at > previous_timestamp, "Timestamp for #{key} was not monotonically increasing")
end
end

@lru_hash.set("a", create_circuit_breaker("a"))
@lru_hash.set("b", create_circuit_breaker("b"))
@lru_hash.set("c", create_circuit_breaker("c"))
lru_hash.set("a", create_circuit_breaker("a"))
lru_hash.set("b", create_circuit_breaker("b"))
lru_hash.set("c", create_circuit_breaker("c"))
assert_equal ["a", "b", "c"], lru_hash.values.map(&:name)

# Before: [a, b, c]
# After: [a, c, b]
Timecop.travel(Semian.minimum_lru_time - 1) do
@lru_hash.get("b")
assert_monotonic.call
end
sleep 1
lru_hash.get("b")
assert_monotonic.call(lru_hash)
assert_equal ["a", "c", "b"], lru_hash.values.map(&:name)

# Before: [a, c, b]
# After: [a, c, b, d]
Timecop.travel(Semian.minimum_lru_time - 1) do
@lru_hash.set("d", create_circuit_breaker("d"))
assert_monotonic.call
end
lru_hash.set("d", create_circuit_breaker("d"))
assert_monotonic.call(lru_hash)
assert_equal ["a", "c", "b", "d"], lru_hash.values.map(&:name)

# Before: [a, c, b, d]
# After: [b, d, e]
Timecop.travel(Semian.minimum_lru_time) do
@lru_hash.set("e", create_circuit_breaker("e"))
assert_monotonic.call
end
sleep 2
lru_hash.set("e", create_circuit_breaker("e"))
assert_monotonic.call(lru_hash)
assert_equal ["b", "d", "e"], lru_hash.values.map(&:name)
ensure
Semian.unsubscribe(subscriber)
end
Expand Down

0 comments on commit b7fcb45

Please sign in to comment.