From f0d6f5a9ff3af91406d11c79c04e2557c35eb747 Mon Sep 17 00:00:00 2001 From: Michael Haney Date: Mon, 23 Mar 2020 14:26:32 -0700 Subject: [PATCH] Only delete events when package was successfully delivered (#5172) (#5173) * Only delete events when package was successfully delivered * Add unit test to check that retrying a package delivery doesn't delete an event file --- .../GDTCORLibrary/GDTCORUploadCoordinator.m | 2 +- .../Unit/GDTCORUploadCoordinatorTest.m | 28 +++++++++++++++++++ .../Unit/Helpers/GDTCOREventGenerator.m | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m b/GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m index 733d3971d58..220890b2778 100644 --- a/GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m +++ b/GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m @@ -241,7 +241,7 @@ - (void)packageDelivered:(GDTCORUploadPackage *)package successful:(BOOL)success [prioritizer packageDelivered:package successful:successful]; } } - if (package.events != nil) { + if (successful && package.events) { [self.storage removeEvents:package.events]; } }); diff --git a/GoogleDataTransport/GDTCORTests/Unit/GDTCORUploadCoordinatorTest.m b/GoogleDataTransport/GDTCORTests/Unit/GDTCORUploadCoordinatorTest.m index 2dccf0af64c..64fb2ca6498 100644 --- a/GoogleDataTransport/GDTCORTests/Unit/GDTCORUploadCoordinatorTest.m +++ b/GoogleDataTransport/GDTCORTests/Unit/GDTCORUploadCoordinatorTest.m @@ -18,6 +18,7 @@ #import +#import "GDTCORLibrary/Private/GDTCORStorage_Private.h" #import "GDTCORLibrary/Private/GDTCORUploadCoordinator.h" #import "GDTCORTests/Common/Categories/GDTCORRegistrar+Testing.h" @@ -167,4 +168,31 @@ - (void)testNSSecureCoding { XCTAssertEqualObjects([GDTCORUploadCoordinator sharedInstance], unarchivedCoordinator); } +/** Tests that retrying a package delivery doesn't delete the file from disk. */ +- (void)testPackageRetrying { + [GDTCORUploadCoordinator sharedInstance].storage = [GDTCORStorage sharedInstance]; + NSSet *events = [GDTCOREventGenerator generate3Events]; + self.prioritizer.events = events; + XCTestExpectation *expectation = [self expectationWithDescription:@"uploader will upload"]; + expectation.assertForOverFulfill = NO; + self.uploader.uploadPackageBlock = ^(GDTCORUploadPackage *_Nonnull package) { + [expectation fulfill]; + [package retryDeliveryInTheFuture]; + }; + [GDTCORUploadCoordinator sharedInstance].timerInterval = NSEC_PER_SEC / 10; + [GDTCORUploadCoordinator sharedInstance].timerLeeway = 0; + + [[GDTCORUploadCoordinator sharedInstance] startTimer]; + [self waitForExpectations:@[ expectation ] timeout:1.0]; + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]; + dispatch_sync([GDTCORUploadCoordinator sharedInstance].coordinationQueue, ^{ + }); + dispatch_sync([GDTCORStorage sharedInstance].storageQueue, ^{ + }); + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]; + for (GDTCOREvent *event in events) { + XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:event.fileURL.path]); + } +} + @end diff --git a/GoogleDataTransport/GDTCORTests/Unit/Helpers/GDTCOREventGenerator.m b/GoogleDataTransport/GDTCORTests/Unit/Helpers/GDTCOREventGenerator.m index 4e58450ebad..c2dd6aed2cb 100644 --- a/GoogleDataTransport/GDTCORTests/Unit/Helpers/GDTCOREventGenerator.m +++ b/GoogleDataTransport/GDTCORTests/Unit/Helpers/GDTCOREventGenerator.m @@ -26,7 +26,6 @@ @implementation GDTCOREventGenerator + (NSMutableSet *)generate3Events { static NSUInteger counter = 0; - NSString *filePath = [NSString stringWithFormat:@"test-%ld.txt", (unsigned long)counter]; int howManyToGenerate = 3; NSMutableSet *set = [[NSMutableSet alloc] initWithCapacity:howManyToGenerate]; for (int i = 0; i < howManyToGenerate; i++) { @@ -34,6 +33,7 @@ @implementation GDTCOREventGenerator event.clockSnapshot = [GDTCORClock snapshot]; event.qosTier = GDTCOREventQosDefault; event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"testing!"]; + NSString *filePath = [NSString stringWithFormat:@"test-%ld.txt", (unsigned long)counter]; [[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData data] attributes:nil];