11#!/usr/bin/env python
22from contextlib import contextmanager
3- import requests
3+ import platform
44import signal
55import threading
66import time
77
8- from util import start_cloudflared , wait_tunnel_ready , check_tunnel_not_connected , LOGGER
8+ import pytest
9+ import requests
10+
11+ from util import start_cloudflared , wait_tunnel_ready , check_tunnel_not_connected
12+
13+
14+ def supported_signals ():
15+ if platform .system () == "Windows" :
16+ return [signal .SIGTERM ]
17+ return [signal .SIGTERM , signal .SIGINT ]
918
1019
1120class TestTermination ():
@@ -14,57 +23,56 @@ class TestTermination():
1423 extra_config = {
1524 "grace-period" : f"{ grace_period } s" ,
1625 }
17- signals = [signal .SIGTERM , signal .SIGINT ]
1826 sse_endpoint = "/sse?freq=1s"
1927
20- def test_graceful_shutdown (self , tmp_path , component_tests_config ):
28+ @pytest .mark .parametrize ("signal" , supported_signals ())
29+ def test_graceful_shutdown (self , tmp_path , component_tests_config , signal ):
2130 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 )
31+ with start_cloudflared (
32+ tmp_path , config , new_process = True , capture_output = False ) as cloudflared :
33+ wait_tunnel_ready (tunnel_url = config .get_url ())
34+
35+ connected = threading .Condition ()
36+ in_flight_req = threading .Thread (
37+ target = self .stream_request , args = (config , connected , False , ))
38+ in_flight_req .start ()
39+
40+ with connected :
41+ connected .wait (self .timeout )
42+ # Send signal after the SSE connection is established
43+ self .terminate_by_signal (cloudflared , signal )
44+ self .wait_eyeball_thread (
45+ in_flight_req , self .grace_period + self .timeout )
3846
3947 # test cloudflared terminates before grace period expires when all eyeball
4048 # connections are drained
41- def test_shutdown_once_no_connection (self , tmp_path , component_tests_config ):
49+ @pytest .mark .parametrize ("signal" , supported_signals ())
50+ def test_shutdown_once_no_connection (self , tmp_path , component_tests_config , signal ):
4251 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 connected :
54- connected . wait ( self .timeout )
55- with self . within_grace_period ():
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 ):
52+ with start_cloudflared (
53+ tmp_path , config , new_process = True , capture_output = False ) as cloudflared :
54+ wait_tunnel_ready ( tunnel_url = config . get_url ())
55+
56+ connected = threading . Condition ()
57+ in_flight_req = threading .Thread (
58+ target = self . stream_request , args = ( config , connected , True , ))
59+ in_flight_req . start ( )
60+
61+ with connected :
62+ connected . wait ( self . timeout )
63+ with self .within_grace_period ():
64+ # Send signal after the SSE connection is established
65+ self . terminate_by_signal ( cloudflared , signal )
66+ self .wait_eyeball_thread ( in_flight_req , self . grace_period )
67+
68+ @ pytest . mark . parametrize ( "signal" , supported_signals ())
69+ def test_no_connection_shutdown (self , tmp_path , component_tests_config , signal ):
6170 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 )
71+ with start_cloudflared (
72+ tmp_path , config , new_process = True , capture_output = False ) as cloudflared :
73+ wait_tunnel_ready (tunnel_url = config .get_url ())
74+ with self .within_grace_period ():
75+ self .terminate_by_signal (cloudflared , signal )
6876
6977 def terminate_by_signal (self , cloudflared , sig ):
7078 cloudflared .send_signal (sig )
0 commit comments