public void ShouldApplyEvent()
        {
            var ccyPair = new CurrencyPair()
            {
                Ask    = 0.1,
                Bid    = 0.1,
                Mid    = 0.1,
                Spread = 0.02,
                State  = CcyPairState.Active,
                Id     = "EUR/USD"
            };

            var changeStateClose = new ChangeCcyPairState()
            {
                State = CcyPairState.Passive
            };

            ccyPair.Apply(changeStateClose);

            Assert.AreEqual(CcyPairState.Passive, ccyPair.State);
            Assert.AreEqual(1, ccyPair.AppliedEvents.Count());

            var changeStateOpen = new ChangeCcyPairState()
            {
                State = CcyPairState.Active
            };

            ccyPair.Apply(changeStateOpen as IEvent);

            Assert.AreEqual(CcyPairState.Active, ccyPair.State);
            Assert.AreEqual(2, ccyPair.AppliedEvents.Count());
        }
Exemplo n.º 2
0
        public void ShouldSerializeEventSubject()
        {
            var changeCcyPairState = new ChangeCcyPairState("test", "FxConnect", CcyPairState.Active);

            var subject = _eventSerializer.GetSubject(changeCcyPairState);

            Assert.AreEqual("test.Active.FxConnect", subject);

            changeCcyPairState = new ChangeCcyPairState("test", null, CcyPairState.Passive);

            subject = _eventSerializer.GetSubject(changeCcyPairState);
            Assert.AreEqual("test.Passive", subject);

            var changeCcyPairPrice = new ChangeCcyPairPrice(
                ccyPairId: "test",
                market: "market",
                ask: 0.1,
                bid: 0.1,
                mid: 0.1,
                spread: 0.02
                );

            subject = _eventSerializer.GetSubject(changeCcyPairPrice);
            Assert.AreEqual("test.market", subject);
        }
Exemplo n.º 3
0
        public async Task ShouldApplyMultipleEvents()
        {
            using (var publisherSocket = new PublisherSocket())
            {
                publisherSocket.Bind(ToSubscribersEndpoint);

                var streamId = "EUR/USD";

                var createEvent = new Func <IEvent <String, CurrencyPair>, Task>(async(@event) =>
                {
                    var message = _eventSerializer.ToProducerMessage(@event);

                    var eventId = new EventId(@event.EventStreamId, 0, @event.Subject, DateTime.Now.Ticks);

                    publisherSocket.SendMoreFrame(message.Subject)
                    .SendMoreFrame(_serializer.Serialize(eventId))
                    .SendFrame(_serializer.Serialize(message));

                    await Task.Delay(200);
                });

                var cacheConfiguration = new DynamicCacheConfiguration(ToSubscribersEndpoint, StateOfTheWorldEndpoint, HeartbeatEndpoint)
                {
                    Subject          = string.Empty,
                    HeartbeatDelay   = TimeSpan.FromSeconds(1),
                    HeartbeatTimeout = TimeSpan.FromSeconds(1),
                };

                var cache = new DynamicCache <string, CurrencyPair>(cacheConfiguration, LoggerForTests <DynamicCache <string, CurrencyPair> > .Default(), _eventSerializer);

                await cache.Run();

                await Task.Delay(2000);

                var priceEvent = new ChangeCcyPairPrice(streamId, "FxConnect", 0.0, 0.0, 0.0, 0.0);

                await createEvent(priceEvent);

                var changeStateClose = new ChangeCcyPairState(streamId, "FxConnect", CcyPairState.Passive);

                await createEvent(changeStateClose);

                Assert.AreEqual(1, cache.Items.Count());
                Assert.AreEqual(CcyPairState.Passive, cache.Items.First().State);
                Assert.AreEqual(2, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count());

                await cache.Destroy();
            }
        }
