public async Task TestSequentialFailures()
        {
            var setup = GivenSetup(shortTimeout: true);

            using (var a = new TestExecutionContext.IsolatedContext())
            {
                var firstMessageProcessed = setup.Awaiter.WaitForMessage(m => m.Id == 2);

                setup.PublishMessage("{ id: 1, bar: \"baz\" }");

                Assert.ThrowsAsync <AssertionException>(async() => await firstMessageProcessed);

                await setup.Verify(mock => mock.Verify(r => r.BasicNack(1ul, true)));

                var secondMessageProcessed = setup.Awaiter.WaitForMessage(m => m.Id == 3);

                Assert.ThrowsAsync <AssertionException>(async() => await secondMessageProcessed);
            }

            await setup.Verify(mock =>
            {
                mock.Verify(r => r.BasicNack(1ul, It.IsAny <bool>()), Times.Once);

                mock.Verify(r => r.StartConsumer(It.IsAny <string>(), It.IsAny <Action <ulong, string> >(), It.IsAny <Action>()), Times.Exactly(2));
                mock.Verify(r => r.BasicCancel(It.IsAny <string>()), Times.Exactly(2));
            });

            setup.ProcessedMessages.IsTrue(m => m.Count == 0);
        }
示例#2
0
        private static void DoTest(IFormatter formatter, ISurrogateSelector surrogate, object obj, bool throwError, bool forWriting, bool forReading)
        {
            Console.Write($"{obj} by {formatter.GetType().Name}: ");
            formatter.SurrogateSelector = forWriting ? surrogate : null;
            TestExecutionContext.IsolatedContext context = throwError ? null : new TestExecutionContext.IsolatedContext();

            try
            {
                using (var ms = new MemoryStream())
                {
                    try
                    {
                        formatter.Serialize(ms, obj);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Serialization failed: {e}");
                        if (throwError)
                            throw;
                        return;
                    }

                    Console.WriteLine($"{ms.Length} bytes.");
                    if (dumpSerContent)
#pragma warning disable 162
                        Console.WriteLine(ms.ToArray().ToRawString());
#pragma warning restore 162

                    formatter.SurrogateSelector = forReading ? surrogate : null;
                    ms.Position = 0L;
                    try
                    {
                        object result = formatter.Deserialize(ms);
                        AssertDeepEquals(obj, result);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Deserialization failed: {e}");
                        if (throwError)
                            throw;
                    }
                }
            }
            finally
            {
                context?.Dispose();
            }
        }
示例#3
0
        protected static void RunMultipleTimes(Action action, int retryCount, TimeSpan standOff)
        {
            var isolatedContext = new TestExecutionContext.IsolatedContext();

            try
            {
                Policy.Handle <AssertionException>().WaitAndRetry(retryCount, i => standOff, (ex, ts, rc, c) =>
                {
                    isolatedContext.Dispose();
                    isolatedContext = new TestExecutionContext.IsolatedContext();
                    Console.WriteLine($"  Assert failed: {ex.Message}\r\n  Retrying assert after delay of {standOff.TotalSeconds}s...\r\n");
                    Task.Delay(standOff).GetAwaiter().GetResult();
                }).Execute(action);
            }
            finally
            {
                isolatedContext.Dispose();
            }
        }