diff options
Diffstat (limited to 'SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m')
-rw-r--r-- | SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m | 127 |
1 files changed, 125 insertions, 2 deletions
diff --git a/SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m b/SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m index 942aa6b7a..8a5ea9318 100644 --- a/SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m +++ b/SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m @@ -5,6 +5,7 @@ #import "SDLError.h" #import "SDLFile.h" #import "SDLFileManager.h" +#import "SDLFileManagerConfiguration.h" #import "SDLFileType.h" #import "SDLListFiles.h" #import "SDLListFilesOperation.h" @@ -26,6 +27,13 @@ SDLFileManagerState *const SDLFileManagerStateReady = @"Ready"; @interface SDLFileManager () @property (strong, nonatomic) NSOperationQueue *transactionQueue; @property (strong, nonatomic) NSMutableSet<SDLFileName *> *uploadedEphemeralFileNames; +@property (strong, nonatomic) NSMutableDictionary<SDLFileName *, NSNumber<SDLUInt> *> *failedFileUploadsCount; +@property (assign, nonatomic) UInt8 maxFileUploadAttempts; +@property (assign, nonatomic) UInt8 maxArtworkUploadAttempts; + +- (BOOL)sdl_canFileBeUploadedAgain:(nullable SDLFile *)file maxUploadCount:(int)maxRetryCount failedFileUploadsCount:(NSMutableDictionary<SDLFileName *, NSNumber<SDLUInt> *> *)failedFileUploadsCount; ++ (NSMutableDictionary<SDLFileName *, NSNumber<SDLUInt> *> *)sdl_incrementFailedUploadCountForFileName:(SDLFileName *)fileName failedFileUploadsCount:(NSMutableDictionary<SDLFileName *, NSNumber<SDLUInt> *> *)failedFileUploadsCount; + @end QuickSpecBegin(SDLFileManagerSpec) @@ -33,11 +41,13 @@ QuickSpecBegin(SDLFileManagerSpec) describe(@"SDLFileManager", ^{ __block TestConnectionManager *testConnectionManager = nil; __block SDLFileManager *testFileManager = nil; + __block SDLFileManagerConfiguration *testFileManagerConfiguration = nil; __block NSUInteger initialSpaceAvailable = 250; beforeEach(^{ testConnectionManager = [[TestConnectionManager alloc] init]; - testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager]; + testFileManagerConfiguration = [[SDLFileManagerConfiguration alloc] initWithArtworkRetryCount:0 fileRetryCount:0]; + testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager configuration:testFileManagerConfiguration]; testFileManager.suspended = YES; }); @@ -57,6 +67,11 @@ describe(@"SDLFileManager", ^{ it(@"should have no pending operations", ^{ expect(testFileManager.pendingTransactions).to(beEmpty()); }); + + it(@"should set the maximum number of upload attempts to 1", ^{ + expect(testFileManager.maxFileUploadAttempts).to(equal(1)); + expect(testFileManager.maxArtworkUploadAttempts).to(equal(1)); + }); }); describe(@"after receiving a start message", ^{ @@ -296,6 +311,10 @@ describe(@"SDLFileManager", ^{ expect(@(completionSuccess)).to(equal(testResponseSuccess)); expect(completionError).toEventuallyNot(beNil()); }); + + it(@"should increment the failure count for the artwork", ^{ + expect(testFileManager.failedFileUploadsCount[testFileName]).toEventually(equal(1)); + }); }); context(@"when the connection errors without a response", ^{ @@ -575,7 +594,8 @@ describe(@"SDLFileManager uploading/deleting multiple files", ^{ beforeEach(^{ testConnectionManager = [[TestMultipleFilesConnectionManager alloc] init]; - testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager]; + SDLFileManagerConfiguration *testFileManagerConfiguration = [[SDLFileManagerConfiguration alloc] initWithArtworkRetryCount:0 fileRetryCount:0]; + testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager configuration:testFileManagerConfiguration]; initialSpaceAvailable = 66666; }); @@ -1525,4 +1545,107 @@ describe(@"SDLFileManager uploading/deleting multiple files", ^{ }); }); +describe(@"SDLFileManager reupload failed files", ^{ + context(@"setting max upload attempts with the file manager configuration", ^{ + __block SDLFileManager *testFileManager = nil; + __block TestConnectionManager *testConnectionManager = nil; + __block SDLFileManagerConfiguration *testFileManagerConfiguration = nil; + + it(@"should set the max upload attempts to 2 if the configuration properties are not set", ^{ + testFileManagerConfiguration = [[SDLFileManagerConfiguration alloc] init]; + testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager configuration:testFileManagerConfiguration]; + + expect(testFileManager.maxFileUploadAttempts).to(equal(2)); + expect(testFileManager.maxArtworkUploadAttempts).to(equal(2)); + }); + + it(@"should set the max upload attempts to 1 if retry attempts are disabled", ^{ + testFileManagerConfiguration = [[SDLFileManagerConfiguration alloc] initWithArtworkRetryCount:0 fileRetryCount:0]; + testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager configuration:testFileManagerConfiguration]; + + expect(testFileManager.maxFileUploadAttempts).to(equal(1)); + expect(testFileManager.maxArtworkUploadAttempts).to(equal(1)); + }); + + it(@"should set the max upload attempts to the corresponding file manager configuration retry attempt count + 1", ^{ + UInt8 artworkRetryCount = 5; + UInt8 fileRetryCount = 3; + testFileManagerConfiguration = [[SDLFileManagerConfiguration alloc] initWithArtworkRetryCount:artworkRetryCount fileRetryCount:fileRetryCount]; + testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager configuration:testFileManagerConfiguration]; + + expect(testFileManager.maxArtworkUploadAttempts).to(equal((artworkRetryCount + 1))); + expect(testFileManager.maxFileUploadAttempts).to(equal(fileRetryCount + 1)); + }); + }); + + context(@"updating the failed upload count", ^{ + __block NSMutableDictionary<SDLFileName *, NSNumber<SDLUInt> *> *testFailedFileUploadsCount = nil; + __block NSString *testFileName = @"Test File A"; + + beforeEach(^{ + testFailedFileUploadsCount = [NSMutableDictionary dictionary]; + + expect(testFailedFileUploadsCount).to(beEmpty()); + }); + + it(@"should correctly add a file name", ^{ + testFailedFileUploadsCount = [SDLFileManager sdl_incrementFailedUploadCountForFileName:testFileName failedFileUploadsCount:testFailedFileUploadsCount]; + + expect(testFailedFileUploadsCount[testFileName]).to(equal(1)); + }); + + it(@"should correctly increment the count for a file name", ^{ + testFailedFileUploadsCount[testFileName] = @1; + testFailedFileUploadsCount = [SDLFileManager sdl_incrementFailedUploadCountForFileName:testFileName failedFileUploadsCount:testFailedFileUploadsCount]; + + expect(testFailedFileUploadsCount[testFileName]).to(equal(2)); + }); + }); + + context(@"checking if a failed upload can be uploaded again", ^{ + __block TestConnectionManager *testConnectionManager = nil; + __block SDLFileManager *testFileManager = nil; + __block SDLFileManagerConfiguration *testFileManagerConfiguration = nil; + __block NSMutableDictionary<SDLFileName *, NSNumber<SDLUInt> *> *testFailedFileUploadsCount = nil; + __block SDLFile *testFile = nil; + __block NSString *testFileName = @"Test File B"; + + beforeEach(^{ + testConnectionManager = [[TestConnectionManager alloc] init]; + testFileManagerConfiguration = [[SDLFileManagerConfiguration alloc] initWithArtworkRetryCount:0 fileRetryCount:0]; + testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager configuration:testFileManagerConfiguration]; + testFailedFileUploadsCount = [NSMutableDictionary dictionary]; + testFile = [[SDLFile alloc] initWithData:[@"someData" dataUsingEncoding:NSUTF8StringEncoding] name:testFileName fileExtension:@"bin" persistent:false]; + }); + + describe(@"the file cannot be uploaded again", ^{ + it(@"should not upload a file that is nil", ^{ + testFile = nil; + BOOL canUploadAgain = [testFileManager sdl_canFileBeUploadedAgain:testFile maxUploadCount:5 failedFileUploadsCount:testFailedFileUploadsCount]; + expect(canUploadAgain).to(equal(NO)); + }); + + it(@"should not upload a file that has already been uploaded the max number of times", ^{ + testFailedFileUploadsCount[testFileName] = @4; + BOOL canUploadAgain = [testFileManager sdl_canFileBeUploadedAgain:testFile maxUploadCount:4 failedFileUploadsCount:testFailedFileUploadsCount]; + expect(canUploadAgain).to(equal(NO)); + }); + }); + + describe(@"the file can be uploaded again", ^{ + it(@"should upload a file that has not yet failed to upload", ^{ + testFailedFileUploadsCount = [NSMutableDictionary dictionary]; + BOOL canUploadAgain = [testFileManager sdl_canFileBeUploadedAgain:testFile maxUploadCount:2 failedFileUploadsCount:testFailedFileUploadsCount]; + expect(canUploadAgain).to(equal(YES)); + }); + + it(@"should upload a file that has not been reuploaded the max number of times", ^{ + testFailedFileUploadsCount[testFileName] = @2; + BOOL canUploadAgain = [testFileManager sdl_canFileBeUploadedAgain:testFile maxUploadCount:4 failedFileUploadsCount:testFailedFileUploadsCount]; + expect(canUploadAgain).to(equal(YES)); + }); + }); + }); +}); + QuickSpecEnd |