24
24
import static com .google .cloud .firestore .LocalFirestoreHelper .update ;
25
25
import static org .junit .Assert .assertArrayEquals ;
26
26
import static org .junit .Assert .assertEquals ;
27
+ import static org .junit .Assert .assertFalse ;
27
28
import static org .junit .Assert .assertTrue ;
28
29
import static org .junit .Assert .fail ;
29
30
import static org .mockito .Mockito .*;
@@ -97,24 +98,13 @@ public class BulkWriterTest {
97
98
98
99
@ Rule public Timeout timeout = new Timeout (2 , TimeUnit .SECONDS );
99
100
100
- @ Spy private final FirestoreRpc firestoreRpc = Mockito .mock (FirestoreRpc .class );
101
-
102
101
private ScheduledExecutorService testExecutor ;
103
102
104
- /** Executor that executes delayed tasks without delay. */
105
- private final ScheduledExecutorService immediateExecutor =
106
- new ScheduledThreadPoolExecutor (1 ) {
107
- @ Override
108
- @ Nonnull
109
- public ScheduledFuture <?> schedule (Runnable command , long delay , TimeUnit unit ) {
110
- return super .schedule (command , 0 , TimeUnit .MILLISECONDS );
111
- }
112
- };
113
-
114
103
@ Spy
115
104
private final FirestoreImpl firestoreMock =
116
105
new FirestoreImpl (
117
- FirestoreOptions .newBuilder ().setProjectId ("test-project" ).build (), firestoreRpc );
106
+ FirestoreOptions .newBuilder ().setProjectId ("test-project" ).build (),
107
+ Mockito .mock (FirestoreRpc .class ));
118
108
119
109
@ Captor private ArgumentCaptor <BatchWriteRequest > batchWriteCapture ;
120
110
@@ -155,7 +145,6 @@ public static ApiFuture<BatchWriteResponse> mergeResponses(
155
145
156
146
@ Before
157
147
public void before () {
158
- lenient ().doReturn (immediateExecutor ).when (firestoreRpc ).getExecutor ();
159
148
testExecutor = Executors .newSingleThreadScheduledExecutor ();
160
149
161
150
timeoutExecutor =
@@ -169,23 +158,27 @@ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
169
158
170
159
bulkWriter =
171
160
firestoreMock .bulkWriter (BulkWriterOptions .builder ().setExecutor (timeoutExecutor ).build ());
161
+ bulkWriter .autoShutdownBulkWriterExecutor = true ;
172
162
doc1 = firestoreMock .document ("coll/doc1" );
173
163
doc2 = firestoreMock .document ("coll/doc2" );
174
164
}
175
165
176
166
@ After
177
167
public void after () throws InterruptedException {
168
+ shutdownScheduledExecutorService (testExecutor );
178
169
shutdownScheduledExecutorService (timeoutExecutor );
179
170
}
180
171
181
172
void shutdownScheduledExecutorService (ScheduledExecutorService executorService )
182
173
throws InterruptedException {
174
+ executorService .shutdown ();
183
175
// Wait for the executor to finish after each test.
184
176
//
185
177
// This ensures the executor service is shut down properly within the given timeout, and thereby
186
178
// avoids potential hangs caused by lingering threads. Note that if a given thread is terminated
187
179
// because of the timeout, the associated test will fail, which is what we want.
188
180
executorService .awaitTermination (100 , TimeUnit .MILLISECONDS );
181
+ assertTrue (executorService .isTerminated ());
189
182
}
190
183
191
184
@ Test
@@ -369,12 +362,19 @@ public void cannotCallMethodsAfterClose() throws Exception {
369
362
} catch (Exception e ) {
370
363
assertEquals (expected , e .getMessage ());
371
364
}
372
- try {
373
- bulkWriter .close ();
374
- fail ("close() should have failed" );
375
- } catch (Exception e ) {
376
- assertEquals (expected , e .getMessage ());
377
- }
365
+ // Close is idempotent and can be called multiple time.
366
+ bulkWriter .close ();
367
+ }
368
+
369
+ @ Test
370
+ public void closeWillShutdownExecutor () throws Exception {
371
+ // We ONLY shutdown executor when the executor was created within the BulkWriter.
372
+ // To simulate this, we set the autoShutdownBulkWriterExecutor field to true.
373
+ bulkWriter .autoShutdownBulkWriterExecutor = true ;
374
+
375
+ assertFalse (timeoutExecutor .isShutdown ());
376
+ bulkWriter .close ();
377
+ assertTrue (timeoutExecutor .isShutdown ());
378
378
}
379
379
380
380
@ Test
0 commit comments