-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec3a66b
commit 0661ede
Showing
5 changed files
with
88 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.