This repository has been archived on 2025-01-16. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2025-01-16/src/tests/test_randomtrackingdict.py
2024-05-26 23:38:41 +09:00

50 lines
1.2 KiB
Python

"""
Tests for RandomTrackingDict Class
"""
import random
import unittest
from time import time
class TestRandomTrackingDict(unittest.TestCase):
"""
Main protocol test case
"""
@staticmethod
def randString():
"""helper function for tests, generates a random string"""
retval = bytearray(32)
for i in range(32):
retval[i] = random.randint(0, 255)
return bytes(retval)
def test_check_randomtrackingdict(self):
"""Check the logic of RandomTrackingDict class"""
from pybitmessage.randomtrackingdict import RandomTrackingDict
a = []
k = RandomTrackingDict()
a.append(time())
for i in range(50000):
k[self.randString()] = True
a.append(time())
while k:
retval = k.randomKeys(1000)
if not retval:
self.fail("error getting random keys")
try:
k.randomKeys(100)
self.fail("bad")
except KeyError:
pass
for i in retval:
del k[i]
a.append(time())
for x in range(len(a) - 1):
self.assertLess(a[x + 1] - a[x], 10)