Skip to content

Commit 316e698

Browse files
committed
add try-with-resource blocks
1 parent 9e590dc commit 316e698

4 files changed

Lines changed: 203 additions & 243 deletions

File tree

java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/ITGapicBidiUnbufferedWritableByteChannelTest.java

Lines changed: 107 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,23 @@ public void scenario1() throws Exception {
128128

129129
BidiWriteCtx<BidiResumableWrite> writeCtx = new BidiWriteCtx<>(resumableWrite);
130130
SettableApiFuture<BidiWriteObjectResponse> done = SettableApiFuture.create();
131-
//noinspection resource
132-
GapicBidiUnbufferedWritableByteChannel channel =
131+
try (GapicBidiUnbufferedWritableByteChannel channel =
133132
new GapicBidiUnbufferedWritableByteChannel(
134133
storageClient.bidiWriteObjectCallable(),
135134
RetrierWithAlg.attemptOnce(),
136135
done,
137136
CHUNK_SEGMENTER,
138137
writeCtx,
139-
GrpcCallContext::createDefault);
140-
141-
ByteBuffer bb = DataGenerator.base64Characters().genByteBuffer(_256KiB);
142-
StorageException se = assertThrows(StorageException.class, () -> channel.write(bb));
143-
assertAll(
144-
() -> assertThat(se.getCode()).isEqualTo(0),
145-
() -> assertThat(se.getReason()).isEqualTo("invalid"),
146-
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(0),
147-
() -> assertThat(channel.isOpen()).isFalse());
138+
GrpcCallContext::createDefault)) {
139+
140+
ByteBuffer bb = DataGenerator.base64Characters().genByteBuffer(_256KiB);
141+
StorageException se = assertThrows(StorageException.class, () -> channel.write(bb));
142+
assertAll(
143+
() -> assertThat(se.getCode()).isEqualTo(0),
144+
() -> assertThat(se.getReason()).isEqualTo("invalid"),
145+
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(0),
146+
() -> assertThat(channel.isOpen()).isFalse());
147+
}
148148
}
149149
}
150150

@@ -210,22 +210,22 @@ public void scenario2() throws Exception {
210210
writeCtx.getTotalSentBytes().set(_256KiB);
211211
writeCtx.getConfirmedBytes().set(_256KiB);
212212

213-
//noinspection resource
214-
GapicBidiUnbufferedWritableByteChannel channel =
213+
try (GapicBidiUnbufferedWritableByteChannel channel =
215214
new GapicBidiUnbufferedWritableByteChannel(
216215
storageClient.bidiWriteObjectCallable(),
217216
RetrierWithAlg.attemptOnce(),
218217
done,
219218
CHUNK_SEGMENTER,
220219
writeCtx,
221-
GrpcCallContext::createDefault);
222-
223-
StorageException se = assertThrows(StorageException.class, channel::close);
224-
assertAll(
225-
() -> assertThat(se.getCode()).isEqualTo(0),
226-
() -> assertThat(se.getReason()).isEqualTo("invalid"),
227-
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_256KiB),
228-
() -> assertThat(channel.isOpen()).isFalse());
220+
GrpcCallContext::createDefault)) {
221+
222+
StorageException se = assertThrows(StorageException.class, channel::close);
223+
assertAll(
224+
() -> assertThat(se.getCode()).isEqualTo(0),
225+
() -> assertThat(se.getReason()).isEqualTo("invalid"),
226+
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_256KiB),
227+
() -> assertThat(channel.isOpen()).isFalse());
228+
}
229229
}
230230
}
231231

