Debian Codegraph @

test/appexit_http.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 import awscrt.http
5 import awscrt.io
6 import enum
7 import sys
8 import threading
9
10 # Application for use with the test_appexit unit tests.
11 # Performs an HTTP request and shuts everything down,
12 # with the opportunity to exit the application at any stage.
13
14 Stage = enum.Enum('Stage', [
15 'Start',
16 'EventLoopGroupStart',
17 'HostResolverStart',
18 'ClientBootstrapStart',
19 'TlsConnectionOptionsStart',
20 'HttpClientStart',
21 'HttpClientConnected',
22 'HttpStreamStart',
23 'HttpStreamReceivingBody',
24 'HttpStreamDone',
25 'HttpConnectionClose',
26 'HttpConnectionDone',
27 'TlsConnectionOptionsDone',
28 'ClientBootstrapDone',
29 'HostResolverDone',
30 'EventLoopGroupDone',
31 'Done',
32 ])
33
34
35 EXIT_STAGE = Stage.Done
36 TIMEOUT = 30.0
37 REQUEST_HOST = 's3.amazonaws.com'
38 REQUEST_PATH = '/code-sharing-aws-crt/elastigirl.png'
39 REQUEST_PORT = 443
40
41
42 def set_stage(stage):
43 print("Stage:", stage)
44 if stage.value >= EXIT_STAGE.value:
45 print("Exiting now")
46 sys.exit()
47
48
49 if __name__ == '__main__':
50 EXIT_STAGE = Stage[sys.argv[1]]
51
52 awscrt.io.init_logging(awscrt.io.LogLevel.Trace, 'stdout')
53
54 set_stage(Stage.Start)
55
56 # EventLoopGroupStart
57 elg = awscrt.io.EventLoopGroup()
58 set_stage(Stage.EventLoopGroupStart)
59
60 # HostResolverStart
61 resolver = awscrt.io.DefaultHostResolver(elg)
62 set_stage(Stage.HostResolverStart)
63
64 # ClientBootstrapStart
65 bootstrap = awscrt.io.ClientBootstrap(elg, resolver)
66 set_stage(Stage.ClientBootstrapStart)
67
68 # TlsConnectionOptionsStart
69 tls_ctx_opt = awscrt.io.TlsContextOptions()
70 tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_opt)
71 tls_conn_opt = tls_ctx.new_connection_options()
72 tls_conn_opt.set_server_name(REQUEST_HOST)
73 set_stage(Stage.TlsConnectionOptionsStart)
74
75 # HttpClientStart
76 http_connection = awscrt.http.HttpClientConnection.new(
77 host_name=REQUEST_HOST,
78 port=REQUEST_PORT,
79 bootstrap=bootstrap,
80 tls_connection_options=tls_conn_opt)
81 set_stage(Stage.HttpClientStart)
82
83 # HttpClientConnected
84 try:
85 http_connection = http_connection.result(TIMEOUT)
86 except Exception:
87 # the internet's a flaky place and this isn't a correctness test
88 print("Connection failed. Exiting out early...")
89 set_stage(Stage.Done)
90
91 set_stage(Stage.HttpClientConnected)
92
93 # HttpStreamStart
94 request = awscrt.http.HttpRequest(path=REQUEST_PATH)
95 request.headers.add('Host', REQUEST_HOST)
96
97 receiving_body_event = threading.Event()
98
99 def on_incoming_body(http_stream, chunk):
100 receiving_body_event.set()
101
102 http_stream = http_connection.request(request, on_body=on_incoming_body)
103 http_stream.activate()
104 set_stage(Stage.HttpStreamStart)
105
106 # HttpStreamReceivingBody
107 assert receiving_body_event.wait(TIMEOUT)
108 set_stage(Stage.HttpStreamReceivingBody)
109
110 # HttpStreamDone
111 try:
112 status_code = http_stream.completion_future.result(TIMEOUT)
113 except Exception:
114 # the internet's a flaky place and this isn't a correctness test
115 print("Request failed. Continuing with cleanup...")
116
117 del http_stream
118 del request
119 set_stage(Stage.HttpStreamDone)
120
121 # HttpConnectionClose
122 connection_shutdown_future = http_connection.close()
123 set_stage(Stage.HttpConnectionClose)
124
125 # HttpConnectionDone
126 del http_connection
127 connection_shutdown_future.result(TIMEOUT)
128 set_stage(Stage.HttpConnectionDone)
129
130 # TlsConnectionOptionsDone
131 del tls_conn_opt
132 del tls_ctx
133 del tls_ctx_opt
134 set_stage(Stage.TlsConnectionOptionsDone)
135
136 # ClientBootstrapDone
137 bootstrap_shutdown_event = bootstrap.shutdown_event
138 del bootstrap
139 assert bootstrap_shutdown_event.wait(TIMEOUT)
140 set_stage(Stage.ClientBootstrapDone)
141
142 # HostResolverDone
143 del resolver
144 set_stage(Stage.HostResolverDone)
145
146 # EventLoopGroupDone
147 elg_shutdown_event = elg.shutdown_event
148 del elg
149 assert elg_shutdown_event.wait(TIMEOUT)
150 set_stage(Stage.EventLoopGroupDone)
151
152 # Done
153 set_stage(Stage.Done)