diff --git a/006-latency-continued/latency/MANIFEST.in b/006-latency-continued/MANIFEST.in similarity index 100% rename from 006-latency-continued/latency/MANIFEST.in rename to 006-latency-continued/MANIFEST.in diff --git a/006-latency-continued/tests/test_runner.py b/006-latency-continued/tests/test_runner.py index aebf6c1..7264b30 100644 --- a/006-latency-continued/tests/test_runner.py +++ b/006-latency-continued/tests/test_runner.py @@ -39,17 +39,33 @@ def test_runner_can_generate_callers(config, run_cb): # Don't generally need (or should) test private methods - they're internal concerns, # but should test combined functionality - with patch('threading.Thread') as thread_mock, patch('threading.Lock') as thread_lock: - callers = runner.generate_callers(caller_indices, run_cb, create_client, results) + # with patch('threading.Thread') as thread_mock, patch('threading.Lock') as thread_lock: + callers = runner.generate_callers(caller_indices, run_cb, create_client, results) assert type(callers) is dict - assert thread_mock.call_count == len(caller_indices) - # Could be more precise and check exact calls all match - thread_mock.assert_called_with( - target=runner.run_caller, - args=(run_cb, create_client, thread_lock(), results, config) - ) - # note the scoping surprise above... (lock) + assert all([type(identifier) is str for identifier in callers]) - # ...but may be better to let the real threads run with fake functions... \ No newline at end of file + for idx, thread in callers.items(): + thread.index = idx + thread.start() + thread.join() + + +def test_runner_threads_require_an_index(config, run_cb): + runner = ParallelRunner(config) + + results = [] + + # Ensure we have our identifiers as strings, as these will be treated as names. + caller_indices = ['1', '2', '3', 'test'] + create_client = lambda config: None + + # Don't generally need (or should) test private methods - they're internal concerns, + # but should test combined functionality + # with patch('threading.Thread') as thread_mock, patch('threading.Lock') as thread_lock: + callers = runner.generate_callers(caller_indices, run_cb, create_client, results) + + for thread in callers.values(): + thread.start() + thread.join() \ No newline at end of file diff --git a/006-latency/bad_python.py b/006-latency/bad_python.py index 0f18785..6510391 100644 --- a/006-latency/bad_python.py +++ b/006-latency/bad_python.py @@ -11,7 +11,9 @@ def a(t, b): global n n += 1 + value = t(b) print(str(n).strip()+": ",t(b)) + def print(a,something, close=0): global STDOUT if STDOUT is None: diff --git a/006-latency/network_test_client.py b/006-latency/network_test_client.py index 4f291b1..2e39155 100644 --- a/006-latency/network_test_client.py +++ b/006-latency/network_test_client.py @@ -24,7 +24,25 @@ repeats = 100 # Keep this <= 100, please! timeout = 5 # Number of seconds until giving up on connection +def round_trip(skt): + payload = os.urandom(1024) -# IN HERE WE WILL WRITE THE NETWORK LATENCY CODE + skt.sendall(payload) + received_payload = skt.recv(1024) -logger.info("Average time taken: {delay} ms".format(delay=average_return_time)) + if received_payload != payload: + raise IOError("We received an incorrect echo") + +try: + with socket.create_connection(address=(host, port), timeout=timeout) as skt: + logger.info("Created connection") + round_trip(skt) + logger.info("Completed trial") +except ConnectionRefusedError as e: + logger.error( + "We could not create a socket connection to the " + "remote echo server" + ) + raise e + +# logger.info("Average time taken: {delay} ms".format(delay=average_return_time)) diff --git a/006-latency/network_test_client2.py b/006-latency/network_test_client2.py index b645fbe..afd76f6 100644 --- a/006-latency/network_test_client2.py +++ b/006-latency/network_test_client2.py @@ -7,6 +7,7 @@ import threading import logging +import time # Set up the logging logging.basicConfig( @@ -17,11 +18,29 @@ thread_count = 10 # Keep this <= 10, please! +data = [] + +lock = threading.Lock() # Our in-thread routine def run(): - pass - -# THREADING CONTROL CODE HERE + current_thread = threading.currentThread() + + time.sleep(0.3) + + with lock: + data.append(current_thread.index) + + logging.info("Hi, my name is %s and %s", current_thread.getName(), current_thread.index) + +thread_indices = range(1, thread_count + 1) +threads = {i: threading.Thread(target=run) for i in thread_indices} + +for idx, thread in threads.items(): + thread.index = idx + thread.start() + +for thread in threads.values(): + thread.join() -logging.info("COMPLETE") \ No newline at end of file +logging.info("COMPLETE: %s", str(data)) \ No newline at end of file