Skip to content

Commit

Permalink
fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Qinyouzeng committed Sep 18, 2024
1 parent 714c56a commit 1a374f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests;
Expand All @@ -7,14 +7,15 @@ namespace Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests;
public class DistributedCacheClientTest : TestBase
{
private RedisCacheClient _distributedCacheClient;
private ConnectionMultiplexer _connection;
private IDatabase _database;

[TestInitialize]
public void Initialize()
{
_distributedCacheClient = new RedisCacheClient(GetConfigurationOptions());

_database = ConnectionMultiplexer.Connect(GetConfigurationOptions()).GetDatabase();
_connection = ConnectionMultiplexer.Connect(GetConfigurationOptions());
_distributedCacheClient = new RedisCacheClient(_connection, GetConfigurationOptions());
_database = _connection.GetDatabase();

_distributedCacheClient.Set("test_caching", "1");
_distributedCacheClient.Set("test_caching_2", Guid.Empty.ToString());
Expand Down Expand Up @@ -96,8 +97,9 @@ public async Task SetAndSpecifyTimeAsyncAndUseGlobalOptions(string key, string v
{
var globalRedisConfigurationOptions = GetConfigurationOptions();
globalRedisConfigurationOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60);
var distributedCacheClient = new RedisCacheClient(globalRedisConfigurationOptions);
var database = (await ConnectionMultiplexer.ConnectAsync(globalRedisConfigurationOptions)).GetDatabase();
var connection = await ConnectionMultiplexer.ConnectAsync(globalRedisConfigurationOptions);
var distributedCacheClient = new RedisCacheClient(connection, globalRedisConfigurationOptions);
var database = connection.GetDatabase();

await distributedCacheClient.SetAsync(key, value);
CheckLifeCycle(database, key, 55, 60);
Expand All @@ -110,8 +112,9 @@ public void SetAndSpecifyTimeAndUseGlobalOptions(string key, string value)
{
var globalRedisConfigurationOptions = GetConfigurationOptions();
globalRedisConfigurationOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60);
var distributedCacheClient = new RedisCacheClient(globalRedisConfigurationOptions);
var database = ConnectionMultiplexer.Connect(globalRedisConfigurationOptions).GetDatabase();
var connection = ConnectionMultiplexer.Connect(globalRedisConfigurationOptions);
var distributedCacheClient = new RedisCacheClient(connection, globalRedisConfigurationOptions);
var database = connection.GetDatabase();

distributedCacheClient.Set(key, value);
CheckLifeCycle(database, key, 55, 60);
Expand Down Expand Up @@ -528,7 +531,7 @@ public void TestGetKeys(string keyPattern, int count)
public void TestGetKeys2()
{
string key = "te" + Guid.NewGuid();
var distributedCacheClient = new RedisCacheClient(new RedisConfigurationOptions()
var distributedCacheClient = new RedisCacheClient(_connection, new RedisConfigurationOptions()
{
GlobalCacheOptions = new CacheOptions()
{
Expand Down Expand Up @@ -851,7 +854,7 @@ public void TestExists()
{
CacheKeyType = CacheKeyType.TypeName
};
var distributedCacheClient = new RedisCacheClient(configurationOptions);
var distributedCacheClient = new RedisCacheClient(_connection, configurationOptions);
var key = "redis.exist";
distributedCacheClient.Set(key, "1");
Assert.IsFalse(distributedCacheClient.Exists(key));
Expand All @@ -874,7 +877,7 @@ public async Task TestExistsAsync()
{
CacheKeyType = CacheKeyType.TypeName
};
var distributedCacheClient = new RedisCacheClient(configurationOptions);
var distributedCacheClient = new RedisCacheClient(_connection, configurationOptions);
var key = "redis.exist";
await distributedCacheClient.SetAsync(key, "1");
Assert.IsFalse(await distributedCacheClient.ExistsAsync(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class BaseRedis

public BaseRedis(RedisConfigurationOptions redisOptions)
{
DistributedCacheClient = new RedisCacheClient(redisOptions);
var options = (ConfigurationOptions)redisOptions;
Connection = ConnectionMultiplexer.Connect(options);
DistributedCacheClient = new RedisCacheClient(Connection, redisOptions);
Database = Connection.GetDatabase(options.DefaultDatabase ?? 0);
}
}

0 comments on commit 1a374f5

Please sign in to comment.