|
| 1 | +#!/usr/bin/env python |
| 2 | +from contextlib import contextmanager |
| 3 | +import requests |
| 4 | +import signal |
| 5 | +import threading |
| 6 | +import time |
| 7 | + |
| 8 | +from util import start_cloudflared, wait_tunnel_ready, check_tunnel_not_connected, LOGGER |
| 9 | + |
| 10 | + |
| 11 | +class TestTermination(): |
| 12 | + grace_period = 5 |
| 13 | + timeout = 10 |
| 14 | + extra_config = { |
| 15 | + "grace-period": f"{grace_period}s", |
| 16 | + } |
| 17 | + signals = [signal.SIGTERM, signal.SIGINT] |
| 18 | + sse_endpoint = "/sse?freq=1s" |
| 19 | + |
| 20 | + def test_graceful_shutdown(self, tmp_path, component_tests_config): |
| 21 | + config = component_tests_config(self.extra_config) |
| 22 | + for sig in self.signals: |
| 23 | + with start_cloudflared( |
| 24 | + tmp_path, config, new_process=True, capture_output=False) as cloudflared: |
| 25 | + wait_tunnel_ready(tunnel_url=config.get_url()) |
| 26 | + |
| 27 | + connected = threading.Condition() |
| 28 | + in_flight_req = threading.Thread( |
| 29 | + target=self.stream_request, args=(config, connected,)) |
| 30 | + in_flight_req.start() |
| 31 | + |
| 32 | + with connected: |
| 33 | + connected.wait(self.timeout) |
| 34 | + # Send signal after the SSE connection is established |
| 35 | + self.terminate_by_signal(cloudflared, sig) |
| 36 | + self.wait_eyeball_thread( |
| 37 | + in_flight_req, self.grace_period + self.timeout) |
| 38 | + |
| 39 | + # test cloudflared terminates before grace period expires when all eyeball |
| 40 | + # connections are drained |
| 41 | + def test_shutdown_once_no_connection(self, tmp_path, component_tests_config): |
| 42 | + config = component_tests_config(self.extra_config) |
| 43 | + for sig in self.signals: |
| 44 | + with start_cloudflared( |
| 45 | + tmp_path, config, new_process=True, capture_output=False) as cloudflared: |
| 46 | + wait_tunnel_ready(tunnel_url=config.get_url()) |
| 47 | + |
| 48 | + connected = threading.Condition() |
| 49 | + in_flight_req = threading.Thread( |
| 50 | + target=self.stream_request, args=(config, connected, True, )) |
| 51 | + in_flight_req.start() |
| 52 | + |
| 53 | + with self.within_grace_period(): |
| 54 | + with connected: |
| 55 | + connected.wait(self.timeout) |
| 56 | + # Send signal after the SSE connection is established |
| 57 | + self.terminate_by_signal(cloudflared, sig) |
| 58 | + self.wait_eyeball_thread(in_flight_req, self.grace_period) |
| 59 | + |
| 60 | + def test_no_connection_shutdown(self, tmp_path, component_tests_config): |
| 61 | + config = component_tests_config(self.extra_config) |
| 62 | + for sig in self.signals: |
| 63 | + with start_cloudflared( |
| 64 | + tmp_path, config, new_process=True, capture_output=False) as cloudflared: |
| 65 | + wait_tunnel_ready(tunnel_url=config.get_url()) |
| 66 | + with self.within_grace_period(): |
| 67 | + self.terminate_by_signal(cloudflared, sig) |
| 68 | + |
| 69 | + def terminate_by_signal(self, cloudflared, sig): |
| 70 | + cloudflared.send_signal(sig) |
| 71 | + check_tunnel_not_connected() |
| 72 | + cloudflared.wait() |
| 73 | + |
| 74 | + def wait_eyeball_thread(self, thread, timeout): |
| 75 | + thread.join(timeout) |
| 76 | + assert thread.is_alive() == False, "eyeball thread is still alive" |
| 77 | + |
| 78 | + # Using this context asserts logic within the context is executed within grace period |
| 79 | + @contextmanager |
| 80 | + def within_grace_period(self): |
| 81 | + try: |
| 82 | + start = time.time() |
| 83 | + yield |
| 84 | + finally: |
| 85 | + duration = time.time() - start |
| 86 | + assert duration < self.grace_period |
| 87 | + |
| 88 | + def stream_request(self, config, connected, early_terminate): |
| 89 | + expected_terminate_message = "502 Bad Gateway" |
| 90 | + url = config.get_url() + self.sse_endpoint |
| 91 | + |
| 92 | + with requests.get(url, timeout=5, stream=True) as resp: |
| 93 | + with connected: |
| 94 | + connected.notifyAll() |
| 95 | + lines = 0 |
| 96 | + for line in resp.iter_lines(): |
| 97 | + if expected_terminate_message.encode() == line: |
| 98 | + break |
| 99 | + lines += 1 |
| 100 | + if early_terminate and lines == 2: |
| 101 | + return |
| 102 | + # /sse returns count followed by 2 new lines |
| 103 | + assert lines >= (self.grace_period * 2) |
0 commit comments