Skip to content

Commit

Permalink
A bit more Volkswagening
Browse files Browse the repository at this point in the history
  • Loading branch information
julik committed Feb 26, 2024
1 parent 821503a commit d56308a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
33 changes: 16 additions & 17 deletions test/adapters/adapter_test_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_bucket_lifecycle_with_unbounded_fillups
assert_in_delta level, 0.65, LEVEL_DELTA
assert_equal false, is_full

level, is_full = adapter.add_tokens(key: k, capacity: capacity, leak_rate: leak_rate, n_tokens: 0.4)
level, is_full = adapter.add_tokens(key: k, capacity: capacity, leak_rate: leak_rate, n_tokens: 0.7)
assert_in_delta level, 1.0, LEVEL_DELTA
assert_equal true, is_full

Expand Down Expand Up @@ -173,19 +173,22 @@ def test_bucket_lifecycle_handles_conditional_fillup_in_steps
end

def test_bucket_lifecycle_allows_conditional_fillup_after_leaking_out
key = random_key
capacity = 30
leak_rate = capacity / 0.5
rng = Random.new(Minitest.seed)
12.times do |i|
key = rng.hex(4)
capacity = 30
leak_rate = capacity / 0.5

_, _, did_accept = adapter.add_tokens_conditionally(key: key, capacity: capacity, leak_rate: leak_rate, n_tokens: 29.6)
assert did_accept
_, _, did_accept = adapter.add_tokens_conditionally(key: key, capacity: capacity, leak_rate: leak_rate, n_tokens: 29.6)
assert did_accept, "Should have accepted the topup on iteration #{i}"

_, _, did_accept = adapter.add_tokens_conditionally(key: key, capacity: capacity, leak_rate: leak_rate, n_tokens: 1)
refute did_accept
_, _, did_accept = adapter.add_tokens_conditionally(key: key, capacity: capacity, leak_rate: leak_rate, n_tokens: 1)
refute did_accept, "Should have refused the topup on iteration #{i}"

sleep 0.6 # Spend enough time to allow the bucket to leak out completely
_, _, did_accept = adapter.add_tokens_conditionally(key: key, capacity: capacity, leak_rate: leak_rate, n_tokens: 1)
assert did_accept, "Once the bucket has leaked out to 0 the fillup should be accepted again"
sleep 0.6 # Spend enough time to allow the bucket to leak out completely
_, _, did_accept = adapter.add_tokens_conditionally(key: key, capacity: capacity, leak_rate: leak_rate, n_tokens: 1)
assert did_accept, "Once the bucket has leaked out to 0 the fillup should be accepted again on iteration #{i}"
end
end

def test_set_block_sets_a_block
Expand Down Expand Up @@ -232,12 +235,8 @@ def test_prune
end

def test_create_tables
adapter = create_adapter # Has to be in local scope
ActiveRecord::Schema.define(version: 1) do |via_definer|
adapter.create_tables(via_definer)
end
rescue ActiveRecord::ConnectionNotEstablished
# This adapter does not require a connection
# All we are testing for is that the adapter responds to that method and accepts an argument
adapter.create_tables(nil)
end

def xtest_should_accept_threadsafe_conditional_fillups
Expand Down
12 changes: 12 additions & 0 deletions test/adapters/postgres_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def truncate_test_tables
ActiveRecord::Base.connection.execute("TRUNCATE TABLE pecorino_blocks")
end

def test_create_tables
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute("DROP TABLE pecorino_leaky_buckets")
ActiveRecord::Base.connection.execute("DROP TABLE pecorino_blocks")
# The adapter has to be in a variable as the schema definition is scoped to the migrator, not self
retained_adapter = create_adapter # the schema define block is run via instance_exec so it does not retain scope
ActiveRecord::Schema.define(version: 1) do |via_definer|
retained_adapter.create_tables(via_definer)
end
end
end

Minitest.after_run do
ActiveRecord::Base.connection.close
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "postgres")
Expand Down

0 comments on commit d56308a

Please sign in to comment.