Skip to content

Commit

Permalink
增加RedisCacheProvider单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 31, 2023
1 parent 3ae215a commit 4e707e9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
18 changes: 9 additions & 9 deletions NewLife.Redis/Services/RedisCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace NewLife.Caching.Services;
public class RedisCacheProvider : CacheProvider
{
#region 属性
private FullRedis _redis;
private FullRedis _redisQueue;
private FullRedis? _redis;
private FullRedis? _redisQueue;

/// <summary>队列</summary>
public FullRedis RedisQueue { get => _redisQueue; set => _redisQueue = value; }
public FullRedis? RedisQueue { get => _redisQueue; set => _redisQueue = value; }
#endregion

#region 构造
Expand All @@ -37,7 +37,7 @@ public RedisCacheProvider(IServiceProvider serviceProvider)
/// <summary>初始化</summary>
/// <param name="config"></param>
/// <param name="serviceProvider"></param>
public void Init(IConfigProvider config, IServiceProvider serviceProvider = null)
public void Init(IConfigProvider config, IServiceProvider? serviceProvider = null)
{
var cacheConn = config["RedisCache"];
var queueConn = config["RedisQueue"];
Expand All @@ -49,8 +49,8 @@ public void Init(IConfigProvider config, IServiceProvider serviceProvider = null
{
_redis = new FullRedis(serviceProvider, "RedisCache")
{
Log = serviceProvider.GetService<ILog>(),
Tracer = serviceProvider.GetService<ITracer>(),
Log = serviceProvider.GetRequiredService<ILog>(),
Tracer = serviceProvider.GetRequiredService<ITracer>(),
};
}
else
Expand All @@ -68,8 +68,8 @@ public void Init(IConfigProvider config, IServiceProvider serviceProvider = null
{
_redisQueue = new FullRedis(serviceProvider, "RedisQueue")
{
Log = serviceProvider.GetService<ILog>(),
Tracer = serviceProvider.GetService<ITracer>(),
Log = serviceProvider.GetRequiredService<ILog>(),
Tracer = serviceProvider.GetRequiredService<ITracer>(),
};
}
else
Expand All @@ -90,7 +90,7 @@ public void Init(IConfigProvider config, IServiceProvider serviceProvider = null
/// <param name="topic">主题</param>
/// <param name="group">消费组。未指定消费组时使用简单队列(如RedisQueue),指定消费组时使用完整队列(如RedisStream)</param>
/// <returns></returns>
public override IProducerConsumer<T> GetQueue<T>(String topic, String group = null)
public override IProducerConsumer<T> GetQueue<T>(String topic, String? group = null)
{
if (_redisQueue != null)
{
Expand Down
23 changes: 23 additions & 0 deletions XUnitTest/Services/RedisCacheProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using NewLife.Caching.Services;
using NewLife.Model;
using Xunit;

namespace XUnitTest.Services;

public class RedisCacheProviderTests
{
[Fact]
public void Ctor()
{
var sp = ObjectContainer.Provider;

var provider = new RedisCacheProvider(sp);
Assert.NotNull(provider.Cache);
Assert.NotNull(provider.InnerCache);
Assert.Null(provider.RedisQueue);

var q = provider.GetQueue<Object>("test");
Assert.NotNull(q);
}
}

0 comments on commit 4e707e9

Please sign in to comment.