Exemplo n.º 4
0
        public async Task ShouldHandleStateOfTheWorldRequest()
        {
            using (var publisher = new PublisherSocket())
            {
                publisher.Connect(ToPublishersEndpoint);

                var command1 = new ChangeCcyPairState("EUR/USD", "TEST2", CcyPairState.Passive);
                var command2 = new ChangeCcyPairState("EUR/USD", "TEST2", CcyPairState.Active);

                var message1 = _eventSerializer.ToProducerMessage(command1);
                var message2 = _eventSerializer.ToProducerMessage(command2);

                await Task.Delay(200);

                publisher.SendMoreFrame(message1.Subject)
                .SendFrame(_eventSerializer.Serializer.Serialize(message1));

                await Task.Delay(200);

                publisher.SendMoreFrame(message2.Subject)
                .SendFrame(_eventSerializer.Serializer.Serialize(message2));

                await Task.Delay(200);

                var cacheItems = await _eventCache.GetAllStreams();

                Assert.AreEqual(2, cacheItems.Count());

                using (var dealer = new DealerSocket())
                {
                    var request = new StateRequest()
                    {
                        Subject = "EUR/USD"
                    };

                    var requestBytes = _eventSerializer.Serializer.Serialize(request);

                    dealer.Connect(StateOfTheWorldEndpoint);
                    dealer.SendFrame(requestBytes);

                    var hasResponse = dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(1000), out var responseBytes);

                    var response = _eventSerializer.Serializer.Deserialize <StateReply>(responseBytes);

                    Assert.AreEqual(2, response.Events.Count());
                }
            }
        }
        public void ShouldSerializeEventSubject()
        {
            var serializer      = new JsonNetSerializer();
            var eventSerializer = new EventSerializer(serializer);

            var changeCcyPairState = new ChangeCcyPairState()
            {
                EventStreamId = "test",
                State         = CcyPairState.Active,
                Market        = "FxConnect"
            };

            var subject = eventSerializer.GetSubject(changeCcyPairState);

            Assert.AreEqual("test.Active.FxConnect", subject);

            changeCcyPairState = new ChangeCcyPairState()
            {
                EventStreamId = "test",
                State         = CcyPairState.Passive,
            };

            subject = eventSerializer.GetSubject(changeCcyPairState);
            Assert.AreEqual("test.Passive.*", subject);

            var changeCcyPairPrice = new ChangeCcyPairPrice(
                ccyPairId: "test",
                market: "market",
                ask: 0.1,
                bid: 0.1,
                mid: 0.1,
                spread: 0.02
                );

            subject = eventSerializer.GetSubject(changeCcyPairPrice);
            Assert.AreEqual("test.market", subject);
        }
Exemplo n.º 6
0
        public async Task ShouldHandleIncomingEvent()
        {
            using (var publisher = new PublisherSocket())
            {
                publisher.Connect(ToPublishersEndpoint);

                using (var subscriber = new SubscriberSocket())
                {
                    subscriber.Connect(ToSubscribersEndpoint);
                    subscriber.SubscribeToAnyTopic();

                    var command = new ChangeCcyPairState("EUR/USD", "TEST", CcyPairState.Passive);
                    var message = _eventSerializer.ToProducerMessage(command);

                    await Task.Delay(200);

                    publisher.SendMoreFrame(message.Subject)
                    .SendFrame(_eventSerializer.Serializer.Serialize(message));

                    NetMQMessage msg = null;

                    var hasResponse = subscriber.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(1000), ref msg);

                    Assert.IsTrue(hasResponse);

                    var eventIdBytes      = msg[1].Buffer;
                    var eventMessageBytes = msg[2].Buffer;

                    var eventId = _eventSerializer.Serializer.Deserialize <EventId>(eventIdBytes);

                    Assert.AreEqual("EUR/USD", eventId.EventStream);
                    Assert.AreEqual("EUR/USD.0", eventId.Id);
                    Assert.AreEqual(0, eventId.Version);
                    Assert.AreEqual(message.Subject, eventId.Subject);

                    var producerMessage = _eventSerializer.Serializer.Deserialize <ProducerMessage>(eventMessageBytes);

                    var @event = _serializer.Deserialize <ChangeCcyPairState>(producerMessage.MessageBytes);

                    Assert.AreEqual(message.Subject, producerMessage.Subject);
                    Assert.AreEqual(typeof(ChangeCcyPairState), producerMessage.MessageType);

                    Assert.AreEqual(command.EventStreamId, @event.EventStreamId);
                    Assert.AreEqual(command.Market, @event.Market);
                    Assert.AreEqual(command.State, @event.State);

                    command = new ChangeCcyPairState("EUR/USD", "TEST", CcyPairState.Active);
                    message = _eventSerializer.ToProducerMessage(command);

                    await Task.Delay(200);

                    publisher.SendMoreFrame(message.Subject)
                    .SendFrame(_eventSerializer.Serializer.Serialize(message));

                    await Task.Delay(50);

                    hasResponse = subscriber.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(1000), ref msg);

                    Assert.IsTrue(hasResponse);

                    eventIdBytes      = msg[1].Buffer;
                    eventMessageBytes = msg[2].Buffer;

                    eventId = _eventSerializer.Serializer.Deserialize <EventId>(eventIdBytes);

                    Assert.AreEqual("EUR/USD", eventId.EventStream);
                    Assert.AreEqual("EUR/USD.1", eventId.Id);
                    Assert.AreEqual(1, eventId.Version);
                    Assert.AreEqual(message.Subject, eventId.Subject);
                }
            }
        }