Skip to content

Commit 01d593f

Browse files
committed
Minor version bump.
- Use `threading.Lock` over `multiprocessing.Lock`. - Add tests for thread-safety.
1 parent 73ad906 commit 01d593f

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

sonyflake/about.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Dict
22

33
MAJOR: int = 1
4-
MINOR: int = 0
4+
MINOR: int = 1
55
PATCH: int = 0
66
SUFFIX: str = ""
77

sonyflake/sonyflake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
import ipaddress
3-
from multiprocessing import Lock
43
from socket import gethostbyname, gethostname
4+
from threading import Lock
55
from time import sleep
66
from typing import Callable, Dict, Optional
77

tests/test_sonyflake.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import multiprocessing
2-
from collections import Counter
1+
import threading
32
from datetime import datetime, timedelta
43
from random import randint
54
from time import sleep
@@ -64,3 +63,16 @@ def test_sonyflake_for_10sec(self):
6463
self.assertEqual(
6564
max_sequence, (1 << BIT_LEN_SEQUENCE) - 1, "Unexpected max sequence"
6665
)
66+
67+
def test_sonyflake_in_parallel(self):
68+
threads = []
69+
results = []
70+
for _ in range(10000):
71+
thread = threading.Thread(target=lambda: results.append(self.sf.next_id()))
72+
thread.start()
73+
threads.append(thread)
74+
for thread in threads:
75+
thread.join()
76+
result_set = set(results)
77+
self.assertEqual(len(results), len(result_set))
78+
self.assertCountEqual(results, result_set)

0 commit comments

Comments
 (0)