Ejemplo n.º 1
0
        // Ex 4: Create custom exception
        public void Ex4_CustomException()
        {
            string basepath = @"..\..\..\TmpDat";
            string filepath = Path.Combine(basepath, "OrderProcessingException.dat");

            // Serialize exception
            OrderProcessingException ope = new OrderProcessingException(7, "Serialize example");
            IFormatter formatter         = new BinaryFormatter();

            using (FileStream fs = new FileStream(filepath, FileMode.Create))
            {
                formatter.Serialize(fs, ope);
            }

            // Deserialize exception
            using (FileStream fs = new FileStream(filepath, FileMode.Open))
            {
                OrderProcessingException opeDerialized = (OrderProcessingException)formatter.Deserialize(fs);
                Console.WriteLine("Exception: {0}", opeDerialized.OrderId);
            }
        }
        public void HandleSubmitOrderForProductionAsyncThrowsWhenOrderSubmissionThrowsAndTheRetryThrows()
        {
            var mockClock            = new Mock <IClock>();
            var mockSubmitter        = new Mock <IOrderSubmitter>();
            var mockSubmitPublisher  = new Mock <ICommandPublisher <SubmitOrderForProduction> >();
            var mockFailurePublisher = new Mock <ICommandPublisher <NotifyOfFatalFailure> >();
            var mockEventPublisher   = new Mock <IEventPublisher <EventBase> >();
            var mockLogger           = new Mock <ILogger>();
            var mockLifetimeScope    = new Mock <IDisposable>();
            var serializerSettings   = new JsonSerializerSettings();
            var serializer           = new JsonSerializer {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };
            var processorFunctions = new TestOrderSubmitterFunctions(serializer, new CommandRetryThresholds(1, 1, 1), mockClock.Object, mockSubmitter.Object, mockSubmitPublisher.Object, mockFailurePublisher.Object, mockEventPublisher.Object, mockLogger.Object, mockLifetimeScope.Object);
            var expectedException  = new OrderProcessingException();


            serializer.Converters.Add(new StringEnumConverter());
            serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            serializerSettings.Converters.Add(new StringEnumConverter());

            mockLogger
            .Setup(logger => logger.ForContext(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <bool>()))
            .Returns(mockLogger.Object);

            mockSubmitPublisher
            .Setup(publisher => publisher.PublishAsync(It.IsAny <SubmitOrderForProduction>(), It.Is <Instant?>(value => value.HasValue)))
            .Throws(expectedException);

            mockSubmitter
            .Setup(processor => processor.SubmitOrderForProductionAsync(It.IsAny <string>(),
                                                                        It.IsAny <string>(),
                                                                        It.IsAny <DependencyEmulation>(),
                                                                        It.IsAny <string>()))
            .ThrowsAsync(new ApplicationException());

            var command = new SubmitOrderForProduction
            {
                Id              = Guid.NewGuid(),
                CorrelationId   = "ABC",
                PartnerCode     = "Bob",
                OrderId         = "123",
                OccurredTimeUtc = new DateTime(2017, 12, 09, 9, 0, 0, DateTimeKind.Utc)
            };

            using (var memStream = new MemoryStream())
                using (var writer = new StreamWriter(memStream))
                    using (var jsonWriter = new JsonTextWriter(writer))
                    {
                        serializer.Serialize(jsonWriter, command);
                        jsonWriter.Flush();

                        memStream.Seek(0, SeekOrigin.Begin);

                        using (var message = new BrokeredMessage(memStream))
                        {
                            message.ContentType   = MimeTypes.Json;
                            message.CorrelationId = Guid.NewGuid().ToString();
                            message.MessageId     = Guid.NewGuid().ToString();

                            Action actionUnderTest = () => processorFunctions.HandleSubmitOrderForProductionAsync(message).GetAwaiter().GetResult();

                            actionUnderTest.ShouldThrow <OrderProcessingException>("because the command retry publishing experienced an exception")
                            .Subject.SingleOrDefault().Should().Be(expectedException, "because the exception should bubble");
                        }

                        jsonWriter.Close();
                        writer.Close();
                        memStream.Close();
                    }
        }
        public void HandleSubmitOrderForProductionAsyncThrowsWhenOrderSubmissionFailsAndIsNotRetried()
        {
            var mockClock            = new Mock <IClock>();
            var mockSubmitter        = new Mock <IOrderSubmitter>();
            var mockSubmitPublisher  = new Mock <ICommandPublisher <SubmitOrderForProduction> >();
            var mockFailurePublisher = new Mock <ICommandPublisher <NotifyOfFatalFailure> >();
            var mockEventPublisher   = new Mock <IEventPublisher <EventBase> >();
            var mockLogger           = new Mock <ILogger>();
            var mockLifetimeScope    = new Mock <IDisposable>();
            var serializerSettings   = new JsonSerializerSettings();
            var serializer           = new JsonSerializer {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };
            var processorFunctions = new TestOrderSubmitterFunctions(serializer, new CommandRetryThresholds(-1, 0, 0), mockClock.Object, mockSubmitter.Object, mockSubmitPublisher.Object, mockFailurePublisher.Object, mockEventPublisher.Object, mockLogger.Object, mockLifetimeScope.Object);
            var expectedException  = new OrderProcessingException();


            serializer.Converters.Add(new StringEnumConverter());
            serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            serializerSettings.Converters.Add(new StringEnumConverter());

            mockLogger
            .Setup(logger => logger.ForContext(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <bool>()))
            .Returns(mockLogger.Object);

            mockSubmitter
            .Setup(processor => processor.SubmitOrderForProductionAsync(It.IsAny <string>(),
                                                                        It.IsAny <string>(),
                                                                        It.IsAny <DependencyEmulation>(),
                                                                        It.IsAny <string>()))
            .ReturnsAsync(OperationResult.ExceptionResult);

            var command = new SubmitOrderForProduction
            {
                Id              = Guid.NewGuid(),
                CorrelationId   = "ABC",
                PartnerCode     = "Bob",
                OrderId         = "123",
                OccurredTimeUtc = new DateTime(2017, 12, 09, 9, 0, 0, DateTimeKind.Utc)
            };

            using (var memStream = new MemoryStream())
                using (var writer = new StreamWriter(memStream))
                    using (var jsonWriter = new JsonTextWriter(writer))
                    {
                        serializer.Serialize(jsonWriter, command);
                        jsonWriter.Flush();

                        memStream.Seek(0, SeekOrigin.Begin);

                        using (var message = new BrokeredMessage(memStream))
                        {
                            message.ContentType   = MimeTypes.Json;
                            message.CorrelationId = Guid.NewGuid().ToString();
                            message.MessageId     = Guid.NewGuid().ToString();

                            Action actionUnderTest = () => processorFunctions.HandleSubmitOrderForProductionAsync(message).GetAwaiter().GetResult();

                            actionUnderTest.ShouldThrow <FailedtoHandleCommandException>("because the order submitter failed to submit the order");
                        }

                        jsonWriter.Close();
                        writer.Close();
                        memStream.Close();
                    };

            mockFailurePublisher.Verify(publisher => publisher.TryPublishAsync(It.IsAny <NotifyOfFatalFailure>(), It.Is <Instant?>(value => value == null)),
                                        Times.Once,
                                        "The failure notification should have been published because the command wasn't eligible for retries");
        }