Skip to content

Commit

Permalink
update latency exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Jul 1, 2020
1 parent a7a5864 commit 9c484b0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
File renamed without changes.
36 changes: 26 additions & 10 deletions 006-latency-continued/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
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()
2 changes: 2 additions & 0 deletions 006-latency/bad_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 20 additions & 2 deletions 006-latency/network_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
27 changes: 23 additions & 4 deletions 006-latency/network_test_client2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import threading
import logging
import time

# Set up the logging
logging.basicConfig(
Expand All @@ -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")
logging.info("COMPLETE: %s", str(data))

0 comments on commit 9c484b0

Please sign in to comment.