Complete the code to simulate a network delay of 2 seconds in a test.
def test_network_delay(): import time time.[1](2) assert True
The time.sleep() function pauses the program for the given number of seconds, simulating network delay.
Complete the code to check if the network is reachable by pinging 'example.com'.
import subprocess def is_network_reachable(): response = subprocess.run(['ping', '-c', '1', '[1]'], capture_output=True) return response.returncode == 0
Using 'example.com' as the target host checks general network reachability.
Fix the error in the code to simulate packet loss by dropping 30% of packets in a test.
import random def simulate_packet_loss(): if random.random() [1] 0.3: return 'Packet dropped' else: return 'Packet sent'
Using < 0.3 means 30% chance to drop the packet, as random.random() returns a float between 0 and 1.
Fill both blanks to create a test that retries a network request up to 3 times if it fails.
def retry_request(): attempts = 0 while attempts [1] 3: success = make_request() if success: return True attempts [2] 1 return False
The loop runs while attempts is less than 3, and attempts is incremented by 1 each time using +=.
Fill all three blanks to create a dictionary comprehension that maps URLs to their response times if response time is less than 500ms.
response_times = {url[1]: time for url, time in results.items() if time [2] 500 and url [3] 'http://example.com'}The URL keys are converted to lowercase with .lower(), times less than 500 are filtered with <, and URLs not equal to 'http://example.com' are included with !=.