Skip to content

Commit f0d6f5a

Browse files
authored
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
1 parent 1a9a6a5 commit f0d6f5a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ - (void)packageDelivered:(GDTCORUploadPackage *)package successful:(BOOL)success
241241
[prioritizer packageDelivered:package successful:successful];
242242
}
243243
}
244-
if (package.events != nil) {
244+
if (successful && package.events) {
245245
[self.storage removeEvents:package.events];
246246
}
247247
});

GoogleDataTransport/GDTCORTests/Unit/GDTCORUploadCoordinatorTest.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#import <GoogleDataTransport/GDTCORPlatform.h>
2020

21+
#import "GDTCORLibrary/Private/GDTCORStorage_Private.h"
2122
#import "GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
2223

2324
#import "GDTCORTests/Common/Categories/GDTCORRegistrar+Testing.h"
@@ -167,4 +168,31 @@ - (void)testNSSecureCoding {
167168
XCTAssertEqualObjects([GDTCORUploadCoordinator sharedInstance], unarchivedCoordinator);
168169
}
169170

171+
/** Tests that retrying a package delivery doesn't delete the file from disk. */
172+
- (void)testPackageRetrying {
173+
[GDTCORUploadCoordinator sharedInstance].storage = [GDTCORStorage sharedInstance];
174+
NSSet<GDTCOREvent *> *events = [GDTCOREventGenerator generate3Events];
175+
self.prioritizer.events = events;
176+
XCTestExpectation *expectation = [self expectationWithDescription:@"uploader will upload"];
177+
expectation.assertForOverFulfill = NO;
178+
self.uploader.uploadPackageBlock = ^(GDTCORUploadPackage *_Nonnull package) {
179+
[expectation fulfill];
180+
[package retryDeliveryInTheFuture];
181+
};
182+
[GDTCORUploadCoordinator sharedInstance].timerInterval = NSEC_PER_SEC / 10;
183+
[GDTCORUploadCoordinator sharedInstance].timerLeeway = 0;
184+
185+
[[GDTCORUploadCoordinator sharedInstance] startTimer];
186+
[self waitForExpectations:@[ expectation ] timeout:1.0];
187+
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
188+
dispatch_sync([GDTCORUploadCoordinator sharedInstance].coordinationQueue, ^{
189+
});
190+
dispatch_sync([GDTCORStorage sharedInstance].storageQueue, ^{
191+
});
192+
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
193+
for (GDTCOREvent *event in events) {
194+
XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:event.fileURL.path]);
195+
}
196+
}
197+
170198
@end

GoogleDataTransport/GDTCORTests/Unit/Helpers/GDTCOREventGenerator.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ @implementation GDTCOREventGenerator
2626

2727
+ (NSMutableSet<GDTCOREvent *> *)generate3Events {
2828
static NSUInteger counter = 0;
29-
NSString *filePath = [NSString stringWithFormat:@"test-%ld.txt", (unsigned long)counter];
3029
int howManyToGenerate = 3;
3130
NSMutableSet<GDTCOREvent *> *set = [[NSMutableSet alloc] initWithCapacity:howManyToGenerate];
3231
for (int i = 0; i < howManyToGenerate; i++) {
3332
GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"1337" target:50];
3433
event.clockSnapshot = [GDTCORClock snapshot];
3534
event.qosTier = GDTCOREventQosDefault;
3635
event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"testing!"];
36+
NSString *filePath = [NSString stringWithFormat:@"test-%ld.txt", (unsigned long)counter];
3737
[[NSFileManager defaultManager] createFileAtPath:filePath
3838
contents:[NSData data]
3939
attributes:nil];

0 commit comments

Comments
 (0)