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)
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)
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")
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")
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()