Ejemplo n.º 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)
Ejemplo n.º 2
0
 def test_sonyflake_once(self):
     sleep_time = randint(1, 50)
     self._sleep(sleep_time)
     next_id = self.sf.next_id()
     parts = SonyFlake.decompose(next_id)
     self.assertEqual(parts["msb"], 0)
     self.assertTrue(sleep_time <= parts["time"] <= sleep_time + 1)
     self.assertEqual(parts["sequence"], 0)
     self.assertEqual(parts["machine_id"], self.machine_id)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
 def test_sonyflake_for_10sec(self):
     last_id = 0
     current = initial = self._current_time()
     max_sequence = 0
     while (current - initial) < 1000:
         next_id = self.sf.next_id()
         parts = SonyFlake.decompose(next_id)
         self.assertLess(last_id, next_id, "Duplicated id")
         current = self._current_time()
         last_id = next_id
         self.assertEqual(parts["msb"], 0)
         self.assertEqual(parts["machine_id"], self.machine_id)
         overtime = self.start_time + (parts["time"] - current)
         self.assertLessEqual(overtime, 0, "Unexpected overtime.")
         max_sequence = max(max_sequence, parts["sequence"])
     self.assertEqual(max_sequence, (1 << BIT_LEN_SEQUENCE) - 1,
                      "Unexpected max sequence")