Skip to content

Commit 65fd82c

Browse files
committed
Add test for pre-execution failures
1 parent 89883c7 commit 65fd82c

File tree

1 file changed

+54
-0
lines changed
  • test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services

1 file changed

+54
-0
lines changed

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/TraceIdTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919

2020
import java.util.List;
21+
import java.util.concurrent.atomic.AtomicReference;
2122
import org.junit.jupiter.api.Test;
2223
import org.slf4j.MDC;
2324
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
2425
import software.amazon.awssdk.awscore.interceptor.TraceIdExecutionInterceptor;
26+
import software.amazon.awssdk.core.interceptor.Context;
27+
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
28+
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
2529
import software.amazon.awssdk.http.AbortableInputStream;
2630
import software.amazon.awssdk.http.HttpExecuteResponse;
2731
import software.amazon.awssdk.http.SdkHttpRequest;
@@ -188,5 +192,55 @@ public void traceIdInterceptorPreservesTraceIdAcrossExceptionallyCompletedFuture
188192
}
189193
});
190194
}
195+
196+
@Test
197+
public void traceIdInterceptorPreservesTraceIdAcrossExceptionallyCompletedFuturesThrownInPreExecution() {
198+
EnvironmentVariableHelper.run(env -> {
199+
env.set("AWS_LAMBDA_FUNCTION_NAME", "foo");
200+
MDC.put("AWS_LAMBDA_X_TRACE_ID", "mdc-trace-123");
201+
202+
ExecutionInterceptor throwingInterceptor = new ExecutionInterceptor() {
203+
private boolean hasThrown = false;
204+
205+
@Override
206+
public void beforeMarshalling(Context.BeforeMarshalling context, ExecutionAttributes executionAttributes) {
207+
if (!hasThrown) {
208+
hasThrown = true;
209+
throw new RuntimeException("failing in pre execution");
210+
}
211+
}
212+
};
213+
214+
try (MockAsyncHttpClient mockHttpClient = new MockAsyncHttpClient();
215+
ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder()
216+
.region(Region.US_WEST_2)
217+
.credentialsProvider(AnonymousCredentialsProvider.create())
218+
.overrideConfiguration(o -> o.addExecutionInterceptor(throwingInterceptor))
219+
.httpClient(mockHttpClient)
220+
.build()) {
221+
222+
mockHttpClient.stubResponses(
223+
HttpExecuteResponse.builder()
224+
.response(SdkHttpResponse.builder().statusCode(200).build())
225+
.responseBody(AbortableInputStream.create(new StringInputStream("{}")))
226+
.build()
227+
);
228+
229+
client.allTypes()
230+
.exceptionally(throwable -> {
231+
client.allTypes().join();
232+
return null;
233+
}).join();
234+
235+
List<SdkHttpRequest> requests = mockHttpClient.getRequests();
236+
237+
assertThat(requests).hasSize(1);
238+
assertThat(requests.get(0).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("mdc-trace-123");
239+
240+
} finally {
241+
MDC.clear();
242+
}
243+
});
244+
}
191245
}
192246

0 commit comments

Comments
 (0)