Skip to content

Commit

Permalink
Add support for .NET 6
Browse files Browse the repository at this point in the history
  • Loading branch information
yang-xiaodong committed Jul 30, 2024
1 parent ec3a66b commit 0661ede
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 108 deletions.
8 changes: 7 additions & 1 deletion src/Savorboard.CAP.InMemoryMessageQueue/InMemoryQueue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using DotNetCore.CAP.Messages;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -73,6 +74,11 @@ public void Send(TransportMessage message)
}
}
}
else
{
throw new InvalidOperationException(
$"Cannot find the corresponding group for {name}. Have you subscribed?");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8</TargetFramework>
<TargetFrameworks>net6;net8</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -32,7 +33,7 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Version>8.2.0</Version>
<Version>8.2.1</Version>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down
78 changes: 78 additions & 0 deletions test/InMemoryQueueTest/ConsumerClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DotNetCore.CAP.Internal;
using DotNetCore.CAP.Messages;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Savorboard.CAP.InMemoryMessageQueue;
using Xunit;
using Xunit.Abstractions;

namespace InMemoryQueueTest
{
public class ConsumerClientTests(ITestOutputHelper output)
{
[Fact]
public void QueueNotSubscribeTest()
{
var logger = NullLoggerFactory.Instance.CreateLogger<InMemoryQueue>();
var groupId = "test-group";
var topic = "test-topic";
var content = "test content";

var queue = new InMemoryQueue(logger);
queue.Subscribe(groupId, [topic]);
var headers = new Dictionary<string, string>();
var messageId = new SnowflakeId().NextId().ToString();
headers.Add(Headers.MessageId, messageId);
headers.Add(Headers.MessageName, topic + "-assert");
Assert.Throws<InvalidOperationException>(() => queue.Send(new TransportMessage(headers, Encoding.UTF8.GetBytes(content))));
}

[Fact]
public void SendMessageTest()
{
var logger = NullLoggerFactory.Instance.CreateLogger<InMemoryQueue>();
var groupId = "test-group";
var topic = "test-topic";
var content = "test content";

var queue = new InMemoryQueue(logger);
var headers = new Dictionary<string, string>();

var messageId = new SnowflakeId().NextId().ToString();
headers.Add(Headers.MessageId, messageId);
headers.Add(Headers.MessageName, topic);
headers.Add(Headers.Type, typeof(string).FullName);
headers.Add(Headers.SentTime, DateTimeOffset.Now.ToString());
if (headers.TryAdd(Headers.CorrelationId, messageId))
{
headers.Add(Headers.CorrelationSequence, 0.ToString());
}
var transportMsg = new TransportMessage(headers, Encoding.UTF8.GetBytes(content));

ManualResetEventSlim reset = new(false);
var client = new InMemoryConsumerClient(logger, queue, groupId, 1)
{
OnMessageCallback = (x, obj) =>
{
output.WriteLine($"Received message: {Encoding.UTF8.GetString(x.Body.ToArray())}");
Assert.Equal(content, Encoding.UTF8.GetString(x.Body.ToArray()));
reset.Set();
return Task.CompletedTask;
}
};

client.Subscribe([topic]);

Task.Run(() => client.Listening(TimeSpan.FromSeconds(10), default));

queue.Send(transportMsg);

reset.Wait();
}
}
}
48 changes: 0 additions & 48 deletions test/InMemoryQueueTest/InMemoryConsumerClientTests.cs

This file was deleted.

57 changes: 0 additions & 57 deletions test/InMemoryQueueTest/InMemoryQueueTest.cs

This file was deleted.

0 comments on commit 0661ede

Please sign in to comment.