Skip to content

Commit

Permalink
Fix #827
Browse files Browse the repository at this point in the history
* Add a public method for testing and transparency
* Add tests
  • Loading branch information
joeljfischer committed Jan 29, 2018
1 parent 2cc9168 commit 61113a5
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 7 deletions.
5 changes: 5 additions & 0 deletions SmartDeviceLink/SDLFileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ typedef void (^SDLFileManagerStartupCompletionHandler)(BOOL success, NSError *__
*/
@property (copy, nonatomic, readonly) NSSet<SDLFileName *> *remoteFileNames;

/**
A set of all names of files uploaded this session (and this session only)
*/
@property (copy, nonatomic, readonly) NSSet<SDLFileName *> *uploadedEphemeralFileNames;

/**
* The number of bytes still available for files for this app.
*/
Expand Down
14 changes: 9 additions & 5 deletions SmartDeviceLink/SDLFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ @interface SDLFileManager ()
@property (weak, nonatomic) id<SDLConnectionManagerType> connectionManager;

// Remote state
@property (strong, nonatomic, readwrite) NSMutableSet<SDLFileName *> *mutableRemoteFileNames;
@property (strong, nonatomic) NSMutableSet<SDLFileName *> *mutableRemoteFileNames;
@property (assign, nonatomic, readwrite) NSUInteger bytesAvailable;

// Local state
@property (strong, nonatomic) NSOperationQueue *transactionQueue;
@property (strong, nonatomic) NSMutableDictionary<SDLFileName *, NSOperation *> *uploadsInProgress;
@property (strong, nonatomic) NSMutableSet<SDLFileName *> *localUploadedFileNames;
@property (strong, nonatomic) NSMutableSet<SDLFileName *> *mutableUploadedEphemeralFileNames;
@property (strong, nonatomic) SDLStateMachine *stateMachine;
@property (copy, nonatomic, nullable) SDLFileManagerStartupCompletionHandler startupCompletionHandler;

Expand All @@ -72,7 +72,7 @@ - (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)manager
_transactionQueue.name = @"SDLFileManager Transaction Queue";
_transactionQueue.maxConcurrentOperationCount = 1;
_uploadsInProgress = [[NSMutableDictionary alloc] init];
_localUploadedFileNames = [[NSMutableSet<SDLFileName *> alloc] init];
_mutableUploadedEphemeralFileNames = [[NSMutableSet<SDLFileName *> alloc] init];

_stateMachine = [[SDLStateMachine alloc] initWithTarget:self initialState:SDLFileManagerStateShutdown states:[self.class sdl_stateTransitionDictionary]];

Expand Down Expand Up @@ -103,6 +103,10 @@ - (void)stop {
return [NSSet setWithSet:self.mutableRemoteFileNames];
}

- (NSSet<SDLFileName *> *)uploadedEphemeralFileNames {
return [NSSet setWithSet:self.mutableUploadedEphemeralFileNames];
}

- (NSString *)currentState {
return self.stateMachine.currentState;
}
Expand Down Expand Up @@ -275,7 +279,7 @@ - (void)uploadFiles:(NSArray<SDLFile *> *)files progressHandler:(nullable SDLFil
dispatch_group_enter(uploadFilesTask);

// HAX: [#827](https://github.com/smartdevicelink/sdl_ios/issues/827) Older versions of Core had a bug where list files would cache incorrectly. This led to attempted uploads failing due to the system thinking they were already there when they were not.
if (!file.persistent && [self.remoteFileNames containsObject:file.name] && ![self.localUploadedFileNames containsObject:file.name]) {
if (!file.persistent && [self.remoteFileNames containsObject:file.name] && ![self.mutableUploadedEphemeralFileNames containsObject:file.name]) {
file.overwrite = true;
}

Expand Down Expand Up @@ -390,7 +394,7 @@ - (void)sdl_uploadFile:(SDLFile *)file completionHandler:(nullable SDLFileManage
}
if (success) {
[weakSelf.mutableRemoteFileNames addObject:fileName];
[weakSelf.localUploadedFileNames addObject:fileName];
[weakSelf.mutableUploadedEphemeralFileNames addObject:fileName];
}
if (uploadCompletion != nil) {
uploadCompletion(success, bytesAvailable, error);
Expand Down
9 changes: 7 additions & 2 deletions SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ @interface SDLFileManager ()

QuickSpecBegin(SDLFileManagerSpec)

describe(@"SDLFileManager", ^{
fdescribe(@"SDLFileManager", ^{
__block TestConnectionManager *testConnectionManager = nil;
__block SDLFileManager *testFileManager = nil;
__block NSUInteger initialSpaceAvailable = 250;
Expand Down Expand Up @@ -225,6 +225,7 @@ @interface SDLFileManager ()

it(@"should set the file manager state to be waiting", ^{
expect(testFileManager.currentState).to(match(SDLFileManagerStateReady));
expect(testFileManager.uploadedEphemeralFileNames).to(beEmpty());
});

it(@"should create a putfile with the correct data", ^{
Expand Down Expand Up @@ -253,6 +254,7 @@ @interface SDLFileManager ()
expect(@(testFileManager.bytesAvailable)).toEventually(equal(testResponseBytesAvailable));
expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady));
expect(testFileManager.remoteFileNames).toEventually(contain(testFileName));
expect(testFileManager.uploadedEphemeralFileNames).toEventually(contain(testFileName));
});

it(@"should call the completion handler with the correct data", ^{
Expand Down Expand Up @@ -282,6 +284,7 @@ @interface SDLFileManager ()
expect(@(testFileManager.bytesAvailable)).toEventually(equal(@(initialSpaceAvailable)));
expect(testFileManager.remoteFileNames).toEventually(contain(testFileName));
expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady));
expect(testFileManager.uploadedEphemeralFileNames).to(beEmpty());
});

it(@"should call the completion handler with the correct data", ^{
Expand All @@ -307,7 +310,7 @@ @interface SDLFileManager ()
});
});

context(@"when allow overwrite is false", ^{
context(@"when allow overwrite is NO", ^{
__block SDLRPCRequest *lastRequest = nil;

beforeEach(^{
Expand Down Expand Up @@ -373,6 +376,7 @@ @interface SDLFileManager ()
it(@"should set the file manager state correctly", ^{
expect(@(testFileManager.bytesAvailable)).toEventually(equal(testResponseBytesAvailable));
expect(testFileManager.remoteFileNames).toEventually(contain(testFileName));
expect(testFileManager.uploadedEphemeralFileNames).toEventually(contain(testUploadFile.name));
expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady));
});

Expand Down Expand Up @@ -404,6 +408,7 @@ @interface SDLFileManager ()
it(@"should set the file manager state correctly", ^{
expect(@(testFileManager.bytesAvailable)).toEventually(equal(@(initialSpaceAvailable)));
expect(testFileManager.remoteFileNames).toEventuallyNot(contain(testFileName));
expect(testFileManager.uploadedEphemeralFileNames).toEventuallyNot(contain(testUploadFile.name));
expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady));
});

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ...ScreenViewControllerSnapshotTests/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 61113a5

Please sign in to comment.