public async Task Test_LockComplete_MessageFlow <T>(T _)
            where T : IMessage, new() // be careful with this, if the test doesn't run it's because the T validation is broken
        {
            _.CheckInlineType();      // inline data check

            await NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString()).ScorchNamespace();

            MessageQueue.LockTimers.Release();
            MessageQueue.BrokeredMessages.Release();

            using (var ts = new CancellationTokenSource())
                using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
                {
                    await msn.Send(new T());

                    msn.Receive <T>(
                        async m =>
                    {
                        await m.Lock();
                        await Task.Delay(TimeSpan.FromSeconds(5), ts.Token);
                        await m.Complete();

                        ts.Cancel(); // kill switch
                    });

                    try
                    {
                        await Task.Delay(TimeSpan.FromMinutes(2), ts.Token);
                    }
                    catch (TaskCanceledException) { /* soak the kill switch */ }

                    MessageQueue.BrokeredMessages.Should().BeEmpty();
                    MessageQueue.LockTimers.Should().BeEmpty();
                }
        }
        public async Task Test_SendingRandomMessages <T>(T _)
            where T : IMessage, new() // be careful with this, if the test doesn't run it's because the T validation is broken
        {
            _.CheckInlineType();      // inline data check

            var sendCount = new Random().Next(1, 10);
            await NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString()).ScorchNamespace();

            MessageQueue.LockTimers.Release();
            MessageQueue.BrokeredMessages.Release();

            var messages = new List <T>();

            for (var i = 0; i < sendCount; i++)
            {
                messages.Add(new T());
            }

            using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
            {
                foreach (var message in messages)
                {
                    await msn.Send(message);
                }

                await Task.Delay(TimeSpan.FromSeconds(5)); // wait 5 seconds to flush out all the messages

                var qClient   = QueueClient.CreateFromConnectionString(NamespaceHelper.GetConnectionString(), typeof(T).GetQueueName());
                var rMessages = (await qClient.ReadBatchAsync <T>(sendCount)).ToList();
                rMessages.Should().BeEquivalentTo(messages);

                qClient.Close();
            }
        }
        public async Task Test_LockMessage_ForFiveMinutes <T>(T _)
            where T : IMessage, new() // be careful with this, if the test doesn't run it's because the T validation is broken
        {
            _.CheckInlineType();      // inline data check

            await NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString()).ScorchNamespace();

            MessageQueue.LockTimers.Release();
            MessageQueue.BrokeredMessages.Release();

            using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
            {
                await msn.Send(new T());

                var message = default(T);
                msn.Receive <T>(
                    async m =>
                {
                    message = m;
                    await message.Lock();
                });

                await Task.Delay(TimeSpan.FromMinutes(5));

                message.Should().NotBeNull();
                await message.Complete(); // If this throws, Lock failed
            }
        }
        public async Task Test_ReceivingRandomMessages <T>(T _)
            where T : IMessage, new() // be careful with this, if the test doesn't run it's because the T validation is broken
        {
            _.CheckInlineType();      // inline data check

            var receiveCount = new Random().Next(1, 10);
            await NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString()).ScorchNamespace();

            MessageQueue.LockTimers.Release();
            MessageQueue.BrokeredMessages.Release();

            var rMessages = new List <T>();
            var messages  = new List <T>();

            for (var i = 0; i < receiveCount; i++)
            {
                messages.Add(new T());
            }

            using (var ts = new CancellationTokenSource())
                using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
                {
                    // We need to create the messenger before sending the messages to avoid writing unecessary code to create the queue
                    // during the test. Receive will create the queue automatically. This breaks the AAA pattern by design.
                    // This test flow also ensures that receive will actually create the queue properly.
                    msn.Receive <T>(
                        m =>
                    {
                        rMessages.Add(m);
                        if (rMessages.Count == messages.Count)
                        {
                            ts.Cancel();                                    // kill switch
                        }
                    });

                    var qClient = QueueClient.CreateFromConnectionString(NamespaceHelper.GetConnectionString(), typeof(T).GetQueueName());
                    await qClient.WriteBatchAsync(messages);

                    try
                    {
                        await Task.Delay(TimeSpan.FromMinutes(2), ts.Token);
                    }
                    catch (TaskCanceledException) { /* soak the kill switch */ }

                    rMessages.Should().BeEquivalentTo(messages);
                }
        }
        public async Task Test_ReceiveCreatesTheQueue <T>(T _)
            where T : IMessage, new() // be careful with this, if the test doesn't run it's because the T validation is broken
        {
            _.CheckInlineType();      // inline data check

            var nsm = NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString());
            await nsm.ScorchNamespace();

            MessageQueue.LockTimers.Release();
            MessageQueue.BrokeredMessages.Release();

            using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
            {
                msn.Receive <T>(__ => { });
                (await nsm.GetQueuesAsync()).ToList().AssertSingleQueueExists(typeof(T));
            }
        }
        public async Task Test_SendCreatesTheQueue <T>(T _)
            where T : IMessage, new() // be careful with this, if the test doesn't run it's because the T validation is broken
        {
            _.CheckInlineType();      // inline data check

            var nsm = NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString());
            await nsm.ScorchNamespace();

            MessageQueue.LockTimers.Release();
            MessageQueue.BrokeredMessages.Release();

            using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
            {
                await msn.Send(new T());

                await Task.Delay(TimeSpan.FromSeconds(5)); // wait 5 seconds to flush out all the messages

                (await nsm.GetQueuesAsync()).ToList().AssertSingleQueueExists(typeof(T));
            }
        }
        public async Task Test_LockError_MessageFlow <T>(T _)
            where T : IMessage, new() // be careful with this, if the test doesn't run it's because the T validation is broken
        {
            _.CheckInlineType();      // inline data check

            await NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString()).ScorchNamespace();

            MessageQueue.LockTimers.Release();
            MessageQueue.BrokeredMessages.Release();

            using (var ts = new CancellationTokenSource())
                using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
                {
                    var message = new T();
                    await msn.Send(message);

                    msn.Receive <T>(
                        async m =>
                    {
                        await m.Lock();
                        await Task.Delay(TimeSpan.FromSeconds(5), ts.Token);
                        await m.Error();

                        ts.Cancel(); // kill switch
                    });

                    try
                    {
                        await Task.Delay(TimeSpan.FromMinutes(2), ts.Token);
                    }
                    catch (TaskCanceledException) { /* soak the kill switch */ }

                    var qClient  = QueueClient.CreateFromConnectionString(NamespaceHelper.GetConnectionString(), QueueClient.FormatDeadLetterPath(typeof(T).GetQueueName()));
                    var rMessage = (await qClient.ReadBatchAsync <T>(1)).First();

                    rMessage.ShouldBeEquivalentTo(message);

                    MessageQueue.BrokeredMessages.Should().BeEmpty();
                    MessageQueue.LockTimers.Should().BeEmpty();
                }
        }
Esempio n. 8
0
    private static async Task SendBurstMemoryIsolation(int count)
    {
        await NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString()).ScorchNamespace();

        var messages = new List <TestMessage>();

        for (var i = 0; i < count; i++)
        {
            messages.Add(new TestMessage());
        }

        using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
        {
            foreach (var message in messages)
            {
                await msn.Send(message);
            }
        }

        messages.Clear(); // force List de-refence to improve the GC.Collect
    }
Esempio n. 9
0
    private static async Task SendBurst(int count)
    {
        await NamespaceManager.CreateFromConnectionString(NamespaceHelper.GetConnectionString()).ScorchNamespace();

        var messages = new List <TestMessage>();

        for (var i = 0; i < count; i++)
        {
            messages.Add(new TestMessage());
        }

        using (IMessenger msn = new Messenger(NamespaceHelper.GetConnectionString()))
        {
            // send one to get the queues created outside the collection
            await msn.Send(new TestMessage());

            StartJbProfilers(); // PROFILER START
            foreach (var message in messages)
            {
                await msn.Send(message);
            }
        }
    }