Skip to content

Commit

Permalink
Minor version bump.
Browse files Browse the repository at this point in the history
 - Use `threading.Lock` over `multiprocessing.Lock`.
 - Add tests for thread-safety.
  • Loading branch information
hjpotter92 committed Feb 24, 2021
1 parent 73ad906 commit 01d593f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sonyflake/about.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict

MAJOR: int = 1
MINOR: int = 0
MINOR: int = 1
PATCH: int = 0
SUFFIX: str = ""

Expand Down
2 changes: 1 addition & 1 deletion sonyflake/sonyflake.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import ipaddress
from multiprocessing import Lock
from socket import gethostbyname, gethostname
from threading import Lock
from time import sleep
from typing import Callable, Dict, Optional

Expand Down
16 changes: 14 additions & 2 deletions tests/test_sonyflake.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import multiprocessing
from collections import Counter
import threading
from datetime import datetime, timedelta
from random import randint
from time import sleep
Expand Down Expand Up @@ -64,3 +63,16 @@ def test_sonyflake_for_10sec(self):
self.assertEqual(
max_sequence, (1 << BIT_LEN_SEQUENCE) - 1, "Unexpected max sequence"
)

def test_sonyflake_in_parallel(self):
threads = []
results = []
for _ in range(10000):
thread = threading.Thread(target=lambda: results.append(self.sf.next_id()))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
result_set = set(results)
self.assertEqual(len(results), len(result_set))
self.assertCountEqual(results, result_set)

0 comments on commit 01d593f

Please sign in to comment.