From be9c1e6bc780c37d78fb85baf25eeceee6376b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Lled=C3=B3?= Date: Wed, 25 Oct 2023 13:11:09 +0200 Subject: [PATCH] Fix tests --- spec/unit/queue_storage_spec.rb | 6 ++-- spec/unit/stats/cleaner_spec.rb | 16 +++++----- test/integration/authrep/basic_test.rb | 2 +- test/integration/authrep/set_usage_test.rb | 4 +-- test/integration/report_test.rb | 6 ++-- test/test_helpers/storage.rb | 8 ++--- test/unit/storage_sync_test.rb | 37 ++++++++++++---------- 7 files changed, 41 insertions(+), 38 deletions(-) diff --git a/spec/unit/queue_storage_spec.rb b/spec/unit/queue_storage_spec.rb index 5543e4d73..d08cb51b4 100644 --- a/spec/unit/queue_storage_spec.rb +++ b/spec/unit/queue_storage_spec.rb @@ -52,11 +52,11 @@ module Backend private def is_sentinel?(connection) - connector = connection.instance_variable_get(:@inner) + config = connection.instance_variable_get(:@inner) .instance_variable_get(:@client) - .instance_variable_get(:@connector) + .instance_variable_get(:@config) - connector.instance_of?(Redis::Client::Connector::Sentinel) + !!config&.sentinel? end end end diff --git a/spec/unit/stats/cleaner_spec.rb b/spec/unit/stats/cleaner_spec.rb index 89d3ee494..d600a7d48 100644 --- a/spec/unit/stats/cleaner_spec.rb +++ b/spec/unit/stats/cleaner_spec.rb @@ -76,10 +76,10 @@ module Stats it 'deletes only the stats of services marked to be deleted' do Cleaner.delete!(non_proxied_instances) - expect(keys_not_to_be_deleted.keys.all? { |key| storage.exists(key) }) + expect(keys_not_to_be_deleted.keys.all? { |key| storage.exists?(key) }) .to be true - expect(keys_to_be_deleted.keys.none? { |key| storage.exists(key) }) + expect(keys_to_be_deleted.keys.none? { |key| storage.exists?(key) }) .to be true end @@ -94,7 +94,7 @@ module Stats before { storage.del(redis_set_marked_to_be_deleted) } it 'does not delete any keys' do - expect(all_keys.keys.all? {|key| storage.exists(key) }) + expect(all_keys.keys.all? {|key| storage.exists?(key) }) end end @@ -120,10 +120,10 @@ module Stats it 'deletes only the stats of services marked to be deleted' do Cleaner.delete!(non_proxied_instances, log_deleted_keys: log_to) - expect(keys_not_to_be_deleted.keys.all? { |key| storage.exists(key) }) + expect(keys_not_to_be_deleted.keys.all? { |key| storage.exists?(key) }) .to be true - expect(keys_to_be_deleted.keys.none? { |key| storage.exists(key) }) + expect(keys_to_be_deleted.keys.none? { |key| storage.exists?(key) }) .to be true end @@ -180,17 +180,17 @@ module Stats it 'deletes the stats with usage 0' do Cleaner.delete_stats_keys_set_to_0(non_proxied_instances) - expect(stats_with_usage_0.keys.none? { |k| storage.exists(k) }).to be true + expect(stats_with_usage_0.keys.none? { |k| storage.exists?(k) }).to be true end it 'does not delete the stats with usage != 0' do Cleaner.delete_stats_keys_set_to_0(non_proxied_instances) - expect(stats_with_non_zero_usage.keys.all? { |k| storage.exists(k) }). to be true + expect(stats_with_non_zero_usage.keys.all? { |k| storage.exists?(k) }). to be true end it 'does not delete non-stats keys' do Cleaner.delete_stats_keys_set_to_0(non_proxied_instances) - expect(non_stats_keys.keys.all? { |k| storage.exists(k) }).to be true + expect(non_stats_keys.keys.all? { |k| storage.exists?(k) }).to be true end context 'with the option to log deleted keys enabled' do diff --git a/test/integration/authrep/basic_test.rb b/test/integration/authrep/basic_test.rb index e0422e527..a625a4f74 100644 --- a/test/integration/authrep/basic_test.rb +++ b/test/integration/authrep/basic_test.rb @@ -1003,7 +1003,7 @@ def setup assert_equal 200, last_response.status # 'Hits' was 0, so there shouldn't be a stats key for it. - hits_key_created = @storage.exists( + hits_key_created = @storage.exists?( application_key( @service_id, @application.id, diff --git a/test/integration/authrep/set_usage_test.rb b/test/integration/authrep/set_usage_test.rb index 95ccbc7e7..a138913d9 100644 --- a/test/integration/authrep/set_usage_test.rb +++ b/test/integration/authrep/set_usage_test.rb @@ -276,7 +276,7 @@ def setup end stats_keys = app_keys_for_all_periods(@service_id, @application.id, hits_id, current_time) - stats_keys_created = stats_keys.any? { |key| @storage.exists(key) } + stats_keys_created = stats_keys.any? { |key| @storage.exists?(key) } assert_false stats_keys_created end @@ -304,7 +304,7 @@ def setup end stats_keys = app_keys_for_all_periods(@service_id, @application.id, hits_id, current_time) - stats_keys_created = stats_keys.any? { |key| @storage.exists(key) } + stats_keys_created = stats_keys.any? { |key| @storage.exists?(key) } assert_false stats_keys_created end end diff --git a/test/integration/report_test.rb b/test/integration/report_test.rb index 6a5825738..d865a59f4 100644 --- a/test/integration/report_test.rb +++ b/test/integration/report_test.rb @@ -286,7 +286,7 @@ def setup assert_equal 202, last_response.status # 'Hits' was 0, so there shouldn't be a stats key for it. - hits_key_created = @storage.exists( + hits_key_created = @storage.exists?( application_key( @service_id, @application.id, @@ -331,7 +331,7 @@ def setup assert_equal 202, last_response.status stats_keys = app_keys_for_all_periods(@service_id, @application.id, hits_id, current_time) - stats_keys_created = stats_keys.any? { |key| @storage.exists(key) } + stats_keys_created = stats_keys.any? { |key| @storage.exists?(key) } assert_false stats_keys_created end @@ -371,7 +371,7 @@ def setup assert_equal 202, last_response.status stats_keys = app_keys_for_all_periods(@service_id, @application.id, hits_id, current_time) - stats_keys_created = stats_keys.any? { |key| @storage.exists(key) } + stats_keys_created = stats_keys.any? { |key| @storage.exists?(key) } assert_false stats_keys_created end diff --git a/test/test_helpers/storage.rb b/test/test_helpers/storage.rb index 43f912317..a52ca9d84 100644 --- a/test/test_helpers/storage.rb +++ b/test/test_helpers/storage.rb @@ -88,12 +88,12 @@ def flushall end end - def method_missing(m, *args, &blk) + def method_missing(m, *args, **kwargs, &blk) # define and delegate the missing method - self.class.send(:define_method, m) do |*a, &b| - inner.send(m, *a, &b) + self.class.send(:define_method, m) do |*a, **kwa, &b| + inner.send(m, *a, **kwa, &b) end - inner.send(m, *args, &blk) + inner.send(m, *args, **kwargs, &blk) end def respond_to_missing?(m) diff --git a/test/unit/storage_sync_test.rb b/test/unit/storage_sync_test.rb index 8eb9a1925..5c9cf055c 100644 --- a/test/unit/storage_sync_test.rb +++ b/test/unit/storage_sync_test.rb @@ -49,7 +49,7 @@ def test_sentinels_connection_string } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: [{ host: '127.0.0.1', port: 26_379 }, { host: '127.0.0.1', port: 36_379 }]) @@ -62,7 +62,7 @@ def test_sentinels_connection_string_escaped } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: [{ host: '127.0.0.1', port: 26_379, password: 'passw,ord' }, { host: '127.0.0.1', port: 36_379 }]) @@ -75,7 +75,7 @@ def test_sentinels_connection_array_strings } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: [{ host: '127.0.0.1', port: 26_379 }, { host: '127.0.0.1', port: 36_379 }]) @@ -91,7 +91,7 @@ def test_sentinels_connection_array_hashes } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: config_obj[:sentinels].compact.reject(&:empty?)) end @@ -113,7 +113,7 @@ def test_sentinels_simple_url } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: "redis://#{config_obj[:url]}", sentinels: [{ host: '127.0.0.1', port: 26_379 }]) end @@ -128,7 +128,7 @@ def test_sentinels_array_hashes_default_port } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: [{ host: '127.0.0.1', port: default_sentinel_port }, { host: '192.168.1.1', port: default_sentinel_port }, @@ -146,7 +146,7 @@ def test_sentinels_array_strings_default_port } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: [{ host: '127.0.0.2', port: default_sentinel_port }, { host: '127.0.0.1', port: default_sentinel_port }, @@ -164,7 +164,7 @@ def test_sentinels_array_hashes_password } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: [{ host: '192.168.1.1', port: 3333, password: 'abc' }, { host: '192.168.1.2', port: 4444 }, @@ -180,7 +180,7 @@ def test_sentinels_array_strings_password } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: [{ host: '192.168.1.1', port: 3333, password: 'abc' }, { host: '192.168.1.2', port: 4444 }, @@ -196,7 +196,7 @@ def test_sentinels_correct_role } conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj) - assert_sentinel_connector(conn) + assert_sentinel_config(conn) assert_client_config(conn, url: config_obj[:url], sentinels: [{ host: '127.0.0.1', port: 26_379 }], role: role) @@ -255,16 +255,19 @@ def assert_connection(client) assert_equal 'bar', client.get('foo') end - def assert_sentinel_connector(client) - connector = client.instance_variable_get(:@inner).instance_variable_get(:@client).instance_variable_get(:@connector) - assert_instance_of Redis::Client::Connector::Sentinel, connector + def assert_sentinel_config(client) + config = client.instance_variable_get(:@inner).instance_variable_get(:@client).instance_variable_get(:@config) + assert config.sentinel? end def assert_client_config(conn, url:, **conf) - client = conn.instance_variable_get(:@inner).instance_variable_get(:@client) - assert_equal client.options[:url], url - conf.each do |k, v| - assert_equal v, client.options[k] + config = conn.instance_variable_get(:@inner).instance_variable_get(:@client).instance_variable_get(:@config) + assert_equal URI(url).host, config.name + assert_equal conf[:role] || :master, config.instance_variable_get(:@role) + conf[:sentinels].each_with_index do |s, i| + assert_equal s[:host], config.sentinels[i].host + assert_equal s[:port], config.sentinels[i].port + assert_equal s[:password], config.sentinels[i].password end end