public async Task ShouldPublishEvent2()
        {
            // arange
            string topic     = Guid.NewGuid().ToString();
            var    processor = new TestMessageProcessor();

            host.RegisterMessageProcessor(topic + "TestEvent", processor);

            var settings = publisterSettings.Copy();

            settings.Topic = topic;
            var publisher     = new DomainEventPublisher(settings, new NullLogger <DomainEventPublisher>());
            var correlationId = Guid.NewGuid().ToString();

            // act
            var @event = new TestEvent()
            {
                IntValue = random.Next(), StringValue = Guid.NewGuid().ToString()
            };
            await publisher.PublishAsync(@event, correlationId).ConfigureAwait(false);

            // assert
            Assert.Single(processor.Messages);
            var message = processor.Messages[0];

            Assert.Equal(@event.IntValue, JsonConvert.DeserializeObject <TestEvent>(message.GetBody <string>()).IntValue);
            Assert.Equal(@event.StringValue, JsonConvert.DeserializeObject <TestEvent>(message.GetBody <string>()).StringValue);
            Assert.Equal(@event.GetType().FullName, message.ApplicationProperties[Constants.MESSAGE_TYPE_KEY]);
            Assert.Equal(correlationId, message.Properties.CorrelationId);
        }
        public async Task ShouldScheduleEvent2()
        {
            // arrange
            string topic     = Guid.NewGuid().ToString();
            var    processor = new TestMessageProcessor();

            host.RegisterMessageProcessor(topic + "TestEvent", processor);

            var settings = publisterSettings.Copy();

            settings.Topic = topic;
            var publisher     = new DomainEventPublisher(settings, new NullLogger <DomainEventPublisher>());
            var correlationId = Guid.NewGuid().ToString();
            var scheduleDate  = DateTime.UtcNow.AddDays(1);

            // act
            var @event = new TestEvent()
            {
                IntValue = random.Next(), StringValue = Guid.NewGuid().ToString()
            };
            await publisher.ScheduleAsync <TestEvent>(@event, scheduleDate, correlationId).ConfigureAwait(false);

            // assert
            Assert.Single(processor.Messages);
            var message = processor.Messages[0];

            Assert.Equal(@event.IntValue, JsonConvert.DeserializeObject <TestEvent>(message.GetBody <string>()).IntValue);
            Assert.Equal(@event.StringValue, JsonConvert.DeserializeObject <TestEvent>(message.GetBody <string>()).StringValue);
            Assert.Equal(@event.GetType().FullName, message.ApplicationProperties[Constants.MESSAGE_TYPE_KEY]);
            Assert.Equal(correlationId, message.Properties.CorrelationId);
            ((DateTime)message.MessageAnnotations[new Symbol(Constants.SCHEDULED_ENQUEUE_TIME_UTC)]).Should().BeCloseTo(scheduleDate);
        }
        public async Task Test()
        {
            // Arrange
            await _persist.CreateAsync(TestStream, TestGroup, Settings);

            // Act
            await _persist.SubscribeAsync(TestStream, TestGroup, SubscribeReturn);

            var evt       = new TestEvent("I wrote my first event!");
            var eventData = new EventData(
                Uuid.NewUuid(),
                evt.GetType().ToString(),
                JsonSerializer.SerializeToUtf8Bytes(evt)
                );
            await _client.AppendToStreamAsync(
                TestStream,
                StreamState.StreamExists,
                new[] { eventData }
                );

            await Task.Delay(TimeSpan.FromSeconds(10));

            // Clean
            await _persist.DeleteAsync(TestStream, TestGroup);
        }
Ejemplo n.º 4
0
        public void AsType()
        {
            var e = new TestEvent(Guid.NewGuid().ToString())
            {
                Message = "test"
            }.AddTestMetaData <TestState>(new AggregateId("test"));
            var jsonNetEvent  = JsonConvert.SerializeObject(e);
            var corejsonEvent = System.Text.Json.JsonSerializer.Serialize(e, e.GetType());

            Assert.Equal(jsonNetEvent, corejsonEvent);
        }
