Skip to content

Commit

Permalink
test/unit: adapt storage tests
Browse files Browse the repository at this point in the history
Most of these tests only apply to StorageSync. StorageAsync does not
support sentinels, for example.
  • Loading branch information
davidor committed Sep 25, 2019
1 parent ee195e9 commit c37c7b3
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions test/unit/storage_test.rb → test/unit/storage_sync_test.rb
Expand Up @@ -2,43 +2,43 @@

class StorageTest < Test::Unit::TestCase
def test_basic_operations
@storage = Storage.instance(true)
@storage.flushdb
assert_nil @storage.get('foo')
@storage.set('foo', 'bar')
assert_equal 'bar', @storage.get('foo')
storage = StorageSync.instance(true)
storage.del('foo')
assert_nil storage.get('foo')
storage.set('foo', 'bar')
assert_equal 'bar', storage.get('foo')
end

def test_redis_host_and_port
storage = Storage.send :new, url('127.0.0.1:6379')
storage = StorageSync.send :new, url('127.0.0.1:6379')
assert_connection(storage)
end

def test_redis_url
storage = Storage.send :new, url('redis://127.0.0.1:6379/0')
storage = StorageSync.send :new, url('redis://127.0.0.1:6379/0')
assert_connection(storage)
end

def test_redis_unix
storage = Storage.send :new, url('unix:///tmp/redis_unix.6379.sock')
storage = StorageSync.send :new, url('unix:///tmp/redis_unix.6379.sock')
assert_connection(storage)
end

def test_redis_protected_url
assert_nothing_raised do
Storage.send :new, url('redis://user:passwd@127.0.0.1:6379/0')
StorageSync.send :new, url('redis://user:passwd@127.0.0.1:6379/0')
end
end

def test_redis_malformed_url
assert_raise Storage::InvalidURI do
Storage.send :new, url('a_malformed_url:1:10')
StorageSync.send :new, url('a_malformed_url:1:10')
end
end

def test_redis_url_without_scheme
assert_nothing_raised do
Storage.send :new, url('foo')
StorageSync.send :new, url('foo')
end
end

Expand All @@ -47,7 +47,8 @@ def test_sentinels_connection_string
url: 'redis://master-group-name',
sentinels: ',redis://127.0.0.1:26379, , , 127.0.0.1:36379,'
}
conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)

conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: [{ host: '127.0.0.1', port: 26_379 },
Expand All @@ -59,7 +60,8 @@ def test_sentinels_connection_string_escaped
url: 'redis://master-group-name',
sentinels: 'redis://user:passw\,ord@127.0.0.1:26379 ,127.0.0.1:36379, ,'
}
conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)

conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: [{ host: '127.0.0.1', port: 26_379, password: 'passw,ord' },
Expand All @@ -71,7 +73,8 @@ def test_sentinels_connection_array_strings
url: 'redis://master-group-name',
sentinels: ['redis://127.0.0.1:26379 ', ' 127.0.0.1:36379', nil]
}
conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)

conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: [{ host: '127.0.0.1', port: 26_379 },
Expand All @@ -86,7 +89,8 @@ def test_sentinels_connection_array_hashes
{ host: '127.0.0.1', port: 36_379 },
nil]
}
conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)

conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: config_obj[:sentinels].compact.reject(&:empty?))
Expand All @@ -98,7 +102,7 @@ def test_sentinels_malformed_url
sentinels: 'redis://127.0.0.1:26379,a_malformed_url:1:10'
}
assert_raise Storage::InvalidURI do
Storage.send :new, Storage::Helpers.config_with(config_obj)
StorageSync.send :new, Storage::Helpers.config_with(config_obj)
end
end

Expand All @@ -107,7 +111,8 @@ def test_sentinels_simple_url
url: 'master-group-name', # url of the sentinel master name conf
sentinels: 'redis://127.0.0.1:26379'
}
conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)

conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: "redis://#{config_obj[:url]}",
sentinels: [{ host: '127.0.0.1', port: 26_379 }])
Expand All @@ -121,7 +126,8 @@ def test_sentinels_array_hashes_default_port
{ host: '192.168.1.2', port: nil },
{ host: '127.0.0.1', port: 36379 }]
}
conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)

conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: [{ host: '127.0.0.1', port: default_sentinel_port },
Expand All @@ -138,7 +144,8 @@ def test_sentinels_array_strings_default_port
'192.168.1.1', '127.0.0.1:36379',
'redis://127.0.0.1:46379']
}
conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)

conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: [{ host: '127.0.0.2', port: default_sentinel_port },
Expand All @@ -156,7 +163,7 @@ def test_sentinels_array_hashes_password
{ host: '192.168.1.3', port: 5555, password: nil }]
}

conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)
conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: [{ host: '192.168.1.1', port: 3333, password: 'abc' },
Expand All @@ -172,7 +179,7 @@ def test_sentinels_array_strings_password
'redis://192.168.1.3:5555']
}

conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)
conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: [{ host: '192.168.1.1', port: 3333, password: 'abc' },
Expand All @@ -187,7 +194,8 @@ def test_sentinels_correct_role
sentinels: 'redis://127.0.0.1:26379',
role: role
}
conn = Storage.send :orig_new, Storage::Helpers.config_with(config_obj)

conn = StorageSync.send :new, Storage::Helpers.config_with(config_obj)
assert_sentinel_connector(conn)
assert_client_config(conn, url: config_obj[:url],
sentinels: [{ host: '127.0.0.1', port: 26_379 }],
Expand Down Expand Up @@ -218,13 +226,13 @@ def test_role_empty_when_sentinels_does_not_exist

def test_redis_no_scheme
assert_nothing_raised do
Storage.send :new, url('backend-redis:6379')
StorageSync.send :new, url('backend-redis:6379')
end
end

def test_redis_unknown_scheme
assert_raise ArgumentError do
Storage.send :new, url('myscheme://127.0.0.1:6379')
StorageSync.send :new, url('myscheme://127.0.0.1:6379')
end
end

Expand All @@ -248,12 +256,12 @@ def assert_connection(client)
end

def assert_sentinel_connector(client)
connector = client.instance_variable_get(:@client).instance_variable_get(:@connector)
connector = client.instance_variable_get(:@inner).instance_variable_get(:@client).instance_variable_get(:@connector)
assert_instance_of Redis::Client::Connector::Sentinel, connector
end

def assert_client_config(conn, url:, **conf)
client = conn.instance_variable_get(:@client)
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]
Expand Down

0 comments on commit c37c7b3

Please sign in to comment.