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# 27import logging 28import time 29from datetime import timedelta 30from threading import Thread 31import pytest 32 33from testenv import Env, CurlClient, ExecResult 34 35 36log = logging.getLogger(__name__) 37 38 39@pytest.mark.skipif(condition=Env().ci_run, reason="not suitable for CI runs") 40class TestGoAway: 41 42 @pytest.fixture(autouse=True, scope='class') 43 def _class_scope(self, env, httpd, nghttpx): 44 if env.have_h3(): 45 nghttpx.start_if_needed() 46 httpd.clear_extra_configs() 47 httpd.reload() 48 49 # download files sequentially with delay, reload server for GOAWAY 50 def test_03_01_h2_goaway(self, env: Env, httpd, nghttpx, repeat): 51 proto = 'h2' 52 count = 3 53 self.r = None 54 def long_run(): 55 curl = CurlClient(env=env) 56 # send 10 chunks of 1024 bytes in a response body with 100ms delay in between 57 urln = f'https://{env.authority_for(env.domain1, proto)}' \ 58 f'/curltest/tweak?id=[0-{count - 1}]'\ 59 '&chunks=10&chunk_size=1024&chunk_delay=100ms' 60 self.r = curl.http_download(urls=[urln], alpn_proto=proto) 61 62 t = Thread(target=long_run) 63 t.start() 64 # each request will take a second, reload the server in the middle 65 # of the first one. 66 time.sleep(1.5) 67 assert httpd.reload() 68 t.join() 69 r: ExecResult = self.r 70 r.check_response(count=count, http_status=200) 71 # reload will shut down the connection gracefully with GOAWAY 72 # we expect to see a second connection opened afterwards 73 assert r.total_connects == 2 74 for idx, s in enumerate(r.stats): 75 if s['num_connects'] > 0: 76 log.debug(f'request {idx} connected') 77 # this should take `count` seconds to retrieve 78 assert r.duration >= timedelta(seconds=count) 79 80 # download files sequentially with delay, reload server for GOAWAY 81 @pytest.mark.skipif(condition=not Env.have_h3(), reason="h3 not supported") 82 def test_03_02_h3_goaway(self, env: Env, httpd, nghttpx, repeat): 83 proto = 'h3' 84 if proto == 'h3' and env.curl_uses_lib('msh3'): 85 pytest.skip("msh3 stalls here") 86 if proto == 'h3' and env.curl_uses_lib('quiche'): 87 pytest.skip("does not work in CI, but locally for some reason") 88 if proto == 'h3' and env.curl_uses_ossl_quic(): 89 pytest.skip('OpenSSL QUIC fails here') 90 count = 3 91 self.r = None 92 def long_run(): 93 curl = CurlClient(env=env) 94 # send 10 chunks of 1024 bytes in a response body with 100ms delay in between 95 urln = f'https://{env.authority_for(env.domain1, proto)}' \ 96 f'/curltest/tweak?id=[0-{count - 1}]'\ 97 '&chunks=10&chunk_size=1024&chunk_delay=100ms' 98 self.r = curl.http_download(urls=[urln], alpn_proto=proto) 99 100 t = Thread(target=long_run) 101 t.start() 102 # each request will take a second, reload the server in the middle 103 # of the first one. 104 time.sleep(1.5) 105 assert nghttpx.reload(timeout=timedelta(seconds=2)) 106 t.join() 107 r: ExecResult = self.r 108 # this should take `count` seconds to retrieve 109 assert r.duration >= timedelta(seconds=count) 110 r.check_response(count=count, http_status=200, connect_count=2) 111 # reload will shut down the connection gracefully with GOAWAY 112 # we expect to see a second connection opened afterwards 113 for idx, s in enumerate(r.stats): 114 if s['num_connects'] > 0: 115 log.debug(f'request {idx} connected') 116 117 # download files sequentially with delay, reload server for GOAWAY 118 def test_03_03_h1_goaway(self, env: Env, httpd, nghttpx, repeat): 119 proto = 'http/1.1' 120 count = 3 121 self.r = None 122 def long_run(): 123 curl = CurlClient(env=env) 124 # send 10 chunks of 1024 bytes in a response body with 100ms delay in between 125 urln = f'https://{env.authority_for(env.domain1, proto)}' \ 126 f'/curltest/tweak?id=[0-{count - 1}]'\ 127 '&chunks=10&chunk_size=1024&chunk_delay=100ms' 128 self.r = curl.http_download(urls=[urln], alpn_proto=proto) 129 130 t = Thread(target=long_run) 131 t.start() 132 # each request will take a second, reload the server in the middle 133 # of the first one. 134 time.sleep(1.5) 135 assert httpd.reload() 136 t.join() 137 r: ExecResult = self.r 138 r.check_response(count=count, http_status=200, connect_count=2) 139 # reload will shut down the connection gracefully with GOAWAY 140 # we expect to see a second connection opened afterwards 141 for idx, s in enumerate(r.stats): 142 if s['num_connects'] > 0: 143 log.debug(f'request {idx} connected') 144 # this should take `count` seconds to retrieve 145 assert r.duration >= timedelta(seconds=count) 146