예제 #1
0
 def test_sonyflake_epoch(self):
     sf = SonyFlake(start_time=SONYFLAKE_EPOCH)
     self.assertEqual(sf.start_time, 140952960000)
     next_id = sf.next_id()
     parts = SonyFlake.decompose(next_id)
     self.assertEqual(parts["msb"], 0)
     self.assertEqual(parts["machine_id"], self.machine_id)
     self.assertEqual(parts["sequence"], 0)
예제 #2
0
    def test_sonyflake_custom_machine_id(self):
        machine_id = randint(1, 255**2)

        def get_machine_id():
            return machine_id

        sf = SonyFlake(machine_id=get_machine_id)
        next_id = sf.next_id()
        parts = SonyFlake.decompose(next_id)
        self.assertEqual(parts["machine_id"], machine_id)
예제 #3
0
    def test_sonyflake_invalid_machine_id(self):
        def check_machine_id(_: int) -> bool:
            return False

        sonyflake = SonyFlake(check_machine_id=check_machine_id)
        self.assertIsNone(sonyflake, "Machine ID check failed")
예제 #4
0
 def test_sonyflake_future(self):
     future_start_time = datetime.utcnow() + timedelta(minutes=1)
     sonyflake = SonyFlake(start_time=future_start_time)
     self.assertIsNone(sonyflake, "SonyFlake starting in the future")
예제 #5
0
 def setUp(self):
     start_time = datetime.utcnow()
     self.sf = SonyFlake(start_time)
     self.start_time = SonyFlake.to_sonyflake_time(start_time)
     self.machine_id = lower_16bit_private_ip()