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