@@ -291,22 +291,22 @@ public void scenario3() throws Exception {
291291
writeCtx.getTotalSentBytes().set(_512KiB);
292292
writeCtx.getConfirmedBytes().set(_512KiB);
293293

294-
//noinspection resource
295-
GapicBidiUnbufferedWritableByteChannel channel =
294+
try (GapicBidiUnbufferedWritableByteChannel channel =
296295
new GapicBidiUnbufferedWritableByteChannel(
297296
storageClient.bidiWriteObjectCallable(),
298297
RetrierWithAlg.attemptOnce(),
299298
done,
300299
CHUNK_SEGMENTER,
301300
writeCtx,
302-
GrpcCallContext::createDefault);
303-
304-
StorageException se = assertThrows(StorageException.class, channel::close);
305-
assertAll(
306-
() -> assertThat(se.getCode()).isEqualTo(0),
307-
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
308-
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_512KiB),
309-
() -> assertThat(channel.isOpen()).isFalse());
301+
GrpcCallContext::createDefault)) {
302+
303+
StorageException se = assertThrows(StorageException.class, channel::close);
304+
assertAll(
305+
() -> assertThat(se.getCode()).isEqualTo(0),
306+
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
307+
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_512KiB),
308+
() -> assertThat(channel.isOpen()).isFalse());
309+
}
310310
}
311311
}
312312

@@ -374,19 +374,20 @@ public void scenario4() throws Exception {
374374
writeCtx.getTotalSentBytes().set(_256KiB);
375375
writeCtx.getConfirmedBytes().set(_256KiB);
376376

377-
GapicBidiUnbufferedWritableByteChannel channel =
377+
try (GapicBidiUnbufferedWritableByteChannel channel =
378378
new GapicBidiUnbufferedWritableByteChannel(
379379
storageClient.bidiWriteObjectCallable(),
380380
RetrierWithAlg.attemptOnce(),
381381
done,
382382
CHUNK_SEGMENTER,
383383
writeCtx,
384-
GrpcCallContext::createDefault);
384+
GrpcCallContext::createDefault)) {
385385

386-
channel.close();
386+
channel.close();
387387

388-
BidiWriteObjectResponse BidiWriteObjectResponse = done.get(2, TimeUnit.SECONDS);
389-
assertThat(BidiWriteObjectResponse).isEqualTo(resp1);
388+
BidiWriteObjectResponse BidiWriteObjectResponse = done.get(2, TimeUnit.SECONDS);
389+
assertThat(BidiWriteObjectResponse).isEqualTo(resp1);
390+
}
390391
}
391392
}
392393

@@ -454,22 +455,22 @@ public void scenario4_1() throws Exception {
454455
writeCtx.getTotalSentBytes().set(_512KiB);
455456
writeCtx.getConfirmedBytes().set(_512KiB);
456457

457-
//noinspection resource
458-
GapicBidiUnbufferedWritableByteChannel channel =
458+
try (GapicBidiUnbufferedWritableByteChannel channel =
459459
new GapicBidiUnbufferedWritableByteChannel(
460460
storageClient.bidiWriteObjectCallable(),
461461
RetrierWithAlg.attemptOnce(),
462462
done,
463463
CHUNK_SEGMENTER,
464464
writeCtx,
465-
GrpcCallContext::createDefault);
466-
467-
StorageException se = assertThrows(StorageException.class, channel::close);
468-
assertAll(
469-
() -> assertThat(se.getCode()).isEqualTo(0),
470-
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
471-
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_512KiB),
472-
() -> assertThat(channel.isOpen()).isFalse());
465+
GrpcCallContext::createDefault)) {
466+
467+
StorageException se = assertThrows(StorageException.class, channel::close);
468+
assertAll(
469+
() -> assertThat(se.getCode()).isEqualTo(0),
470+
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
471+
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_512KiB),
472+
() -> assertThat(channel.isOpen()).isFalse());
473+
}
473474
}
474475
}
475476

