Skip to content

Commit

Permalink
Only delete events when package was successfully delivered (#5172) (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
mikehaney24 authored Mar 23, 2020
1 parent 1a9a6a5 commit f0d6f5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
});
Expand Down
28 changes: 28 additions & 0 deletions GoogleDataTransport/GDTCORTests/Unit/GDTCORUploadCoordinatorTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#import <GoogleDataTransport/GDTCORPlatform.h>

#import "GDTCORLibrary/Private/GDTCORStorage_Private.h"
#import "GDTCORLibrary/Private/GDTCORUploadCoordinator.h"

#import "GDTCORTests/Common/Categories/GDTCORRegistrar+Testing.h"
Expand Down Expand Up @@ -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<GDTCOREvent *> *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
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ @implementation GDTCOREventGenerator

+ (NSMutableSet<GDTCOREvent *> *)generate3Events {
static NSUInteger counter = 0;
NSString *filePath = [NSString stringWithFormat:@"test-%ld.txt", (unsigned long)counter];
int howManyToGenerate = 3;
NSMutableSet<GDTCOREvent *> *set = [[NSMutableSet alloc] initWithCapacity:howManyToGenerate];
for (int i = 0; i < howManyToGenerate; i++) {
GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"1337" target:50];
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];
Expand Down

0 comments on commit f0d6f5a

Please sign in to comment.