public async Task TestIisHostReceiver() { touched = 0; CancellationTokenSource tokenSource = new CancellationTokenSource(); // Start the listener on another thread. Task t = await Task.Factory.StartNew(Listener2, tokenSource.Token); var stopwatch = new Stopwatch(); Assert.Equal(0, Interlocked.Read(ref touched)); var messages = new List <Message>(); var tags = new Dictionary <string, string> { { "key", "value" } }; for (int i = 0; i < 1000; i++) { messages.Add( new Message("Order.Test.OutQ.Listener", Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()), string.Empty, Guid.NewGuid().ToString(), tags) ); } Assert.Equal(0, Interlocked.Read(ref touched)); QueueStream queue = new QueueStream("localhost:50000", "Demo.Test.IISHost", null); stopwatch.Start(); await queue.Send(new SendRequest(messages)); stopwatch.Stop(); Logger.Technical().Debug($"Messages sent in {stopwatch.ElapsedMilliseconds} ms.").Log(); stopwatch.Reset(); stopwatch.Start(); do { await Task.Delay(10); } while (Interlocked.Read(ref touched) < 1000 && stopwatch.ElapsedMilliseconds < 10000); stopwatch.Stop(); Logger.Technical().Debug($"Messages read in {stopwatch.ElapsedMilliseconds} ms.").Log(); tokenSource.Cancel(); await Task.Delay(150); Assert.Equal(1000, Interlocked.Read(ref touched)); Task.WaitAll(t); queue.Close(); }
private async Task Listener2(object parameter) { CancellationToken cancellationToken = (CancellationToken)parameter; QueueStream queue = new QueueStream("localhost:50000", "Demo.Test.Listener", null); PollRequest pollRequest = new() { Queue = "Order.Test.OutQ.Listener", WaitTimeout = 1000, MaxItems = 100, AutoAck = false }; var messagesToAck = new ConcurrentBag <Message>(); do { PollResponse response = await queue.Poll(pollRequest); if (!response.HasMessages) { continue; } Logger.Technical().Debug($"{response.Messages.Count()} message(s) received from the PollResponse.").Log(); Parallel.ForEach(response.Messages, async msg => { try { Assert.Equal(1, msg.Tags.Count()); Assert.True(Guid.TryParse(Encoding.UTF8.GetString(msg.Body), out var guid)); await Task.Delay(100); msg.Ack(); Interlocked.Increment(ref touched); } catch (Exception ex) { Logger.Technical().Exception(ex).Log(); msg.NAck(); } }); } while (!cancellationToken.IsCancellationRequested); queue.Close(); } }
public async Task TestBasicSendAndReceive() { QueueStream queue = new QueueStream("localhost:50000", "Demo.Test.Basic", null); var messages = new List <Message>(); var tags = new Dictionary <string, string> { { "key", "value" } }; for (int i = 0; i < 1000; i++) { messages.Add( new Message("Order.Test.OutQ.Basic", Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()), string.Empty, Guid.NewGuid().ToString(), tags) ); } var res = await queue.Send(new SendRequest(messages)); PollRequest pollRequest = new() { Queue = "Order.Test.OutQ.Basic", WaitTimeout = 1000, MaxItems = 100, AutoAck = false }; var read = 0; while (read < 1000) { PollResponse response = await queue.Poll(pollRequest); if (!response.HasMessages) { continue; } Logger.Technical().Debug($"{response.Messages.Count()} message(s) received from the PollResponse.").Log(); Parallel.ForEach(response.Messages, msg => { try { Assert.Equal(1, msg.Tags.Count()); Assert.True(Guid.TryParse(Encoding.UTF8.GetString(msg.Body), out var guid)); msg.Ack(); } catch (Exception) { msg.NAck(); } }); read += response.Messages.Count(); } Assert.Equal(1000, read); queue.Close(); }