test_19_shutdown.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #***************************************************************************
  4. # _ _ ____ _
  5. # Project ___| | | | _ \| |
  6. # / __| | | | |_) | |
  7. # | (__| |_| | _ <| |___
  8. # \___|\___/|_| \_\_____|
  9. #
  10. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. #
  12. # This software is licensed as described in the file COPYING, which
  13. # you should have received as part of this distribution. The terms
  14. # are also available at https://curl.se/docs/copyright.html.
  15. #
  16. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. # copies of the Software, and permit persons to whom the Software is
  18. # furnished to do so, under the terms of the COPYING file.
  19. #
  20. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. # KIND, either express or implied.
  22. #
  23. # SPDX-License-Identifier: curl
  24. #
  25. ###########################################################################
  26. #
  27. import logging
  28. import re
  29. import pytest
  30. from testenv import Env, CurlClient, LocalClient
  31. log = logging.getLogger(__name__)
  32. class TestShutdown:
  33. @pytest.fixture(autouse=True, scope='class')
  34. def _class_scope(self, env, httpd, nghttpx):
  35. if env.have_h3():
  36. nghttpx.start_if_needed()
  37. httpd.clear_extra_configs()
  38. httpd.reload()
  39. @pytest.fixture(autouse=True, scope='class')
  40. def _class_scope(self, env, httpd):
  41. indir = httpd.docs_dir
  42. env.make_data_file(indir=indir, fname="data-10k", fsize=10*1024)
  43. env.make_data_file(indir=indir, fname="data-100k", fsize=100*1024)
  44. env.make_data_file(indir=indir, fname="data-1m", fsize=1024*1024)
  45. # check with `tcpdump` that we see curl TCP RST packets
  46. @pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
  47. @pytest.mark.parametrize("proto", ['http/1.1'])
  48. def test_19_01_check_tcp_rst(self, env: Env, httpd, repeat, proto):
  49. if env.ci_run:
  50. pytest.skip("seems not to work in CI")
  51. curl = CurlClient(env=env)
  52. url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]'
  53. r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[
  54. '--parallel'
  55. ])
  56. r.check_response(http_status=200, count=2)
  57. assert r.tcpdump
  58. assert len(r.tcpdump.stats) != 0, f'Expected TCP RSTs packets: {r.tcpdump.stderr}'
  59. # check with `tcpdump` that we do NOT see TCP RST when CURL_GRACEFUL_SHUTDOWN set
  60. @pytest.mark.skipif(condition=not Env.tcpdump(), reason="tcpdump not available")
  61. @pytest.mark.parametrize("proto", ['http/1.1', 'h2'])
  62. def test_19_02_check_shutdown(self, env: Env, httpd, repeat, proto):
  63. if not env.curl_is_debug():
  64. pytest.skip('only works for curl debug builds')
  65. curl = CurlClient(env=env, run_env={
  66. 'CURL_GRACEFUL_SHUTDOWN': '2000',
  67. 'CURL_DEBUG': 'ssl,tcp'
  68. })
  69. url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]'
  70. r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[
  71. '--parallel'
  72. ])
  73. r.check_response(http_status=200, count=2)
  74. assert r.tcpdump
  75. assert len(r.tcpdump.stats) == 0, 'Unexpected TCP RSTs packets'
  76. # run downloads where the server closes the connection after each request
  77. @pytest.mark.parametrize("proto", ['http/1.1'])
  78. def test_19_03_shutdown_by_server(self, env: Env, httpd, repeat, proto):
  79. if not env.curl_is_debug():
  80. pytest.skip('only works for curl debug builds')
  81. count = 10
  82. curl = CurlClient(env=env, run_env={
  83. 'CURL_GRACEFUL_SHUTDOWN': '2000',
  84. 'CURL_DEBUG': 'ssl'
  85. })
  86. url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\
  87. f'id=[0-{count-1}]&with_cl&close'
  88. r = curl.http_download(urls=[url], alpn_proto=proto)
  89. r.check_response(http_status=200, count=count)
  90. shutdowns = [line for line in r.trace_lines
  91. if re.match(r'.*CCACHE\] shutdown #\d+, done=1', line)]
  92. assert len(shutdowns) == count, f'{shutdowns}'
  93. # run downloads with CURLOPT_FORBID_REUSE set, meaning *we* close
  94. # the connection after each request
  95. @pytest.mark.parametrize("proto", ['http/1.1'])
  96. def test_19_04_shutdown_by_curl(self, env: Env, httpd, proto, repeat):
  97. if not env.curl_is_debug():
  98. pytest.skip('only works for curl debug builds')
  99. count = 10
  100. docname = 'data.json'
  101. url = f'https://localhost:{env.https_port}/{docname}'
  102. client = LocalClient(name='hx-download', env=env, run_env={
  103. 'CURL_GRACEFUL_SHUTDOWN': '2000',
  104. 'CURL_DEBUG': 'ssl'
  105. })
  106. if not client.exists():
  107. pytest.skip(f'example client not built: {client.name}')
  108. r = client.run(args=[
  109. '-n', f'{count}', '-f', '-V', proto, url
  110. ])
  111. r.check_exit_code(0)
  112. shutdowns = [line for line in r.trace_lines
  113. if re.match(r'.*CCACHE\] shutdown #\d+, done=1', line)]
  114. assert len(shutdowns) == count, f'{shutdowns}'
  115. # run event-based downloads with CURLOPT_FORBID_REUSE set, meaning *we* close
  116. # the connection after each request
  117. @pytest.mark.parametrize("proto", ['http/1.1'])
  118. def test_19_05_event_shutdown_by_server(self, env: Env, httpd, proto, repeat):
  119. if not env.curl_is_debug():
  120. pytest.skip('only works for curl debug builds')
  121. count = 10
  122. curl = CurlClient(env=env, run_env={
  123. # forbid connection reuse to trigger shutdowns after transfer
  124. 'CURL_FORBID_REUSE': '1',
  125. # make socket receives block 50% of the time to delay shutdown
  126. 'CURL_DBG_SOCK_RBLOCK': '50',
  127. 'CURL_DEBUG': 'ssl'
  128. })
  129. url = f'https://{env.authority_for(env.domain1, proto)}/curltest/tweak/?'\
  130. f'id=[0-{count-1}]&with_cl&'
  131. r = curl.http_download(urls=[url], alpn_proto=proto, extra_args=[
  132. '--test-event'
  133. ])
  134. r.check_response(http_status=200, count=count)
  135. # check that we closed all connections
  136. closings = [line for line in r.trace_lines
  137. if re.match(r'.*CCACHE\] closing #\d+', line)]
  138. assert len(closings) == count, f'{closings}'
  139. # check that all connection sockets were removed from event
  140. removes = [line for line in r.trace_lines
  141. if re.match(r'.*socket cb: socket \d+ REMOVED', line)]
  142. assert len(removes) == count, f'{removes}'
  143. # check graceful shutdown on multiplexed http
  144. @pytest.mark.parametrize("proto", ['h2', 'h3'])
  145. def test_19_06_check_shutdown(self, env: Env, httpd, nghttpx, repeat, proto):
  146. if proto == 'h3' and not env.have_h3():
  147. pytest.skip("h3 not supported")
  148. if not env.curl_is_debug():
  149. pytest.skip('only works for curl debug builds')
  150. curl = CurlClient(env=env, run_env={
  151. 'CURL_GRACEFUL_SHUTDOWN': '2000',
  152. 'CURL_DEBUG': 'all'
  153. })
  154. url = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-1]'
  155. r = curl.http_download(urls=[url], alpn_proto=proto, with_tcpdump=True, extra_args=[
  156. '--parallel'
  157. ])
  158. r.check_response(http_status=200, count=2)
  159. # check connection cache closings
  160. shutdowns = [line for line in r.trace_lines
  161. if re.match(r'.*CCACHE\] shutdown #\d+, done=1', line)]
  162. assert len(shutdowns) == 1, f'{shutdowns}'