def test_timed_out(self): # Test for timed out self.assertTrue( self.timeout1.is_timed_out( Timestamp(int(old_round(time.time() * 1000)) - 3700 * 1000))) self.assertFalse( self.timeout2.is_timed_out( Timestamp(int(old_round(time.time() * 1000)))))
def setUp(self): # Object creation self.transaction_id = TransactionId(b'a' * 32) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')), OrderId(TraderId(b'0' * 20), OrderNumber(2)), OrderId(TraderId(b'1' * 20), OrderNumber(1)), Timestamp(0)) self.proposed_trade = Trade.propose( TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(2)), OrderId(TraderId(b'1' * 20), OrderNumber(3)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')), Timestamp(0)) self.tick = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(5, 'BTC'), AssetAmount(5, 'MC')), Timeout(0), Timestamp(00), True) self.tick2 = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(500, 'BTC'), AssetAmount(5, 'MC')), Timeout(0), Timestamp(0), True) self.order_timestamp = Timestamp.now() self.order = Order( OrderId(TraderId(b'0' * 20), OrderNumber(3)), AssetPair(AssetAmount(50, 'BTC'), AssetAmount(40, 'MC')), Timeout(5000), self.order_timestamp, False) self.order.set_verified() self.order2 = Order( OrderId(TraderId(b'0' * 20), OrderNumber(4)), AssetPair(AssetAmount(50, 'BTC'), AssetAmount(10, 'MC')), Timeout(5), Timestamp(int(old_round(time.time() * 1000)) - 1000 * 1000), True) self.order2.set_verified()
def is_valid(self): """ :return: True if valid, False otherwise :rtype: bool """ return not self._timeout.is_timed_out(self._timestamp) and \ int(old_round(time.time() * 1000)) >= int(self.timestamp) - self.TIME_TOLERANCE and \ int(self._timeout) <= MAX_ORDER_TIMEOUT
def now(cls): """ Create a timestamp with the time set to the current time :return: A timestamp :rtype: Timestamp """ return cls(int(old_round(time.time() * 1000)))
def is_timed_out(self, timestamp): """ Return if a timeout has occurred :param timestamp: A timestamp :type timestamp: Timestamp :return: True if timeout has occurred, False otherwise :rtype: bool """ return int(old_round( time.time() * 1000)) - int(timestamp) >= self._timeout * 1000
def insert_bid(self, bid): """ :type bid: Bid """ if not self._bids.tick_exists(bid.order_id) and bid.order_id not in self.completed_orders and bid.is_valid(): self._bids.insert_tick(bid) timeout_delay = int(bid.timestamp) + int(bid.timeout) * 1000 - int(old_round(time.time() * 1000)) task = deferLater(reactor, timeout_delay, self.timeout_bid, bid.order_id) self.register_task("bid_%s_timeout" % bid.order_id, task) return task.addErrback(self.on_timeout_error) return fail(Failure(RuntimeError("bid invalid"))).addErrback(self.on_invalid_tick_insert)
def insert_ask(self, ask): """ :type ask: Ask """ if not self._asks.tick_exists(ask.order_id) and ask.order_id not in self.completed_orders and ask.is_valid(): self._asks.insert_tick(ask) timeout_delay = int(ask.timestamp) + int(ask.timeout) * 1000 - int(old_round(time.time() * 1000)) task = deferLater(reactor, timeout_delay, self.timeout_ask, ask.order_id) self.register_task("ask_%s_timeout" % ask.order_id, task) return task.addErrback(self.on_timeout_error) return fail(Failure(RuntimeError("ask invalid"))).addErrback(self.on_invalid_tick_insert)
def test_now(self): # Test for Timestamp.now self.assertAlmostEqual(int(old_round(time.time() * 1000)), int(Timestamp.now()), delta=1000)