Skip to content

Commit

Permalink
fixup! Fix: Race condition in DockerContainerTestCase (#6587)
Browse files Browse the repository at this point in the history
  • Loading branch information
achave11-ucsc committed Sep 25, 2024
1 parent d8cc862 commit 5009f13
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions test/docker_container_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,30 @@ def _create_container(cls, image: str, container_port: int, **kwargs) -> Netloc:
try:
time_it = datetime.now()
container_info = cls._docker.api.inspect_container(container.id)
network_settings = container_info['NetworkSettings']
if is_sibling: # no coverage
container_ip = network_settings['IPAddress']
container_ip = container_info['NetworkSettings']['IPAddress']
assert isinstance(container_ip, str)
endpoint = (container_ip, container_port)
log.info('Launched sibling container %s from image %s, listening on %s:%i',
container.name, image, container_ip, container_port)
else:
retries = 0
ports = network_settings['Ports']
while len(ports[f'{container_port}/tcp']) < 1:
if (datetime.now() - time_it).seconds > 3:
# Wait for the published ports of the container that's
# supposedly running, otherwise giveup (let it fail).
log.error('Unreachable TCP port %s for container %s',
container_port, container.name)
seconds = 0.0
while True:
ports = container_info['NetworkSettings']['Ports'][f'{container_port}/tcp']
if len(ports) > 0:
break
time.sleep(.33)
container_info = cls._docker.api.inspect_container(container.id)
ports = container_info['NetworkSettings']['Ports']
retries += 1
milisecs = (datetime.now() - time_it).microseconds / 1000
port = one(ports[f'{container_port}/tcp'])
elif seconds > 3:
raise RuntimeError('Unreachable TCP port', container_port, container.name)
else:
time.sleep(.33)
container_info = cls._docker.api.inspect_container(container.id)
seconds = (datetime.now() - time_it).total_seconds()
port = one(ports)
host_ip = port['HostIp']
host_port = int(port['HostPort'])
log.info('Launched container %s from image %s after %dms (retries %d), '
log.info('Launched container %s from image %s after %.3fs, '
'with container port %s mapped to %s:%i on the host',
container.name, image, milisecs, retries, container_port, host_ip, host_port)
container.name, image, seconds, container_port, host_ip, host_port)
endpoint = (host_ip, host_port)
except BaseException: # no coverage
container.kill()
Expand Down

0 comments on commit 5009f13

Please sign in to comment.