@@ -202,6 +202,64 @@ public void testWriteWithFlushRetryChunkWithDrift() throws IOException {
202202 assertArrayEquals (buffer .array (), capturedBuffer .getValue ());
203203 }
204204
205+ @ Test
206+ public void testWriteWithLastFlushRetryChunkButCompleted () throws IOException {
207+ StorageException exception = new StorageException (new SocketException ("Socket closed" ));
208+ ByteBuffer buffer = randomBuffer (MIN_CHUNK_SIZE );
209+ Capture <byte []> capturedBuffer = Capture .newInstance ();
210+ expect (storageRpcMock .open (BLOB_INFO .toPb (), EMPTY_RPC_OPTIONS )).andReturn (UPLOAD_ID );
211+ expect (
212+ storageRpcMock .writeWithResponse (
213+ eq (UPLOAD_ID ),
214+ capture (capturedBuffer ),
215+ eq (0 ),
216+ eq (0L ),
217+ eq (MIN_CHUNK_SIZE ),
218+ eq (true )))
219+ .andThrow (exception );
220+ expect (storageRpcMock .getCurrentUploadOffset (eq (UPLOAD_ID ))).andReturn (-1L );
221+ replay (storageRpcMock );
222+ writer = new BlobWriteChannel (options , BLOB_INFO , EMPTY_RPC_OPTIONS );
223+ assertEquals (MIN_CHUNK_SIZE , writer .write (buffer ));
224+ writer .close ();
225+ assertFalse (writer .isRetrying ());
226+ assertFalse (writer .isOpen ());
227+ // Capture captures entire buffer of a chunk even when not completely used.
228+ // Making assert selective up to the size of MIN_CHUNK_SIZE
229+ assertArrayEquals (Arrays .copyOf (capturedBuffer .getValue (), MIN_CHUNK_SIZE ), buffer .array ());
230+ }
231+
232+ @ Test
233+ public void testWriteWithFlushRetryChunkButCompletedByAnotherClient () throws IOException {
234+ StorageException exception = new StorageException (new SocketException ("Socket closed" ));
235+ StorageException completedException =
236+ new StorageException (0 , "Resumable upload is already complete." );
237+ ByteBuffer buffer = randomBuffer (MIN_CHUNK_SIZE );
238+ Capture <byte []> capturedBuffer = Capture .newInstance ();
239+ expect (storageRpcMock .open (BLOB_INFO .toPb (), EMPTY_RPC_OPTIONS )).andReturn (UPLOAD_ID );
240+ expect (
241+ storageRpcMock .writeWithResponse (
242+ eq (UPLOAD_ID ),
243+ capture (capturedBuffer ),
244+ eq (0 ),
245+ eq (0L ),
246+ eq (MIN_CHUNK_SIZE ),
247+ eq (false )))
248+ .andThrow (exception );
249+ expect (storageRpcMock .getCurrentUploadOffset (eq (UPLOAD_ID ))).andReturn (-1L );
250+ replay (storageRpcMock );
251+ writer = new BlobWriteChannel (options , BLOB_INFO , EMPTY_RPC_OPTIONS );
252+ writer .setChunkSize (MIN_CHUNK_SIZE );
253+ try {
254+ writer .write (buffer );
255+ fail ("Expected completed exception." );
256+ } catch (StorageException ex ) {
257+ assertEquals (ex , completedException );
258+ }
259+ assertTrue (writer .isRetrying ());
260+ assertTrue (writer .isOpen ());
261+ }
262+
205263 @ Test
206264 public void testWriteWithFlush () throws IOException {
207265 expect (storageRpcMock .open (BLOB_INFO .toPb (), EMPTY_RPC_OPTIONS )).andReturn (UPLOAD_ID );
0 commit comments