24
24
import static org .assertj .core .api .AssertionsForInterfaceTypes .assertThat ;
25
25
26
26
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
27
+ import com .github .tomakehurst .wiremock .stubbing .StubMapping ;
27
28
import java .io .ByteArrayOutputStream ;
28
29
import java .io .File ;
29
30
import java .io .IOException ;
43
44
import software .amazon .awssdk .http .apache .ApacheHttpClient ;
44
45
import software .amazon .awssdk .regions .Region ;
45
46
import software .amazon .awssdk .services .protocolrestjson .ProtocolRestJsonClient ;
47
+ import software .amazon .awssdk .services .protocolrestjson .ProtocolRestJsonClientBuilder ;
46
48
import software .amazon .awssdk .services .protocolrestjson .model .StreamingOutputOperationRequest ;
47
49
import software .amazon .awssdk .services .protocolrestjson .model .StreamingOutputOperationResponse ;
48
50
import software .amazon .awssdk .utils .BinaryUtils ;
@@ -58,7 +60,7 @@ public class ResponseTransformerTest {
58
60
59
61
@ Test
60
62
public void bytesMethodConvertsCorrectly () {
61
- stubFor ( post ( urlPathEqualTo ( STREAMING_OUTPUT_PATH )). willReturn ( aResponse (). withStatus ( 200 ). withBody ( "test \uD83D \uDE02 " )) );
63
+ stubForSuccess ( );
62
64
63
65
ResponseBytes <StreamingOutputOperationResponse > response =
64
66
testClient ().streamingOutputOperationAsBytes (StreamingOutputOperationRequest .builder ().build ());
@@ -120,6 +122,38 @@ public void downloadToOutputStreamDoesNotRetry() throws IOException {
120
122
.isInstanceOf (SdkClientException .class );
121
123
}
122
124
125
+ @ Test
126
+ public void streamingCloseActuallyCloses () throws IOException {
127
+ stubForSuccess ();
128
+
129
+ ProtocolRestJsonClient client = testClientBuilder ()
130
+ .httpClientBuilder (ApacheHttpClient .builder ()
131
+ .connectionAcquisitionTimeout (Duration .ofSeconds (1 ))
132
+ .maxConnections (1 ))
133
+ .build ();
134
+
135
+
136
+ // Two successful requests with a max of one connection means that closing the connection worked.
137
+ client .streamingOutputOperation (StreamingOutputOperationRequest .builder ().build ()).close ();
138
+ client .streamingOutputOperation (StreamingOutputOperationRequest .builder ().build ()).close ();
139
+ }
140
+
141
+ @ Test
142
+ public void streamingAbortActuallyAborts () {
143
+ stubForSuccess ();
144
+
145
+ ProtocolRestJsonClient client = testClientBuilder ()
146
+ .httpClientBuilder (ApacheHttpClient .builder ()
147
+ .connectionAcquisitionTimeout (Duration .ofSeconds (1 ))
148
+ .maxConnections (1 ))
149
+ .build ();
150
+
151
+
152
+ // Two successful requests with a max of one connection means that closing the connection worked.
153
+ client .streamingOutputOperation (StreamingOutputOperationRequest .builder ().build ()).abort ();
154
+ client .streamingOutputOperation (StreamingOutputOperationRequest .builder ().build ()).abort ();
155
+ }
156
+
123
157
private void stubForRetriesTimeoutReadingFromStreams () {
124
158
stubFor (post (urlPathEqualTo (STREAMING_OUTPUT_PATH )).inScenario ("retries" )
125
159
.whenScenarioStateIs (STARTED )
@@ -133,11 +167,18 @@ private void stubForRetriesTimeoutReadingFromStreams() {
133
167
}
134
168
135
169
private ProtocolRestJsonClient testClient () {
170
+ return testClientBuilder ().build ();
171
+ }
172
+
173
+ private ProtocolRestJsonClientBuilder testClientBuilder () {
136
174
return ProtocolRestJsonClient .builder ()
137
175
.region (Region .US_WEST_1 )
138
176
.endpointOverride (URI .create ("http://localhost:" + wireMock .port ()))
139
177
.credentialsProvider (() -> AwsBasicCredentials .create ("akid" , "skid" ))
140
- .httpClientBuilder (ApacheHttpClient .builder ().socketTimeout (Duration .ofSeconds (1 )))
141
- .build ();
178
+ .httpClientBuilder (ApacheHttpClient .builder ().socketTimeout (Duration .ofSeconds (1 )));
179
+ }
180
+
181
+ private StubMapping stubForSuccess () {
182
+ return stubFor (post (urlPathEqualTo (STREAMING_OUTPUT_PATH )).willReturn (aResponse ().withStatus (200 ).withBody ("test \uD83D \uDE02 " )));
142
183
}
143
184
}
0 commit comments