@@ -537,22 +538,22 @@ public void scenario4_2() throws Exception {
537538
writeCtx.getTotalSentBytes().set(_512KiB);
538539
writeCtx.getConfirmedBytes().set(_512KiB);
539540

540-
//noinspection resource
541-
GapicBidiUnbufferedWritableByteChannel channel =
541+
try (GapicBidiUnbufferedWritableByteChannel channel =
542542
new GapicBidiUnbufferedWritableByteChannel(
543543
storageClient.bidiWriteObjectCallable(),
544544
RetrierWithAlg.attemptOnce(),
545545
done,
546546
CHUNK_SEGMENTER,
547547
writeCtx,
548-
GrpcCallContext::createDefault);
549-
550-
StorageException se = assertThrows(StorageException.class, channel::close);
551-
assertAll(
552-
() -> assertThat(se.getCode()).isEqualTo(0),
553-
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
554-
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_512KiB),
555-
() -> assertThat(channel.isOpen()).isFalse());
548+
GrpcCallContext::createDefault)) {
549+
550+
StorageException se = assertThrows(StorageException.class, channel::close);
551+
assertAll(
552+
() -> assertThat(se.getCode()).isEqualTo(0),
553+
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
554+
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_512KiB),
555+
() -> assertThat(channel.isOpen()).isFalse());
556+
}
556557
}
557558
}
558559

@@ -630,23 +631,23 @@ public void scenario5() throws Exception {
630631
writeCtx.getTotalSentBytes().set(_256KiB);
631632
writeCtx.getConfirmedBytes().set(_256KiB);
632633

633-
//noinspection resource
634-
GapicBidiUnbufferedWritableByteChannel channel =
634+
try (GapicBidiUnbufferedWritableByteChannel channel =
635635
new GapicBidiUnbufferedWritableByteChannel(
636636
storageClient.bidiWriteObjectCallable(),
637637
RetrierWithAlg.attemptOnce(),
638638
done,
639639
CHUNK_SEGMENTER,
640640
writeCtx,
641-
GrpcCallContext::createDefault);
642-
643-
ByteBuffer bb = DataGenerator.base64Characters().genByteBuffer(_256KiB);
644-
StorageException se = assertThrows(StorageException.class, () -> channel.write(bb));
645-
assertAll(
646-
() -> assertThat(se.getCode()).isEqualTo(0),
647-
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
648-
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_256KiB),
649-
() -> assertThat(channel.isOpen()).isFalse());
641+
GrpcCallContext::createDefault)) {
642+
643+
ByteBuffer bb = DataGenerator.base64Characters().genByteBuffer(_256KiB);
644+
StorageException se = assertThrows(StorageException.class, () -> channel.write(bb));
645+
assertAll(
646+
() -> assertThat(se.getCode()).isEqualTo(0),
647+
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
648+
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_256KiB),
649+
() -> assertThat(channel.isOpen()).isFalse());
650+
}
650651
}
651652
}
652653

@@ -694,23 +695,23 @@ public void scenario7() throws Exception {
694695
BidiResumableWrite resumableWrite = getResumableWrite(uploadId);
695696
BidiWriteCtx<BidiResumableWrite> writeCtx = new BidiWriteCtx<>(resumableWrite);
696697

697-
//noinspection resource
698-
GapicBidiUnbufferedWritableByteChannel channel =
698+
try (GapicBidiUnbufferedWritableByteChannel channel =
699699
new GapicBidiUnbufferedWritableByteChannel(
700700
storageClient.bidiWriteObjectCallable(),
701701
RetrierWithAlg.attemptOnce(),
702702
done,
703703
CHUNK_SEGMENTER,
704704
writeCtx,
705-
GrpcCallContext::createDefault);
706-
707-
ByteBuffer buf = DataGenerator.base64Characters().genByteBuffer(_256KiB);
708-
StorageException se = assertThrows(StorageException.class, () -> channel.write(buf));
709-
assertAll(
710-
() -> assertThat(se.getCode()).isEqualTo(0),
711-
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
712-
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(0),
713-
() -> assertThat(channel.isOpen()).isFalse());
705+
GrpcCallContext::createDefault)) {
706+
707+
ByteBuffer buf = DataGenerator.base64Characters().genByteBuffer(_256KiB);
708+
StorageException se = assertThrows(StorageException.class, () -> channel.write(buf));
709+
assertAll(
710+
() -> assertThat(se.getCode()).isEqualTo(0),
711+
() -> assertThat(se.getReason()).isEqualTo("dataLoss"),
712+
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(0),
713+
() -> assertThat(channel.isOpen()).isFalse());
714+
}
714715
}
715716
}
716717

