2024-01-23 01:46:00 +00:00
|
|
|
from datetime import datetime
|
2024-02-05 08:04:02 +00:00
|
|
|
from unittest import IsolatedAsyncioTestCase
|
2024-01-23 01:46:00 +00:00
|
|
|
|
|
|
|
import numpy
|
|
|
|
|
2024-02-05 08:04:02 +00:00
|
|
|
from mixnet.client import MixClient
|
2024-01-23 01:46:00 +00:00
|
|
|
from mixnet.poisson import poisson_mean_interval_sec
|
2024-02-05 08:04:02 +00:00
|
|
|
from mixnet.test_utils import (
|
2024-02-08 06:39:50 +00:00
|
|
|
init_mixnet_config,
|
2024-02-05 08:04:02 +00:00
|
|
|
with_test_timeout,
|
|
|
|
)
|
2024-02-05 06:47:36 +00:00
|
|
|
from mixnet.utils import random_bytes
|
2024-01-23 01:46:00 +00:00
|
|
|
|
|
|
|
|
2024-02-05 08:04:02 +00:00
|
|
|
class TestMixClient(IsolatedAsyncioTestCase):
|
2024-01-25 09:04:55 +00:00
|
|
|
@with_test_timeout(100)
|
2024-02-05 08:04:02 +00:00
|
|
|
async def test_mixclient(self):
|
2024-02-08 06:39:50 +00:00
|
|
|
config = init_mixnet_config().mixclient_config
|
2024-02-05 08:04:02 +00:00
|
|
|
config.emission_rate_per_min = 30
|
|
|
|
config.redundancy = 3
|
|
|
|
|
|
|
|
mixclient = await MixClient.new(config)
|
|
|
|
try:
|
|
|
|
# Send a 3500-byte msg, expecting that it is split into at least two packets
|
|
|
|
await mixclient.send_message(random_bytes(3500))
|
|
|
|
|
|
|
|
# Calculate intervals between packet emissions from the mix client
|
|
|
|
intervals = []
|
|
|
|
ts = datetime.now()
|
|
|
|
for _ in range(30):
|
|
|
|
_ = await mixclient.outbound_socket.get()
|
|
|
|
now = datetime.now()
|
|
|
|
intervals.append((now - ts).total_seconds())
|
|
|
|
ts = now
|
|
|
|
|
|
|
|
# Check if packets were emitted at the Poisson emission_rate
|
|
|
|
# If emissions follow the Poisson distribution with a rate `lambda`,
|
|
|
|
# a mean interval between emissions must be `1/lambda`.
|
|
|
|
self.assertAlmostEqual(
|
|
|
|
float(numpy.mean(intervals)),
|
|
|
|
poisson_mean_interval_sec(config.emission_rate_per_min),
|
|
|
|
delta=1.0,
|
2024-01-25 09:04:55 +00:00
|
|
|
)
|
2024-02-05 08:04:02 +00:00
|
|
|
finally:
|
|
|
|
await mixclient.cancel()
|