def test_process_bar_when_subscribers_then_sends_to_registered_handlers( self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.MID) bar_type = BarType(ETHUSDT_BINANCE.symbol, bar_spec, is_internal_aggregation=True) handler1 = ObjectStorer() subscribe1 = Subscribe( venue=BINANCE, data_type=Bar, metadata={"BarType": bar_type}, handler=handler1.store_2, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) handler2 = ObjectStorer() subscribe2 = Subscribe( venue=BINANCE, data_type=Bar, metadata={"BarType": bar_type}, handler=handler2.store_2, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) self.data_engine.execute(subscribe1) self.data_engine.execute(subscribe2) bar = Bar( Price("1051.00000"), Price("1055.00000"), Price("1050.00000"), Price("1052.00000"), Quantity(100), UNIX_EPOCH, ) data = BarData(bar_type, bar) # Act self.data_engine.process(data) # Assert self.assertEqual([(bar_type, bar)], handler1.get_store()) self.assertEqual([(bar_type, bar)], handler2.get_store())
def test_subscribe_bar_type_then_subscribes(self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.MID) bar_type = BarType(ETHUSDT_BINANCE.symbol, bar_spec, is_internal_aggregation=True) handler = ObjectStorer() subscribe = Subscribe( venue=BINANCE, data_type=Bar, metadata={"BarType": bar_type}, handler=handler.store_2, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) # Act self.data_engine.execute(subscribe) # Assert self.assertEqual([bar_type], self.data_engine.subscribed_bars)
def test_unsubscribe_trade_tick_then_unsubscribes(self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() handler = [] subscribe = Subscribe( venue=BINANCE, data_type=TradeTick, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=handler.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) self.data_engine.execute(subscribe) unsubscribe = Unsubscribe( venue=BINANCE, data_type=TradeTick, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=handler.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) # Act self.data_engine.execute(unsubscribe) # Assert self.assertEqual([], self.data_engine.subscribed_trade_ticks)
def test_process_trade_tick_when_subscriber_then_sends_to_registered_handler( self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() handler = [] subscribe = Subscribe( venue=BINANCE, data_type=TradeTick, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=handler.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) self.data_engine.execute(subscribe) tick = TradeTick( ETHUSDT_BINANCE.symbol, Price("1050.00000"), Quantity(100), OrderSide.BUY, TradeMatchId("123456789"), UNIX_EPOCH, ) # Act self.data_engine.process(tick) # Assert self.assertEqual([tick], handler)
def test_process_quote_tick_when_subscribers_then_sends_to_registered_handlers( self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() handler1 = [] subscribe1 = Subscribe( venue=BINANCE, data_type=QuoteTick, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=handler1.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) handler2 = [] subscribe2 = Subscribe( venue=BINANCE, data_type=QuoteTick, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=handler2.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) self.data_engine.execute(subscribe1) self.data_engine.execute(subscribe2) tick = QuoteTick( ETHUSDT_BINANCE.symbol, Price("100.003"), Price("100.003"), Quantity(1), Quantity(1), UNIX_EPOCH, ) # Act self.data_engine.process(tick) # Assert self.assertEqual([ETHUSDT_BINANCE.symbol], self.data_engine.subscribed_quote_ticks) self.assertEqual([tick], handler1) self.assertEqual([tick], handler2)
def test_process_instrument_when_subscribers_then_sends_to_registered_handlers( self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() handler1 = [] subscribe1 = Subscribe( venue=BINANCE, data_type=Instrument, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=handler1.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) handler2 = [] subscribe2 = Subscribe( venue=BINANCE, data_type=Instrument, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=handler2.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) self.data_engine.execute(subscribe1) self.data_engine.execute(subscribe2) # Act self.data_engine.process(ETHUSDT_BINANCE) # Assert self.assertEqual([ETHUSDT_BINANCE.symbol], self.data_engine.subscribed_instruments) self.assertEqual([ETHUSDT_BINANCE], handler1) self.assertEqual([ETHUSDT_BINANCE], handler2)
def test_command_serializer_methods_raise_not_implemented_error(self): # Arrange command = Subscribe( venue=Venue("SIM"), data_type=QuoteTick, metadata={}, handler=[].append, command_id=uuid4(), command_timestamp=UNIX_EPOCH, ) serializer = CommandSerializer() # Act # Assert self.assertRaises(NotImplementedError, serializer.serialize, command) self.assertRaises(NotImplementedError, serializer.deserialize, bytes())
def test_execute_subscribe_when_data_type_unrecognized_logs_and_does_nothing( self): # Arrange self.data_engine.register_client(self.binance_client) subscribe = Subscribe( venue=BINANCE, data_type=str, # str data type is invalid metadata={}, # Invalid anyway handler=[].append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) # Act self.data_engine.execute(subscribe) # Assert self.assertEqual(1, self.data_engine.command_count)
def test_execute_subscribe_instrument_then_adds_handler(self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() subscribe = Subscribe( venue=BINANCE, data_type=Instrument, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=[].append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) # Act self.data_engine.execute(subscribe) # Assert self.assertEqual([ETHUSDT_BINANCE.symbol], self.data_engine.subscribed_instruments)
def test_execute_subscribe_when_already_subscribed_does_not_add_and_logs( self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() subscribe = Subscribe( venue=BINANCE, data_type=QuoteTick, metadata={"Symbol": ETHUSDT_BINANCE.symbol}, handler=[].append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) # Act self.data_engine.execute(subscribe) self.data_engine.execute(subscribe) # Assert self.assertEqual(2, self.data_engine.command_count)
async def run_test(): # Arrange self.data_engine.start() subscribe = Subscribe( venue=BINANCE, data_type=QuoteTick, metadata={}, handler=[].append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) # Act self.data_engine.execute(subscribe) await asyncio.sleep(0.1) # Assert self.assertEqual(0, self.data_engine.message_qsize()) self.assertEqual(1, self.data_engine.command_count) # Tear Down self.data_engine.stop()