-
Notifications
You must be signed in to change notification settings - Fork 193
/
timeouts.rs
179 lines (167 loc) · 6.12 KB
/
timeouts.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
use aws_config::SdkConfig;
use aws_credential_types::provider::SharedCredentialsProvider;
use aws_sdk_s3::config::{Credentials, Region};
use aws_sdk_s3::types::{
CompressionType, CsvInput, CsvOutput, ExpressionType, FileHeaderInfo, InputSerialization,
OutputSerialization,
};
use aws_sdk_s3::Client;
use aws_smithy_async::assert_elapsed;
use aws_smithy_async::rt::sleep::{default_async_sleep, SharedAsyncSleep, TokioSleep};
use aws_smithy_runtime::client::http::test_util::NeverClient;
use aws_smithy_types::error::display::DisplayErrorContext;
use aws_smithy_types::timeout::TimeoutConfig;
use std::future::Future;
use std::net::SocketAddr;
use std::time::Duration;
use tokio::net::TcpListener;
use tokio::time::timeout;
#[tokio::test(start_paused = true)]
async fn test_timeout_service_ends_request_that_never_completes() {
let sdk_config = SdkConfig::builder()
.region(Region::from_static("us-east-2"))
.credentials_provider(SharedCredentialsProvider::new(Credentials::for_tests()))
.http_client(NeverClient::new())
.timeout_config(
TimeoutConfig::builder()
.operation_timeout(Duration::from_secs_f32(0.5))
.build(),
)
.sleep_impl(SharedAsyncSleep::new(TokioSleep::new()))
.build();
let client = Client::new(&sdk_config);
let now = tokio::time::Instant::now();
let err = client
.select_object_content()
.bucket("aws-rust-sdk")
.key("sample_data.csv")
.expression_type(ExpressionType::Sql)
.expression("SELECT * FROM s3object s WHERE s.\"Name\" = 'Jane'")
.input_serialization(
InputSerialization::builder()
.csv(
CsvInput::builder()
.file_header_info(FileHeaderInfo::Use)
.build(),
)
.compression_type(CompressionType::None)
.build(),
)
.output_serialization(
OutputSerialization::builder()
.csv(CsvOutput::builder().build())
.build(),
)
.send()
.await
.unwrap_err();
let expected = "operation timeout (all attempts including retries) occurred after 500ms";
let message = format!("{}", DisplayErrorContext(err));
assert!(
message.contains(expected),
"expected '{message}' to contain '{expected}'"
);
assert_elapsed!(now, std::time::Duration::from_secs_f32(0.5));
}
#[tokio::test]
async fn test_read_timeout() {
async fn run_server(
mut shutdown_receiver: tokio::sync::oneshot::Receiver<()>,
) -> (impl Future<Output = ()>, SocketAddr) {
let listener = TcpListener::bind("0.0.0.0:0").await.unwrap();
let listener_addr = listener.local_addr().unwrap();
(
async move {
while shutdown_receiver.try_recv().is_err() {
if let Ok(Ok((_socket, _))) =
timeout(Duration::from_millis(100), listener.accept()).await
{
tokio::time::sleep(Duration::from_millis(1000)).await;
}
}
},
listener_addr,
)
}
let (server_shutdown, server_shutdown_receiver) = tokio::sync::oneshot::channel();
let (server_fut, server_addr) = run_server(server_shutdown_receiver).await;
let server_handle = tokio::spawn(server_fut);
tokio::time::sleep(Duration::from_millis(100)).await;
let config = SdkConfig::builder()
.sleep_impl(default_async_sleep().unwrap())
.timeout_config(
TimeoutConfig::builder()
.read_timeout(Duration::from_millis(300))
.build(),
)
.endpoint_url(format!("http://{server_addr}"))
.region(Some(Region::from_static("us-east-1")))
.credentials_provider(SharedCredentialsProvider::new(Credentials::for_tests()))
.build();
let client = Client::new(&config);
if let Ok(result) = timeout(
Duration::from_millis(1000),
client.get_object().bucket("test").key("test").send(),
)
.await
{
match result {
Ok(_) => panic!("should not have succeeded"),
Err(err) => {
let message = format!("{}", DisplayErrorContext(&err));
let expected = "timeout: HTTP read timeout occurred after 300ms";
assert!(
message.contains(expected),
"expected '{message}' to contain '{expected}'"
);
}
}
} else {
panic!("the client didn't timeout");
}
server_shutdown.send(()).unwrap();
server_handle.await.unwrap();
}
#[tokio::test]
async fn test_connect_timeout() {
let config = SdkConfig::builder()
.sleep_impl(default_async_sleep().unwrap())
.timeout_config(
TimeoutConfig::builder()
.connect_timeout(Duration::from_millis(300))
.build(),
)
.endpoint_url(
// Emulate a connect timeout error by hitting an unroutable IP
"http://172.255.255.0:18104",
)
.region(Some(Region::from_static("us-east-1")))
.credentials_provider(SharedCredentialsProvider::new(Credentials::for_tests()))
.build();
let client = Client::new(&config);
if let Ok(result) = timeout(
Duration::from_millis(1000),
client.get_object().bucket("test").key("test").send(),
)
.await
{
match result {
Ok(_) => panic!("should not have succeeded"),
Err(err) => {
let message = format!("{}", DisplayErrorContext(&err));
let expected =
"timeout: error trying to connect: HTTP connect timeout occurred after 300ms";
assert!(
message.contains(expected),
"expected '{message}' to contain '{expected}'"
);
}
}
} else {
panic!("the client didn't timeout");
}
}