diff --git a/test/adapters/adapter_test_methods.rb b/test/adapters/adapter_test_methods.rb index 77a9e9e..be708c0 100644 --- a/test/adapters/adapter_test_methods.rb +++ b/test/adapters/adapter_test_methods.rb @@ -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 @@ -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 @@ -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 diff --git a/test/adapters/postgres_adapter_test.rb b/test/adapters/postgres_adapter_test.rb index 1e43786..2efab20 100644 --- a/test/adapters/postgres_adapter_test.rb +++ b/test/adapters/postgres_adapter_test.rb @@ -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")