22
22
import java .io .IOException ;
23
23
import java .net .URI ;
24
24
import java .util .concurrent .CompletableFuture ;
25
+ import org .junit .After ;
25
26
import org .junit .Before ;
26
27
import org .junit .Test ;
27
28
import org .junit .runner .RunWith ;
33
34
import software .amazon .awssdk .crt .http .HttpClientConnection ;
34
35
import software .amazon .awssdk .crt .http .HttpClientConnectionManager ;
35
36
import software .amazon .awssdk .crt .http .HttpRequest ;
37
+ import software .amazon .awssdk .http .SdkCancellationException ;
36
38
import software .amazon .awssdk .http .SdkHttpFullRequest ;
37
39
import software .amazon .awssdk .http .async .AsyncExecuteRequest ;
38
40
import software .amazon .awssdk .http .async .SdkAsyncHttpResponseHandler ;
@@ -43,6 +45,7 @@ public class CrtRequestExecutorTest {
43
45
private CrtRequestExecutor requestExecutor ;
44
46
@ Mock
45
47
private HttpClientConnectionManager connectionManager ;
48
+
46
49
@ Mock
47
50
private SdkAsyncHttpResponseHandler responseHandler ;
48
51
@@ -54,8 +57,13 @@ public void setup() {
54
57
requestExecutor = new CrtRequestExecutor ();
55
58
}
56
59
60
+ @ After
61
+ public void teardown () {
62
+ Mockito .reset (connectionManager , responseHandler , httpClientConnection );
63
+ }
64
+
57
65
@ Test
58
- public void execute_acquireConnectionThrowException_shouldInvokeOnError () {
66
+ public void acquireConnectionThrowException_shouldInvokeOnError () {
59
67
RuntimeException exception = new RuntimeException ("error" );
60
68
CrtRequestContext context = CrtRequestContext .builder ()
61
69
.crtConnPool (connectionManager )
@@ -80,7 +88,7 @@ public void execute_acquireConnectionThrowException_shouldInvokeOnError() {
80
88
}
81
89
82
90
@ Test
83
- public void execute_makeRequestThrowException_shouldInvokeOnError () {
91
+ public void makeRequestThrowException_shouldInvokeOnError () {
84
92
CrtRuntimeException exception = new CrtRuntimeException ("" );
85
93
SdkHttpFullRequest request = createRequest (URI .create ("http://localhost" ));
86
94
CrtRequestContext context = CrtRequestContext .builder ()
@@ -110,4 +118,47 @@ public void execute_makeRequestThrowException_shouldInvokeOnError() {
110
118
assertThat (actualException ).hasCause (exception );
111
119
assertThat (executeFuture ).hasFailedWithThrowableThat ().hasCause (exception ).isInstanceOf (IOException .class );
112
120
}
121
+
122
+ @ Test
123
+ public void makeRequest_success () {
124
+ SdkHttpFullRequest request = createRequest (URI .create ("http://localhost" ));
125
+ CrtRequestContext context = CrtRequestContext .builder ()
126
+ .readBufferSize (2000 )
127
+ .crtConnPool (connectionManager )
128
+ .request (AsyncExecuteRequest .builder ()
129
+ .request (request )
130
+ .requestContentPublisher (createProvider ("" ))
131
+ .responseHandler (responseHandler )
132
+ .build ())
133
+ .build ();
134
+ CompletableFuture <HttpClientConnection > completableFuture = new CompletableFuture <>();
135
+ Mockito .when (connectionManager .acquireConnection ()).thenReturn (completableFuture );
136
+ completableFuture .complete (httpClientConnection );
137
+
138
+ CompletableFuture <Void > executeFuture = requestExecutor .execute (context );
139
+ Mockito .verifyZeroInteractions (responseHandler );
140
+ }
141
+
142
+ @ Test
143
+ public void cancelRequest_shouldInvokeOnError () {
144
+ CrtRequestContext context = CrtRequestContext .builder ()
145
+ .crtConnPool (connectionManager )
146
+ .request (AsyncExecuteRequest .builder ()
147
+ .responseHandler (responseHandler )
148
+ .build ())
149
+ .build ();
150
+ CompletableFuture <HttpClientConnection > completableFuture = new CompletableFuture <>();
151
+
152
+ Mockito .when (connectionManager .acquireConnection ()).thenReturn (completableFuture );
153
+
154
+ CompletableFuture <Void > executeFuture = requestExecutor .execute (context );
155
+ executeFuture .cancel (true );
156
+
157
+ ArgumentCaptor <Exception > argumentCaptor = ArgumentCaptor .forClass (Exception .class );
158
+ Mockito .verify (responseHandler ).onError (argumentCaptor .capture ());
159
+
160
+ Exception actualException = argumentCaptor .getValue ();
161
+ assertThat (actualException ).hasMessageContaining ("The request was cancelled" );
162
+ assertThat (actualException ).isInstanceOf (SdkCancellationException .class );
163
+ }
113
164
}
0 commit comments