Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def mailbox_tester(messages,
numbers=None,
reader_sleeps=0.,
max_messages=100,
expected_result=None,
timeout=SHORT_TIMEOUT,
result_timeout=LONG_TIMEOUT):
if numbers is None:
numbers = np.arange(len(messages))
if expected_result is None:
messages = np.asarray(messages)
expected_result = messages[np.argsort(numbers)]
mb = strax.Mailbox(max_messages=max_messages, timeout=timeout)
n_readers = 2
with concurrent.futures.ThreadPoolExecutor() as tp:
futures = [tp.submit(reader,
source=mb.subscribe(),
reader_sleeps=reader_sleeps)
for _ in range(n_readers)]
for i in range(len(messages)):
mb.send(messages[i], msg_number=numbers[i])
print(f"Sent message {i}. Now {len(mb._mailbox)} ms in mailbox.")
mb.close()
# Results must be equal
def test_deadlock_regression():
"""A reader thread may start after the first message is processed"""
mb = strax.Mailbox(timeout=SHORT_TIMEOUT)
mb.send(0)
readers = [
threading.Thread(target=reader,
kwargs=dict(
source=mb.subscribe(),
name=str(i)))
for i in range(2)
]
readers[0].start()
time.sleep(SHORT_TIMEOUT)
readers[1].start()
mb.send(1)
mb.close()
def test_highlevel():
"""Test highlevel mailbox API"""
mb = strax.Mailbox()
mb.add_sender(range(10))
def test_reader(source):
test_reader.got = r = []
for s in source:
r.append(s)
mb.add_reader(test_reader)
mb.start()
time.sleep(SHORT_TIMEOUT)
assert hasattr(test_reader, 'got')
assert test_reader.got == list(range(10))
mb.cleanup()
assert len(threading.enumerate()) == 1, "Not all threads died"
def test_processing():
"""Test ParallelSource plugin under several conditions"""
# It's always harder with a small mailbox:
strax.Mailbox.DEFAULT_MAX_MESSAGES = 2
for request_peaks in (True, False):
for peaks_parallel in (True, False):
for max_workers in (1, 2):
Peaks.parallel = peaks_parallel
print(f"\nTesting with request_peaks {request_peaks}, "
f"peaks_parallel {peaks_parallel}, "
f"max_workers {max_workers}")
mystrax = strax.Context(storage=[],
register=[Records, Peaks])
bla = mystrax.get_array(
run_id=run_id,
targets='peaks' if request_peaks else 'records',
max_workers=max_workers)
assert len(bla) == recs_per_chunk * n_chunks
assert bla.dtype == (
def test_valid_msg_number():
"""Message numbers are non-negative integers"""
mb = strax.Mailbox()
with pytest.raises(strax.InvalidMessageNumber):
mb.send(0, msg_number=-1)
with pytest.raises(strax.InvalidMessageNumber):
mb.send(0, msg_number='???')
def __missing__(self, key):
res = self[key] = strax.Mailbox(name=key + '_mailbox',
lazy=self.lazy)
return res