1919import static org .assertj .core .api .Assertions .assertThatThrownBy ;
2020
2121import java .io .IOException ;
22+ import java .io .InputStream ;
2223import java .io .PipedInputStream ;
2324import java .io .PipedOutputStream ;
2425import java .nio .ByteBuffer ;
2526import java .util .concurrent .ExecutorService ;
2627import java .util .concurrent .Executors ;
27- import org .junit .jupiter .api .Test ;
28+ import java .util .function .BiFunction ;
29+ import java .util .stream .Stream ;
30+ import org .junit .jupiter .api .AfterAll ;
31+ import org .junit .jupiter .api .BeforeAll ;
2832import org .junit .jupiter .api .Timeout ;
33+ import org .junit .jupiter .params .ParameterizedTest ;
34+ import org .junit .jupiter .params .provider .Arguments ;
35+ import org .junit .jupiter .params .provider .MethodSource ;
2936import software .amazon .awssdk .core .async .AsyncRequestBody ;
3037import software .amazon .awssdk .utils .async .ByteBufferStoringSubscriber ;
3138import software .amazon .awssdk .utils .async .ByteBufferStoringSubscriber .TransferResult ;
3239
3340class InputStreamWithExecutorAsyncRequestBodyTest {
34- @ Test
35- @ Timeout (10 )
36- public void dataFromInputStreamIsCopied () throws Exception {
37- ExecutorService executor = Executors .newSingleThreadExecutor ();
38- try {
39- PipedOutputStream os = new PipedOutputStream ();
40- PipedInputStream is = new PipedInputStream (os );
41-
42- InputStreamWithExecutorAsyncRequestBody asyncRequestBody =
43- (InputStreamWithExecutorAsyncRequestBody ) AsyncRequestBody .fromInputStream (b -> b .inputStream (is ).executor (executor ).contentLength (4L ));
44-
45- ByteBufferStoringSubscriber subscriber = new ByteBufferStoringSubscriber (8 );
46- asyncRequestBody .subscribe (subscriber );
47-
48- os .write (0 );
49- os .write (1 );
50- os .write (2 );
51- os .write (3 );
52- os .close ();
53-
54- asyncRequestBody .activeWriteFuture ().get ();
55-
56- ByteBuffer output = ByteBuffer .allocate (8 );
57- assertThat (subscriber .transferTo (output )).isEqualTo (TransferResult .END_OF_STREAM );
58- output .flip ();
59-
60- assertThat (output .remaining ()).isEqualTo (4 );
61- assertThat (output .get ()).isEqualTo ((byte ) 0 );
62- assertThat (output .get ()).isEqualTo ((byte ) 1 );
63- assertThat (output .get ()).isEqualTo ((byte ) 2 );
64- assertThat (output .get ()).isEqualTo ((byte ) 3 );
65- } finally {
66- executor .shutdownNow ();
67- }
41+
42+ private static ExecutorService customerExecutor ;
43+
44+ @ BeforeAll
45+ static void setUp () {
46+ customerExecutor = Executors .newSingleThreadExecutor ();
47+ }
48+
49+ @ AfterAll
50+ static void tearDown () {
51+ customerExecutor .shutdownNow ();
52+ }
53+
54+ static Stream <Arguments > requestBodyFactories () {
55+ return Stream .of (
56+ Arguments .of ("customerProvidedExecutor" ,
57+ (BiFunction <InputStream , Long , AsyncRequestBody >) (is , len ) ->
58+ AsyncRequestBody .fromInputStream (is , len , customerExecutor )),
59+ Arguments .of ("sdkManagedExecutor_twoArgOverload" ,
60+ (BiFunction <InputStream , Long , AsyncRequestBody >) AsyncRequestBody ::fromInputStream ),
61+ Arguments .of ("sdkManagedExecutor_builderWithoutExecutor" ,
62+ (BiFunction <InputStream , Long , AsyncRequestBody >) (is , len ) ->
63+ AsyncRequestBody .fromInputStream (b -> b .inputStream (is ).contentLength (len )))
64+ );
6865 }
6966
70- @ Test
67+ @ ParameterizedTest (name = "{0}" )
68+ @ MethodSource ("requestBodyFactories" )
7169 @ Timeout (10 )
72- public void errorsReadingInputStreamAreForwardedToSubscriber () throws Exception {
73- ExecutorService executor = Executors .newSingleThreadExecutor ();
74- try {
75- PipedOutputStream os = new PipedOutputStream ();
76- PipedInputStream is = new PipedInputStream (os );
70+ public void dataFromInputStreamIsCopied (String name ,
71+ BiFunction <InputStream , Long , AsyncRequestBody > factory ) throws Exception {
72+ PipedOutputStream os = new PipedOutputStream ();
73+ PipedInputStream is = new PipedInputStream (os );
74+
75+ InputStreamWithExecutorAsyncRequestBody asyncRequestBody =
76+ (InputStreamWithExecutorAsyncRequestBody ) factory .apply (is , 4L );
7777
78- is .close ();
78+ ByteBufferStoringSubscriber subscriber = new ByteBufferStoringSubscriber (8 );
79+ asyncRequestBody .subscribe (subscriber );
80+
81+ os .write (0 );
82+ os .write (1 );
83+ os .write (2 );
84+ os .write (3 );
85+ os .close ();
86+
87+ asyncRequestBody .activeWriteFuture ().get ();
88+
89+ ByteBuffer output = ByteBuffer .allocate (8 );
90+ assertThat (subscriber .transferTo (output )).isEqualTo (TransferResult .END_OF_STREAM );
91+ output .flip ();
92+
93+ assertThat (output .remaining ()).isEqualTo (4 );
94+ assertThat (output .get ()).isEqualTo ((byte ) 0 );
95+ assertThat (output .get ()).isEqualTo ((byte ) 1 );
96+ assertThat (output .get ()).isEqualTo ((byte ) 2 );
97+ assertThat (output .get ()).isEqualTo ((byte ) 3 );
98+ }
99+
100+ @ ParameterizedTest (name = "{0}" )
101+ @ MethodSource ("requestBodyFactories" )
102+ @ Timeout (10 )
103+ public void errorsReadingInputStreamAreForwardedToSubscriber (String name ,
104+ BiFunction <InputStream , Long , AsyncRequestBody > factory ) throws Exception {
105+ PipedOutputStream os = new PipedOutputStream ();
106+ PipedInputStream is = new PipedInputStream (os );
79107
80- InputStreamWithExecutorAsyncRequestBody asyncRequestBody =
81- (InputStreamWithExecutorAsyncRequestBody ) AsyncRequestBody .fromInputStream (b -> b .inputStream (is ).executor (executor ).contentLength (4L ));
108+ is .close ();
82109
110+ InputStreamWithExecutorAsyncRequestBody asyncRequestBody =
111+ (InputStreamWithExecutorAsyncRequestBody ) factory .apply (is , 4L );
83112
84- ByteBufferStoringSubscriber subscriber = new ByteBufferStoringSubscriber (8 );
85- asyncRequestBody .subscribe (subscriber );
86- assertThatThrownBy (() -> asyncRequestBody .activeWriteFuture ().get ()).hasRootCauseInstanceOf (IOException .class );
87- assertThatThrownBy (() -> subscriber .transferTo (ByteBuffer .allocate (8 ))).hasRootCauseInstanceOf (IOException .class );
88- } finally {
89- executor .shutdownNow ();
90- }
113+ ByteBufferStoringSubscriber subscriber = new ByteBufferStoringSubscriber (8 );
114+ asyncRequestBody .subscribe (subscriber );
115+ assertThatThrownBy (() -> asyncRequestBody .activeWriteFuture ().get ()).hasRootCauseInstanceOf (IOException .class );
116+ assertThatThrownBy (() -> subscriber .transferTo (ByteBuffer .allocate (8 ))).hasRootCauseInstanceOf (IOException .class );
91117 }
92- }
118+ }
0 commit comments