Ejemplo n.º 5
0
        public void TestJsonIntermediateFormatDeserializer_SetActive()
        {
            /* PRECONDITION */
            Debug.Assert(inputQueue != null);
            Debug.Assert(outputQueue != null);
            Debug.Assert(deserializer != null);
            Debug.Assert(container != null);
            Debug.Assert(!deserializer.IsActive);

            /* GIVEN */
            // Load all MEF instances correctly
            container.ComposeExportedValue <ISupportDeserializationEventQueue <Event> >(outputQueue);
            container.ComposeExportedValue <IDecodableEventQueue <JsonIntermediateFormatSample> >(inputQueue);
            container.ComposeParts(deserializer);

            const int identifier = 404;
            var       @event     = new TestEvent(identifier);

            using var outputReceivedEvent = new ManualResetEvent(false);
            var sample = new JsonIntermediateFormatSample
            {
                Data      = JsonSerializer.SerializeToUtf8Bytes(@event),
                Timestamp = @event.Timestamp,
                Type      = @event.GetType()
            };

            /* WHEN */
            deserializer.IsActive = true;
            Assert.IsTrue(deserializer.IsActive, "Deserializer did not activate correctly!");

            ExpectOutput(outputQueue, (received) =>
            {
                if (received == null || !received.Equals(@event))
                {
                    return(false);
                }
                outputReceivedEvent.Set();
                return(true);
            });

            inputQueue.Enqueue(sample);

            /* THEN */
            Assert.IsTrue(outputReceivedEvent.WaitOne(maxWaitTime), "Did not receive serialized event in time!");
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            int runs      = Runs;
            int parseRuns = ParseRuns;

            if (args.Length > 0)
            {
                runs = int.Parse(args[0]);
            }
            if (args.Length > 1)
            {
                parseRuns = int.Parse(args[1]);
            }

            TestDelegate a = () =>
            {
                int i = 0;
            };

            Console.WriteLine("Interfaces implemented by delegates");

            var interfaces = a.GetType().GetInterfaces();

            foreach (var iface in interfaces)
            {
                Console.WriteLine(iface.Name);
            }
            Program.TestEvent += a;

            Console.WriteLine("Interfaces implemented by events");

            Console.WriteLine(TestEvent.GetType().Name);
            foreach (var iface in Program.TestEvent.GetType().GetInterfaces())
            {
                Console.WriteLine(iface.Name);
            }
            RunAsString(runs);
            AddFlags(runs);
            ParseCaseInsensitive(parseRuns);
            Program.Parse(parseRuns);
            //Console.ReadKey();
        }
Ejemplo n.º 7
0
        public void TestJsonIntermediateFormatDeserializer_DeactivatesOnClose()
        {
            /* PRECONDITION */
            Debug.Assert(inputQueue != null);
            Debug.Assert(outputQueue != null);
            Debug.Assert(deserializer != null);
            Debug.Assert(container != null);
            Debug.Assert(!deserializer.IsActive);

            /* GIVEN */
            // Load all MEF instances correctly
            container.ComposeExportedValue <ISupportDeserializationEventQueue <Event> >(outputQueue);
            container.ComposeExportedValue <IDecodableEventQueue <JsonIntermediateFormatSample> >(inputQueue);
            container.ComposeParts(deserializer);

            var @event = new TestEvent();
            var sample = new JsonIntermediateFormatSample
            {
                Data      = JsonSerializer.SerializeToUtf8Bytes(@event),
                Timestamp = @event.Timestamp,
                Type      = @event.GetType()
            };

            using var outputReceivedEvent = new ManualResetEvent(false);
            using var didCloseEvent       = new ManualResetEvent(false);

            /* WHEN */
            deserializer.IsActive = true;
            Assert.IsTrue(deserializer.IsActive, "Deserializer did not activate correctly!");

            ExpectOutput(outputQueue, (_) => outputReceivedEvent.Set(), () => didCloseEvent.Set());

            inputQueue.Enqueue(sample);
            Assert.IsTrue(outputReceivedEvent.WaitOne(maxWaitTime), "Did not receive serialized event in time!");

            inputQueue.Close();

            /* THEN */
            Assert.IsTrue(inputQueue.IsClosed, "InputQueue failed to close!");
            Assert.IsTrue(didCloseEvent.WaitOne(maxWaitTime), "Did not close output queue in time!");
        }
        public async Task ShouldPublishEvent1WithSerializationSettings()
        {
            // arrange
            string topic     = Guid.NewGuid().ToString();
            var    processor = new TestMessageProcessor();

            host.RegisterMessageProcessor(topic + "TestEvent", processor);

            var settings = publisterSettings.Copy();

            settings.Topic = topic;
            settings.SerializerSettings = new JsonSerializerSettings()
            {
                Converters = new List <JsonConverter> {
                    new StringEnumConverter(new CamelCaseNamingStrategy())
                }
            };
            var publisher = new DomainEventPublisher(settings, new NullLogger <DomainEventPublisher>());

            // act
            var @event = new TestEvent()
            {
                IntValue = random.Next(), StringValue = Guid.NewGuid().ToString(), Status = StatusEnum.PendingWork
            };
            await publisher.PublishAsync(@event).ConfigureAwait(false);

            // assert
            Assert.Single(processor.Messages);
            var message = processor.Messages[0];
            var body    = message.GetBody <string>();

            Assert.Contains("\"Status\":\"pendingWork\"", body);
            Assert.Equal(@event.IntValue, JsonConvert.DeserializeObject <TestEvent>(body).IntValue);
            Assert.Equal(@event.StringValue, JsonConvert.DeserializeObject <TestEvent>(body).StringValue);
            Assert.Equal(@event.Status, JsonConvert.DeserializeObject <TestEvent>(body).Status);
            Assert.Equal(@event.GetType().FullName, message.ApplicationProperties[Constants.MESSAGE_TYPE_KEY]);
        }
Ejemplo n.º 9
0
 public Task HandleAsync(TestEvent @event)
 {
     stringBuilder.Append($"{@event.GetType().Name}:{@event.Name} ");
     return(Task.CompletedTask);
 }