forked from smithy-lang/smithy-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetryPartitionTest.kt
More file actions
178 lines (161 loc) · 8.69 KB
/
RetryPartitionTest.kt
File metadata and controls
178 lines (161 loc) · 8.69 KB
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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rustsdk
import org.junit.jupiter.api.Test
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.testutil.integrationTest
import software.amazon.smithy.rust.codegen.core.testutil.tokioTest
class RetryPartitionTest {
@Test
fun `default retry partition`() {
awsSdkIntegrationTest(SdkCodegenIntegrationTest.model) { ctx, rustCrate ->
val rc = ctx.runtimeConfig
val codegenScope =
arrayOf(
*RuntimeType.preludeScope,
"capture_request" to RuntimeType.captureRequest(rc),
"capture_test_logs" to
CargoDependency.smithyRuntimeTestUtil(rc).toType()
.resolve("test_util::capture_test_logs::capture_test_logs"),
"Credentials" to
AwsRuntimeType.awsCredentialTypesTestUtil(rc)
.resolve("Credentials"),
"Region" to AwsRuntimeType.awsTypes(rc).resolve("region::Region"),
)
rustCrate.integrationTest("default_retry_partition") {
tokioTest("default_retry_partition_includes_region") {
val moduleName = ctx.moduleUseName()
rustTemplate(
"""
let (_logs, logs_rx) = #{capture_test_logs}();
let (http_client, _rx) = #{capture_request}(#{None});
let client_config = $moduleName::Config::builder()
.http_client(http_client)
.region(#{Region}::new("us-west-2"))
.credentials_provider(#{Credentials}::for_tests())
.build();
let client = $moduleName::Client::from_conf(client_config);
let _ = client
.some_operation()
.send()
.await
.expect("success");
let log_contents = logs_rx.contents();
let expected = r##"token bucket for RetryPartition { inner: Default("dontcare-us-west-2") } added to config bag"##;
assert!(log_contents.contains(expected));
""",
*codegenScope,
)
}
tokioTest("user_config_retry_partition") {
val moduleName = ctx.moduleUseName()
rustTemplate(
"""
let (_logs, logs_rx) = #{capture_test_logs}();
let (http_client, _rx) = #{capture_request}(#{None});
let client_config = $moduleName::Config::builder()
.http_client(http_client)
.region(#{Region}::new("us-west-2"))
.credentials_provider(#{Credentials}::for_tests())
.retry_partition(#{RetryPartition}::new("user-partition"))
.build();
let client = $moduleName::Client::from_conf(client_config);
let _ = client
.some_operation()
.send()
.await
.expect("success");
let log_contents = logs_rx.contents();
let expected = r##"token bucket for RetryPartition { inner: Default("user-partition") } added to config bag"##;
assert!(log_contents.contains(expected));
""",
*codegenScope,
"RetryPartition" to RuntimeType.smithyRuntime(ctx.runtimeConfig).resolve("client::retries::RetryPartition"),
)
}
}
}
}
// This test doesn't need to be in "sdk-codegen" but since "default retry partition" test was initially here,
// it is added to this file for consistency.
@Test
fun `custom retry partition`() {
awsSdkIntegrationTest(
SdkCodegenIntegrationTest.model,
) { ctx, crate ->
val codegenScope =
arrayOf(
"BeforeTransmitInterceptorContextRef" to RuntimeType.beforeTransmitInterceptorContextRef(ctx.runtimeConfig),
"BoxError" to RuntimeType.boxError(ctx.runtimeConfig),
"capture_test_logs" to
CargoDependency.smithyRuntimeTestUtil(ctx.runtimeConfig).toType()
.resolve("test_util::capture_test_logs::capture_test_logs"),
"capture_request" to RuntimeType.captureRequest(ctx.runtimeConfig),
"ConfigBag" to RuntimeType.configBag(ctx.runtimeConfig),
"Intercept" to RuntimeType.intercept(ctx.runtimeConfig),
"RetryConfig" to RuntimeType.smithyTypes(ctx.runtimeConfig).resolve("retry::RetryConfig"),
"RetryPartition" to RuntimeType.smithyRuntime(ctx.runtimeConfig).resolve("client::retries::RetryPartition"),
"RuntimeComponents" to RuntimeType.runtimeComponents(ctx.runtimeConfig),
"TokenBucket" to RuntimeType.smithyRuntime(ctx.runtimeConfig).resolve("client::retries::TokenBucket"),
"MAXIMUM_CAPACITY" to RuntimeType.smithyRuntime(ctx.runtimeConfig).resolve("client::retries::token_bucket::MAXIMUM_CAPACITY"),
)
crate.integrationTest("custom_retry_partition") {
tokioTest("test_custom_token_bucket") {
val moduleName = ctx.moduleUseName()
rustTemplate(
"""
use std::sync::{Arc, atomic::{AtomicU32, Ordering}};
use $moduleName::{Client, Config};
##[derive(Clone, Debug, Default)]
struct TestInterceptor {
called: Arc<AtomicU32>,
}
impl #{Intercept} for TestInterceptor {
fn name(&self) -> &'static str {
"TestInterceptor"
}
fn read_before_attempt(
&self,
_context: &#{BeforeTransmitInterceptorContextRef}<'_>,
_runtime_components: &#{RuntimeComponents},
cfg: &mut #{ConfigBag},
) -> Result<(), #{BoxError}> {
self.called.fetch_add(1, Ordering::Relaxed);
let token_bucket = cfg.load::<#{TokenBucket}>().unwrap();
let max_capacity = #{MAXIMUM_CAPACITY};
let expected = format!("permits: {}", max_capacity);
assert!(
format!("{token_bucket:?}").contains(&expected),
"Expected debug output to contain `{expected}`, but got: {token_bucket:?}"
);
Ok(())
}
}
let (http_client, _) = #{capture_request}(None);
let test_interceptor = TestInterceptor::default();
let client_config = Config::builder()
.interceptor(test_interceptor.clone())
.retry_partition(#{RetryPartition}::custom("test")
.token_bucket(#{TokenBucket}::unlimited())
.build()
)
.http_client(http_client)
.build();
let client = Client::from_conf(client_config);
let _ = client.some_operation().send().await;
assert!(
test_interceptor.called.load(Ordering::Relaxed) == 1,
"the interceptor should have been called"
);
""",
*codegenScope,
)
}
}
}
}
}