Skip to content

Commit

Permalink
feat: add unit test for freeredis bus
Browse files Browse the repository at this point in the history
  • Loading branch information
Memoyu committed Jun 13, 2023
1 parent 82d664d commit 73f7f88
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Xml.Linq;

public class FreeRedisOptionsExtension : IEasyCachingOptionsExtension
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyCaching.BaseTest" Version="1.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="EasyCaching.BaseTest" Version="1.9.0" />
<PackageReference Include="EasyCaching.Serialization.Json" Version="1.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EasyCaching.Bus.FreeRedis\EasyCaching.Bus.FreeRedis.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EasyCaching.Bus.FreeRedis\EasyCaching.Bus.FreeRedis.csproj" />
</ItemGroup>

</Project>
103 changes: 79 additions & 24 deletions tests/EasyCaching.Bus.FreeRedis.Tests/FreeRedisCachingBusTest.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,94 @@
using Castle.DynamicProxy.Generators;
using EasyCaching.Core.Bus;
using FreeRedis;
using Microsoft.Extensions.DependencyInjection;

namespace EasyCaching.Bus.FreeRedis.Tests
namespace EasyCaching.Bus.FreeRedis.Tests;

public class FreeRedisCachingBusTest
{
public class FreeRedisCachingBusTest
{

public FreeRedisCachingBusTest()
private const string Topic = "test-topic";
private IEasyCachingBus _bus;

public FreeRedisCachingBusTest()
{
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(option =>
{

}
option.WithFreeRedisBus(config =>
{
config.ConnectionStrings = new List<ConnectionStringBuilder>
{
"192.168.3.86:6379,defaultDatabase=6,poolsize=10"
};
config.SerializerName = "json";
});
option.WithJson("json");
});

[Fact]
public void WithFreeRedisBus_Connectioned_Should_Succeed()
{
IServiceCollection services = new ServiceCollection();
IServiceProvider serviceProvider = services.BuildServiceProvider();

_bus = serviceProvider.GetService<IEasyCachingBus>()!;
}


[Fact]
public void WithFreeRedisBus_Connectioned_Should_Succeed()
{
IServiceCollection services = new ServiceCollection();

services.AddEasyCaching(option =>
services.AddEasyCaching(option =>
{
option.WithFreeRedisBus(config =>
{
option.WithFreeRedisBus(config =>
config.ConnectionStrings = new List<ConnectionStringBuilder>
{
config.ConnectionStrings = new List<ConnectionStringBuilder>
{
"192.168.3.86:6379,defaultDatabase=6,poolsize=10"
};
config.SerializerName = "json";
});
"192.168.3.86:6379,defaultDatabase=6,poolsize=10"
};
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
var client = serviceProvider.GetService<EasyCachingFreeRedisClient>();
Assert.NotNull(client);
});
IServiceProvider serviceProvider = services.BuildServiceProvider();
var client = serviceProvider.GetService<EasyCachingFreeRedisClient>();
Assert.NotNull(client);

var flag = client.Ping();
Assert.Equal("PONG", flag);
}

[Fact]
public async Task Publish_Msg_Should_Succeed()
{
var message = new EasyCachingMessage
{
Id = Guid.NewGuid().ToString("N"),
CacheKeys = new string[] { "freeredis:bus:cachekey" }
};
await _bus.PublishAsync(Topic, message);

Assert.True(true);
}

[Fact]
public async Task Publish_Msg_And_Subscribe_Should_Succeed()
{
var sendMsgCachkey = "freeredis:bus:cachekey";
var message = new EasyCachingMessage
{
Id = Guid.NewGuid().ToString("N"),
CacheKeys = new string[] { sendMsgCachkey }
};
var getMsgCachkey = string.Empty;
await _bus.SubscribeAsync(Topic,
msg =>
{
getMsgCachkey = msg.CacheKeys[0];
},
() => { });

var flag = client.Ping();
Assert.Equal("PONG", flag);
}
await _bus.PublishAsync(Topic, message);
await Task.Delay(1000);
Assert.Equal(sendMsgCachkey, getMsgCachkey);
}
}

0 comments on commit 73f7f88

Please sign in to comment.