• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.setup_incomplete(),
40                    reason=f"missing: {Env.incomplete_reason()}")
41class TestGoAway:
42
43    @pytest.fixture(autouse=True, scope='class')
44    def _class_scope(self, env, httpd, nghttpx):
45        if env.have_h3():
46            nghttpx.start_if_needed()
47        httpd.clear_extra_configs()
48        httpd.reload()
49
50    # download files sequentially with delay, reload server for GOAWAY
51    def test_03_01_h2_goaway(self, env: Env, httpd, nghttpx, repeat):
52        proto = 'h2'
53        count = 3
54        self.r = None
55        def long_run():
56            curl = CurlClient(env=env)
57            #  send 10 chunks of 1024 bytes in a response body with 100ms delay in between
58            urln = f'https://{env.authority_for(env.domain1, proto)}' \
59                   f'/curltest/tweak?id=[0-{count - 1}]'\
60                   '&chunks=10&chunk_size=1024&chunk_delay=100ms'
61            self.r = curl.http_download(urls=[urln], alpn_proto=proto)
62
63        t = Thread(target=long_run)
64        t.start()
65        # each request will take a second, reload the server in the middle
66        # of the first one.
67        time.sleep(1.5)
68        assert httpd.reload()
69        t.join()
70        r: ExecResult = self.r
71        assert r.exit_code == 0, f'{r}'
72        r.check_stats(count=count, exp_status=200)
73        # reload will shut down the connection gracefully with GOAWAY
74        # we expect to see a second connection opened afterwards
75        assert r.total_connects == 2
76        for idx, s in enumerate(r.stats):
77            if s['num_connects'] > 0:
78                log.debug(f'request {idx} connected')
79        # this should take `count` seconds to retrieve
80        assert r.duration >= timedelta(seconds=count)
81
82    # download files sequentially with delay, reload server for GOAWAY
83    @pytest.mark.skipif(condition=not Env.have_h3(), reason="h3 not supported")
84    def test_03_02_h3_goaway(self, env: Env, httpd, nghttpx, repeat):
85        proto = 'h3'
86        count = 3
87        self.r = None
88        def long_run():
89            curl = CurlClient(env=env)
90            #  send 10 chunks of 1024 bytes in a response body with 100ms delay in between
91            urln = f'https://{env.authority_for(env.domain1, proto)}' \
92                   f'/curltest/tweak?id=[0-{count - 1}]'\
93                   '&chunks=10&chunk_size=1024&chunk_delay=100ms'
94            self.r = curl.http_download(urls=[urln], alpn_proto=proto)
95
96        t = Thread(target=long_run)
97        t.start()
98        # each request will take a second, reload the server in the middle
99        # of the first one.
100        time.sleep(1.5)
101        assert nghttpx.reload(timeout=timedelta(seconds=2))
102        t.join()
103        r: ExecResult = self.r
104        assert r.exit_code == 0, f'{r}'
105        # reload will shut down the connection gracefully with GOAWAY
106        # we expect to see a second connection opened afterwards
107        assert r.total_connects == 2
108        for idx, s in enumerate(r.stats):
109            if s['num_connects'] > 0:
110                log.debug(f'request {idx} connected')
111        # this should take `count` seconds to retrieve
112        assert r.duration >= timedelta(seconds=count)
113        r.check_stats(count=count, exp_status=200, exp_exitcode=0)
114
115    # download files sequentially with delay, reload server for GOAWAY
116    def test_03_03_h1_goaway(self, env: Env, httpd, nghttpx, repeat):
117        proto = 'http/1.1'
118        count = 3
119        self.r = None
120        def long_run():
121            curl = CurlClient(env=env)
122            #  send 10 chunks of 1024 bytes in a response body with 100ms delay in between
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)
127
128        t = Thread(target=long_run)
129        t.start()
130        # each request will take a second, reload the server in the middle
131        # of the first one.
132        time.sleep(1.5)
133        assert httpd.reload()
134        t.join()
135        r: ExecResult = self.r
136        assert r.exit_code == 0, f'{r}'
137        r.check_stats(count=count, exp_status=200)
138        # reload will shut down the connection gracefully with GOAWAY
139        # we expect to see a second connection opened afterwards
140        assert r.total_connects == 2
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
147
148