public void BasicEventReplierTest()
        {
            const int port = 5566;

            //Create event replier
            using EventReplier <PingEvent, OkResponse> eventReplier = new(port, _ => new OkResponse());
            // ReSharper disable once AccessToDisposedClosure
            _ = Task.Run(() => eventReplier.HandleEventsLoop());

            //Setup test's zmq
            Utils.CreateZmq(ZSocketType.REQ, port, false, out ZContext context, out ZSocket socket);

            //Create ping event and serialize it
            byte[] okEventData = EventsSerializer.SerializeEvent(new PingEvent());

            //Send it
            socket.Send(okEventData);

            //Get the response back from the event replier
            byte[]     responseRawData  = socket.Receive();
            OkResponse responseResponse = EventsSerializer.DeserializeEvent <OkResponse>(responseRawData);

            Assert.IsNotNull(responseResponse);

            socket.Dispose();
            context.Dispose();
        }
        public void BasicUnionEventReplierTest()
        {
            const int port = 6677;

            using EventReplier <EngineActionEvent, EngineActionResponse> eventReplier = new(port, _ => new OkResponse());
            // ReSharper disable once AccessToDisposedClosure
            _ = Task.Run(() => eventReplier.HandleEventsLoop());

            //Setup test's zmq
            Utils.CreateZmq(ZSocketType.REQ, port, false, out ZContext context, out ZSocket socket);

            //Get the response
            byte[] okEventData = EventsSerializer.SerializeEvent <EngineActionEvent>(new PingEvent());
            socket.Send(okEventData);

            //Check the type
            byte[] responseRawData = socket.Receive();
            EngineActionResponse responseActionResponse = EventsSerializer.DeserializeEvent <EngineActionResponse>(responseRawData);

            Assert.IsNotNull(responseActionResponse);
            Assert.That(responseActionResponse.GetType(), Is.EqualTo(typeof(OkResponse)));

            socket.Dispose();
            context.Dispose();
        }
Пример #3
0
        public void Save(Guid id, IEnumerable <IEvent> events, int expectedVersion)
        {
            var lastStoredEvent = Events
                                  .Where(e => e.Id == id)
                                  .OrderByDescending(ev => ev.Version)
                                  .FirstOrDefault();
            var lastVersion = lastStoredEvent == null ? -1 : lastStoredEvent.Version;

            if (lastVersion != expectedVersion)
            {
                throw new ConcurrencyException();
            }
            foreach (var @event in events)
            {
                var serializedEvent = _eventsSerializer.SerializeEvent(@event);
                Events.Add(new EventDescriptor
                {
                    Version = @event.Version,
                    Id      = id,
                    Data    = serializedEvent.Data,
                    Type    = serializedEvent.Type
                });
                _bus.Send(@event);
            }
        }
Пример #4
0
        public void Save(AggregateRoot aggregateRoot, int expectedVersion)
        {
            var events          = aggregateRoot.GetUncommittedChanges();
            var lastStoredEvent = Events
                                  .Where(e => e.Id == aggregateRoot.Id)
                                  .OrderByDescending(ev => ev.Version)
                                  .FirstOrDefault();
            var lastVersion = lastStoredEvent == null ? -1 : lastStoredEvent.Version;

            if (lastVersion != expectedVersion)
            {
                throw new ConcurrencyException();
            }
            foreach (var @event in events)
            {
                var serializedEvent = _eventsSerializer.SerializeEvent(@event);
                lastVersion = @event.Version;
                Events.Add(new EventDescriptor
                {
                    Version = @event.Version,
                    Id      = aggregateRoot.Id,
                    Data    = serializedEvent.Data,
                    Type    = serializedEvent.Type
                });
                _bus.Send(@event);
            }
            var snapshottableAggregateRoot = aggregateRoot as SnapshottableAggregateRoot;

            if (snapshottableAggregateRoot != null && snapshottableAggregateRoot.ShouldCreateSnapshot())
            {
                var snapshot = snapshottableAggregateRoot.GetSnapshot();
                snapshot.Version = lastVersion;
                _snapshotStore.SaveSnapshot(snapshot);
            }
        }
        public void BasicUnionSerializationTest()
        {
            byte[] data = EventsSerializer.SerializeEvent <EngineActionEvent>(new PingEvent());

            EngineActionEvent engineEvent = EventsSerializer.DeserializeEvent <EngineActionEvent>(data);

            Assert.That(engineEvent.GetType(), Is.EqualTo(typeof(PingEvent)));
        }
Пример #6
0
        public void BasicEventDispatcherTest()
        {
            const int port = 8732;

            //Setup test's ZMQ
            Utils.CreateZmq(ZSocketType.REP, port, true, out ZContext context, out ZSocket socket);

            //Create the event dispatcher
            EventDispatcher <EngineActionEvent, EngineActionResponse> eventDispatcher = null;

            _ = Task.Run(() =>
            {
                eventDispatcher = new EventDispatcher <EngineActionEvent, EngineActionResponse>(new TimeSpan(0, 0, 0, 4), port);
                eventDispatcher.DispatchEventsThread().RunSynchronously();
            });
            SpinWait.SpinUntil(() => eventDispatcher != null);

            //Send the event
            bool gotResponse = false;

            eventDispatcher.QueueEvent(new PingEvent(), responseEventDispatcher =>
            {
                //We got a response
                gotResponse = true;
                Assert.IsNotNull(responseEventDispatcher);
                Assert.That(responseEventDispatcher.GetType(), Is.EqualTo(typeof(OkResponse)));
            });

            //Get a event from the dispatcher
            byte[]            requestData = socket.Receive();
            EngineActionEvent actionEvent = EventsSerializer.DeserializeEvent <EngineActionEvent>(requestData);

            Assert.IsNotNull(actionEvent);
            Assert.That(actionEvent.GetType(), Is.EqualTo(typeof(PingEvent)));

            //Respond
            EngineActionResponse response = new OkResponse();

            byte[] responseData = EventsSerializer.SerializeEvent <EngineActionResponse>(response);
            socket.Send(responseData);
            SpinWait.SpinUntil(() => gotResponse);

            eventDispatcher.Dispose();
            socket.Dispose();
            context.Dispose();
        }
        public void BasicSerializationTest()
        {
            byte[] data = EventsSerializer.SerializeEvent(new PingEvent());

            PingEvent pingEvent = EventsSerializer.DeserializeEvent <PingEvent>(data);
        }