private static void Queue() { var redis = new Redis(keyNameSpace: "RedisQueueTests"); var queue = new RedisQueue <string>(redis, "CouldSendAndReceiveMessages", timeout: 5000); const int n = 10000; var sw = new Stopwatch(); sw.Start(); var producer1 = Task.Run(async() => { //await Task.Delay(1000); for (var i = 0; i < n; i++) { await queue.TrySendMessage(i.ToString()); //await queue.TrySendMessage(new String('x', i*1)); //await Task.Delay(50); } }); //var producer2 = Task.Run(async () => { // for (var i = n; i < 2*n; i++) { // await queue.TrySendMessage(i.ToString()); // //await Task.Delay(50); // } //}); var consumer = Task.Run(async() => { var c = 0; while (true) { //await Task.Delay(100); QueueReceiveResult <string> message = default(QueueReceiveResult <string>); try { message = await queue.TryReceiveMessage(); } catch (Exception e) { Console.WriteLine(e); } c++; //if (message.OK) { Console.WriteLine(message.Value); } if (message.Ok) { await queue.TryDeleteMessage(message.DeleteHandle); } if (message.Ok && c == n) { break; // n * 2 } } }); producer1.Wait(); //producer2.Wait(); consumer.Wait(); sw.Stop(); Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}"); queue.Dispose(); //Thread.Sleep(2000); }
public async Task CanDisposeCacheAndQueueAndReceiveSubscribedMessages() { var muxer = SharedConnection.GetMuxer(); var messageBus1 = new RedisMessageBus(new RedisMessageBusOptions { Subscriber = muxer.GetSubscriber(), Topic = "test-messages", LoggerFactory = Log }); var cache = new RedisCacheClient(new RedisCacheClientOptions { ConnectionMultiplexer = muxer }); Assert.NotNull(cache); var queue = new RedisQueue <SimpleWorkItem>(new RedisQueueOptions <SimpleWorkItem> { ConnectionMultiplexer = muxer, LoggerFactory = Log }); Assert.NotNull(queue); using (messageBus1) { using (cache) { using (queue) { await cache.SetAsync("test", "test", TimeSpan.FromSeconds(10)); await queue.DequeueAsync(new CancellationToken(true)); var countdown = new AsyncCountdownEvent(2); await messageBus1.SubscribeAsync <SimpleMessageA>(msg => { Assert.Equal("Hello", msg.Data); countdown.Signal(); }); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Hello" }); await countdown.WaitAsync(TimeSpan.FromSeconds(2)); Assert.Equal(1, countdown.CurrentCount); cache.Dispose(); queue.Dispose(); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Hello" }); await countdown.WaitAsync(TimeSpan.FromSeconds(2)); Assert.Equal(0, countdown.CurrentCount); } } } }