@@ -744,23 +745,23 @@ public void incremental_success() throws Exception {
744745
BidiResumableWrite resumableWrite = getResumableWrite(uploadId);
745746
BidiWriteCtx<BidiResumableWrite> writeCtx = new BidiWriteCtx<>(resumableWrite);
746747

747-
//noinspection resource
748-
GapicBidiUnbufferedWritableByteChannel channel =
748+
try (GapicBidiUnbufferedWritableByteChannel channel =
749749
new GapicBidiUnbufferedWritableByteChannel(
750750
storageClient.bidiWriteObjectCallable(),
751751
RetrierWithAlg.attemptOnce(),
752752
done,
753753
CHUNK_SEGMENTER,
754754
writeCtx,
755-
GrpcCallContext::createDefault);
756-
757-
ByteBuffer buf = DataGenerator.base64Characters().genByteBuffer(_256KiB);
758-
int written = channel.write(buf);
759-
assertAll(
760-
() -> assertThat(buf.remaining()).isEqualTo(0),
761-
() -> assertThat(written).isEqualTo(_256KiB),
762-
() -> assertThat(writeCtx.getTotalSentBytes().get()).isEqualTo(_256KiB),
763-
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_256KiB));
755+
GrpcCallContext::createDefault)) {
756+
757+
ByteBuffer buf = DataGenerator.base64Characters().genByteBuffer(_256KiB);
758+
int written = channel.write(buf);
759+
assertAll(
760+
() -> assertThat(buf.remaining()).isEqualTo(0),
761+
() -> assertThat(written).isEqualTo(_256KiB),
762+
() -> assertThat(writeCtx.getTotalSentBytes().get()).isEqualTo(_256KiB),
763+
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_256KiB));
764+
}
764765
}
765766
}
766767

@@ -796,29 +797,29 @@ public void incremental_partialSuccess() throws Exception {
796797

797798
ChunkSegmenter chunkSegmenter =
798799
new ChunkSegmenter(Hasher.noop(), ByteStringStrategy.copy(), _512KiB, _256KiB);
799-
//noinspection resource
800-
GapicBidiUnbufferedWritableByteChannel channel =
800+
try (GapicBidiUnbufferedWritableByteChannel channel =
801801
new GapicBidiUnbufferedWritableByteChannel(
802802
storageClient.bidiWriteObjectCallable(),
803803
RetrierWithAlg.attemptOnce(),
804804
done,
805805
chunkSegmenter,
806806
writeCtx,
807-
GrpcCallContext::createDefault);
808-
809-
ByteBuffer buf = DataGenerator.base64Characters().genByteBuffer(_512KiB);
810-
int written = channel.write(buf);
811-
assertAll(
812-
() -> assertThat(buf.remaining()).isEqualTo(_256KiB),
813-
() -> assertThat(written).isEqualTo(_256KiB),
814-
() ->
815-
assertWithMessage("totalSentBytes")
816-
.that(writeCtx.getTotalSentBytes().get())
817-
.isEqualTo(_256KiB),
818-
() ->
819-
assertWithMessage("confirmedBytes")
820-
.that(writeCtx.getConfirmedBytes().get())
821-
.isEqualTo(_256KiB));
807+
GrpcCallContext::createDefault)) {
808+
809+
ByteBuffer buf = DataGenerator.base64Characters().genByteBuffer(_512KiB);
810+
int written = channel.write(buf);
811+
assertAll(
812+
() -> assertThat(buf.remaining()).isEqualTo(_256KiB),
813+
() -> assertThat(written).isEqualTo(_256KiB),
814+
() ->
815+
assertWithMessage("totalSentBytes")
816+
.that(writeCtx.getTotalSentBytes().get())
817+
.isEqualTo(_256KiB),
818+
() ->
819+
assertWithMessage("confirmedBytes")
820+
.that(writeCtx.getConfirmedBytes().get())
821+
.isEqualTo(_256KiB));
822+
}
822823
}
823824
}
824825

0 commit comments

Comments
 (0)