Skip to content

Commit c5b5a44

Browse files
authored
Rename shutdown to terminate and publish it. (#3694)
* Rename shutdown to terminate and publish it. * Addressing feedbacks
1 parent 423cab7 commit c5b5a44

File tree

13 files changed

+115
-88
lines changed

13 files changed

+115
-88
lines changed

Firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
- [feature] Added a `waitForPendingWrites()` method to `FIRFirestore` class
66
which allows users to wait on a promise that resolves when all pending
77
writes are acknowledged by the Firestore backend.
8+
- [feature] Added a `terminate()` method to `FIRFirestore` which terminates
9+
the instance, releasing any held resources. Once it completes, you can
10+
optionally call `clearPersistence()` to wipe persisted Firestore data
11+
from disk.
812

913
# v1.4.5
1014
- [fixed] Fixed a crash that would happen when changing networks or going from

Firestore/Example/Tests/Integration/API/FIRDatabaseTests.mm

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,21 +1224,21 @@ - (void)testCanDisableNetwork {
12241224
[self awaitExpectations];
12251225
}
12261226

1227-
- (void)testClientCallsAfterShutdownFail {
1227+
- (void)testClientCallsAfterTerminationFail {
12281228
FIRDocumentReference *doc = [self documentRef];
12291229
FIRFirestore *firestore = doc.firestore;
12301230

12311231
[firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
12321232
[self awaitExpectations];
1233-
[firestore shutdownWithCompletion:[self completionForExpectationWithName:@"Shutdown"]];
1233+
[firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate"]];
12341234
[self awaitExpectations];
12351235

12361236
XCTAssertThrowsSpecific(
12371237
{
12381238
[firestore disableNetworkWithCompletion:^(NSError *error){
12391239
}];
12401240
},
1241-
NSException, @"The client has already been shutdown.");
1241+
NSException, @"The client has already been terminated.");
12421242
}
12431243

12441244
- (void)testMaintainsPersistenceAfterRestarting {
@@ -1251,9 +1251,9 @@ - (void)testMaintainsPersistenceAfterRestarting {
12511251
NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
12521252
[self writeDocumentRef:doc data:initialData];
12531253

1254-
// -clearPersistence() requires Firestore to be shut down. Shutdown FIRApp and remove the
1254+
// -clearPersistence() requires Firestore to be terminated. Shutdown FIRApp and remove the
12551255
// firestore instance to emulate the way an end user would do this.
1256-
[self shutdownFirestore:firestore];
1256+
[self terminateFirestore:firestore];
12571257
[self.firestores removeObject:firestore];
12581258
[self deleteApp:app];
12591259

@@ -1278,9 +1278,9 @@ - (void)testCanClearPersistenceAfterRestarting {
12781278
NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
12791279
[self writeDocumentRef:doc data:initialData];
12801280

1281-
// -clearPersistence() requires Firestore to be shut down. Shutdown FIRApp and remove the
1281+
// -clearPersistence() requires Firestore to be terminated. Shutdown FIRApp and remove the
12821282
// firestore instance to emulate the way an end user would do this.
1283-
[self shutdownFirestore:firestore];
1283+
[self terminateFirestore:firestore];
12841284
[self.firestores removeObject:firestore];
12851285
[firestore
12861286
clearPersistenceWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
@@ -1332,7 +1332,7 @@ - (void)testRestartFirestoreLeadsToNewInstance {
13321332
@{@"owner" : @{@"name" : @"Jonny", @"email" : @"[email protected]"}};
13331333
[self writeDocumentRef:[firestore documentWithPath:@"abc/123"] data:data];
13341334

1335-
[self shutdownFirestore:firestore];
1335+
[self terminateFirestore:firestore];
13361336

13371337
// Create a new instance, check it's a different instance.
13381338
FIRFirestore *newInstance = [FIRFirestore firestoreForApp:app];
@@ -1345,7 +1345,7 @@ - (void)testRestartFirestoreLeadsToNewInstance {
13451345
XCTAssertTrue([data isEqualToDictionary:[snapshot data]]);
13461346
}
13471347

1348-
- (void)testAppDeleteLeadsToFirestoreShutdown {
1348+
- (void)testAppDeleteLeadsToFirestoreTermination {
13491349
FIRApp *app = testutil::AppForUnitTesting(util::MakeString([FSTIntegrationTestCase projectID]));
13501350
FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
13511351
firestore.settings = [FSTIntegrationTestCase settings];
@@ -1355,33 +1355,33 @@ - (void)testAppDeleteLeadsToFirestoreShutdown {
13551355

13561356
[self deleteApp:app];
13571357

1358-
XCTAssertTrue(firestore.wrapped->client()->is_shutdown());
1358+
XCTAssertTrue(firestore.wrapped->client()->is_terminated());
13591359
}
13601360

1361-
- (void)testShutdownCanBeCalledMultipleTimes {
1361+
- (void)testTerminateCanBeCalledMultipleTimes {
13621362
FIRApp *app = testutil::AppForUnitTesting(util::MakeString([FSTIntegrationTestCase projectID]));
13631363
FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
13641364

1365-
[firestore shutdownWithCompletion:[self completionForExpectationWithName:@"Shutdown1"]];
1365+
[firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate1"]];
13661366
[self awaitExpectations];
13671367
XCTAssertThrowsSpecific(
13681368
{
13691369
[firestore disableNetworkWithCompletion:^(NSError *error){
13701370
}];
13711371
},
1372-
NSException, @"The client has already been shutdown.");
1372+
NSException, @"The client has already been terminated.");
13731373

1374-
[firestore shutdownWithCompletion:[self completionForExpectationWithName:@"Shutdown2"]];
1374+
[firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate2"]];
13751375
[self awaitExpectations];
13761376
XCTAssertThrowsSpecific(
13771377
{
13781378
[firestore enableNetworkWithCompletion:^(NSError *error){
13791379
}];
13801380
},
1381-
NSException, @"The client has already been shutdown.");
1381+
NSException, @"The client has already been terminated.");
13821382
}
13831383

1384-
- (void)testCanRemoveListenerAfterShutdown {
1384+
- (void)testCanRemoveListenerAfterTermination {
13851385
FIRApp *app = testutil::AppForUnitTesting(util::MakeString([FSTIntegrationTestCase projectID]));
13861386
FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
13871387
firestore.settings = [FSTIntegrationTestCase settings];
@@ -1393,7 +1393,7 @@ - (void)testCanRemoveListenerAfterShutdown {
13931393
[doc addSnapshotListener:[accumulator valueEventHandler]];
13941394
[accumulator awaitEventWithName:@"Snapshot"];
13951395

1396-
[firestore shutdownWithCompletion:[self completionForExpectationWithName:@"shutdown"]];
1396+
[firestore terminateWithCompletion:[self completionForExpectationWithName:@"terminate"]];
13971397
[self awaitExpectations];
13981398

13991399
// This should proceed without error.

Firestore/Example/Tests/Util/FSTIntegrationTestCase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ extern "C" {
6363
*/
6464
- (FIRFirestore *)firestoreWithApp:(FIRApp *)app;
6565

66-
/** Synchronously shuts down the given firestore. */
67-
- (void)shutdownFirestore:(FIRFirestore *)firestore;
66+
/** Synchronously terminates the given firestore. */
67+
- (void)terminateFirestore:(FIRFirestore *)firestore;
6868

6969
/** Synchronously deletes the given FIRapp. */
7070
- (void)deleteApp:(FIRApp *)app;

Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ - (void)setUp {
125125
- (void)tearDown {
126126
@try {
127127
for (FIRFirestore *firestore in _firestores) {
128-
[self shutdownFirestore:firestore];
128+
[self terminateFirestore:firestore];
129129
}
130130
} @finally {
131131
_firestores = nil;
@@ -327,12 +327,12 @@ - (void)primeBackend {
327327

328328
[listenerRegistration remove];
329329

330-
[self shutdownFirestore:db];
330+
[self terminateFirestore:db];
331331
});
332332
}
333333

334-
- (void)shutdownFirestore:(FIRFirestore *)firestore {
335-
[firestore shutdownWithCompletion:[self completionForExpectationWithName:@"shutdown"]];
334+
- (void)terminateFirestore:(FIRFirestore *)firestore {
335+
[firestore terminateWithCompletion:[self completionForExpectationWithName:@"shutdown"]];
336336
[self awaitExpectations];
337337
}
338338

Firestore/Source/API/FIRFirestore+Internal.h

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,7 @@ NS_ASSUME_NONNULL_BEGIN
6464

6565
+ (FIRFirestore *)recoverFromFirestore:(std::shared_ptr<api::Firestore>)firestore;
6666

67-
/**
68-
* Shuts down this `FIRFirestore` instance.
69-
*
70-
* After shutdown only the `clearPersistence` method may be used. Any other method
71-
* will throw an error.
72-
*
73-
* To restart after shutdown, simply create a new instance of FIRFirestore with
74-
* `firestore` or `firestoreForApp` methods.
75-
*
76-
* Shutdown does not cancel any pending writes and any tasks that are awaiting a response from
77-
* the server will not be resolved. The next time you start this instance, it will resume
78-
* attempting to send these writes to the server.
79-
*
80-
* Note: Under normal circumstances, calling this method is not required. This
81-
* method is useful only when you want to force this instance to release all of its resources or
82-
* in combination with `clearPersistence` to ensure that all local state is destroyed
83-
* between test runs.
84-
*
85-
* @param completion A block to execute once everything has shut down.
86-
*/
87-
// TODO(b/135755126): Make this public.
88-
- (void)shutdownWithCompletion:(nullable void (^)(NSError *_Nullable error))completion
89-
NS_SWIFT_NAME(shutdown(completion:));
90-
91-
- (void)shutdownInternalWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
67+
- (void)terminateInternalWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
9268

9369
- (const std::shared_ptr<util::AsyncQueue> &)workerQueue;
9470

Firestore/Source/API/FIRFirestore.mm

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ - (void)waitForPendingWritesWithCompletion:(void (^)(NSError *_Nullable error))c
281281
_firestore->WaitForPendingWrites(util::MakeCallback(completion));
282282
}
283283

284+
- (void)terminateWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
285+
id<FSTFirestoreInstanceRegistry> strongRegistry = _registry;
286+
if (strongRegistry) {
287+
[strongRegistry
288+
removeInstanceWithDatabase:util::MakeNSString(_firestore->database_id().database_id())];
289+
}
290+
[self terminateInternalWithCompletion:completion];
291+
}
292+
284293
@end
285294

286295
@implementation FIRFirestore (Internal)
@@ -305,17 +314,8 @@ + (FIRFirestore *)recoverFromFirestore:(std::shared_ptr<Firestore>)firestore {
305314
return (__bridge FIRFirestore *)firestore->extension();
306315
}
307316

308-
- (void)shutdownInternalWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
309-
_firestore->Shutdown(util::MakeCallback(completion));
310-
}
311-
312-
- (void)shutdownWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
313-
id<FSTFirestoreInstanceRegistry> strongRegistry = _registry;
314-
if (strongRegistry) {
315-
[strongRegistry
316-
removeInstanceWithDatabase:util::MakeNSString(_firestore->database_id().database_id())];
317-
}
318-
[self shutdownInternalWithCompletion:completion];
317+
- (void)terminateInternalWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
318+
_firestore->Terminate(util::MakeCallback(completion));
319319
}
320320

321321
@end

Firestore/Source/API/FSTFirestoreComponent.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ - (void)appWillBeDeleted:(FIRApp *)app {
134134
[_instances removeAllObjects];
135135
}
136136
for (NSString *key in instances) {
137-
[instances[key] shutdownInternalWithCompletion:nil];
137+
[instances[key] terminateInternalWithCompletion:nil];
138138
}
139139
}
140140

Firestore/Source/Public/FIRFirestore.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,31 @@ NS_SWIFT_NAME(Firestore)
208208
*/
209209
- (void)waitForPendingWritesWithCompletion:(void (^)(NSError *_Nullable error))completion;
210210

211+
#pragma mark - Terminating
212+
213+
/**
214+
* Terminates this `FIRFirestore` instance.
215+
*
216+
* After calling `terminate` only the `clearPersistence` method may be used. Any other method
217+
* will throw an error.
218+
*
219+
* To restart after termination, simply create a new instance of FIRFirestore with
220+
* `firestore` or `firestoreForApp` methods.
221+
*
222+
* Termination does not cancel any pending writes and any tasks that are awaiting a response from
223+
* the server will not be resolved. The next time you start this instance, it will resume
224+
* attempting to send these writes to the server.
225+
*
226+
* Note: Under normal circumstances, calling this method is not required. This
227+
* method is useful only when you want to force this instance to release all of its resources or
228+
* in combination with `clearPersistence` to ensure that all local state is destroyed
229+
* between test runs.
230+
*
231+
* @param completion A block to execute once everything has been terminated.
232+
*/
233+
- (void)terminateWithCompletion:(nullable void (^)(NSError *_Nullable error))completion
234+
NS_SWIFT_NAME(terminate(completion:));
235+
211236
@end
212237

213238
NS_ASSUME_NONNULL_END

Firestore/Swift/Tests/API/BasicCompileTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func main() {
6363
clearPersistence(database: db)
6464

6565
types()
66+
67+
waitForPendingWrites(database: db)
68+
69+
terminateDb(database: db)
6670
}
6771

6872
func initializeDb() -> Firestore {
@@ -448,3 +452,21 @@ func types() {
448452
let _: Transaction
449453
let _: WriteBatch
450454
}
455+
456+
func waitForPendingWrites(database db: Firestore) {
457+
db.waitForPendingWrites { error in
458+
if let e = error {
459+
print("Uh oh! \(e)")
460+
return
461+
}
462+
}
463+
}
464+
465+
func terminateDb(database db: Firestore) {
466+
db.terminate { error in
467+
if let e = error {
468+
print("Uh oh! \(e)")
469+
return
470+
}
471+
}
472+
}

Firestore/core/src/firebase/firestore/api/firestore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Firestore : public std::enable_shared_from_this<Firestore> {
9292
void RunTransaction(core::TransactionUpdateCallback update_callback,
9393
core::TransactionResultCallback result_callback);
9494

95-
void Shutdown(util::StatusCallback callback);
95+
void Terminate(util::StatusCallback callback);
9696
void ClearPersistence(util::StatusCallback callback);
9797
void WaitForPendingWrites(util::StatusCallback callback);
9898

0 commit comments

Comments
 (0)