Skip to content

Commit b14f2a7

Browse files
committed
filemanager should not upload a single file multiple times, add tests for upload operation
1 parent d6a620d commit b14f2a7

4 files changed

Lines changed: 74 additions & 8 deletions

File tree

lib/js/src/manager/file/_FileManagerBase.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,16 @@ class _FileManagerBase extends _SubManagerBase {
252252
if (success) {
253253
this._bytesAvailable = bytesAvailable;
254254
this._remoteFiles.push(fileName);
255-
if (!file.isPersistent()) {
255+
if (!fileClone.isPersistent()) {
256256
this._uploadedEphemeralFileNames.push(fileName);
257257
}
258258
} else if (errorMessage !== 'File is already on the head unit, aborting upload operation') {
259259
this._incrementFailedUploadCountForFileName(fileName, this._failedFileUploadsCount);
260260

261-
const maxUploadCount = (file instanceof SdlArtwork) ? this._maxArtworkUploadAttempts : this._maxFileUploadAttempts;
262-
if (this._canFileBeUploadedAgain(file, maxUploadCount, this._failedFileUploadsCount)) {
263-
console.log(`FileManagerBase: Attempting to resend file with name ${file.getName()} after a failed upload attempt`);
264-
this._sdlUploadFilePrivate(file, listener);
261+
const maxUploadCount = (fileClone instanceof SdlArtwork) ? this._maxArtworkUploadAttempts : this._maxFileUploadAttempts;
262+
if (this._canFileBeUploadedAgain(fileClone, maxUploadCount, this._failedFileUploadsCount)) {
263+
console.log(`FileManagerBase: Attempting to resend file with name ${fileClone.getName()} after a failed upload attempt`);
264+
this._sdlUploadFilePrivate(fileClone, listener);
265265
return;
266266
}
267267
}

lib/js/src/manager/file/_UploadFileOperation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class _UploadFileOperation extends _Task {
4242
const file = this._fileWrapper.getFile();
4343
const msgVersion = this._lifecycleManager.getSdlMsgVersion();
4444
const rpcVersion = new Version(msgVersion.getMajorVersion(), msgVersion.getMinorVersion(), msgVersion.getPatchVersion());
45-
if (!file.isPersistent() && !this.hasUploadedFile(file) && new Version(4, 4, 0).isNewerThan(rpcVersion) === 1) {
45+
if (!file.isPersistent() && !this._fileManager.hasUploadedFile(file) && new Version(4, 4, 0).isNewerThan(rpcVersion) === 1) {
4646
file.setOverwrite(true);
4747
}
4848

tests/managers/file/FileManagerTests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module.exports = function (appClient) {
9595
async function checkForUploadFailure (fileManager, sdlFile) {
9696
const operation = new SDL.manager.file._UploadFileOperation(sdlManager._lifecycleManager, sdlManager.getFileManager(), new SDL.manager.file._SdlFileWrapper(sdlFile, (success, bytesAvailable, fileNames, errorMessage) => {
9797
Validator.assertTrue(!success);
98-
}), sdlManager._fileManager.getRemoteFileNames());
98+
}));
9999
await operation.onExecute();
100100
}
101101

@@ -435,7 +435,7 @@ module.exports = function (appClient) {
435435
Validator.assertTrue(success);
436436
sdlManager._fileManager._remoteFiles.push(artworkToUpload[1].getName());
437437
resolve();
438-
}), sdlManager._fileManager.getRemoteFileNames());
438+
}));
439439
sdlManager._fileManager._addTask(uploadOperation);
440440
});
441441
stub.restore();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const chai = require('chai');
2+
chai.use(require('chai-as-promised'));
3+
const SDL = require('../../config.js').node;
4+
5+
// Mocking framework
6+
// Used to stub an RPC call so that it isn't actually sent to Core
7+
const sinon = require('sinon');
8+
9+
const Validator = require('../../Validator');
10+
11+
module.exports = function (appClient) {
12+
const sdlManager = appClient._sdlManager;
13+
const fileManager = sdlManager.getFileManager();
14+
const uploadOverwriteFailure = 'File is already on the head unit, aborting upload operation';
15+
16+
describe('UploadFileOperationTests', function () {
17+
describe('when the file is already on the head unit', function () {
18+
describe('when not overwriting', function () {
19+
it('should not send the upload RPCs and finish the operation', async function () {
20+
const stub = sinon.stub(fileManager, 'fileNeedsUpload')
21+
.callsFake((file) => false);
22+
const testFileName = 'TestSmallMemory';
23+
const testFileData = 'test1234';
24+
const testFile = new SDL.manager.file.filetypes.SdlFile(testFileName, SDL.rpc.enums.FileType.BINARY, testFileData, true);
25+
26+
const testFileWrapper = new SDL.manager.file._SdlFileWrapper(testFile, (success, bytesAvailable, fileNames, errorMessage) => {
27+
Validator.assertTrue(!success);
28+
Validator.assertNull(bytesAvailable);
29+
Validator.assertNull(fileNames);
30+
Validator.assertNotNullUndefined(errorMessage);
31+
Validator.assertEquals(errorMessage, uploadOverwriteFailure);
32+
});
33+
const operation = new SDL.manager.file._UploadFileOperation(sdlManager._lifecycleManager, sdlManager.getFileManager(), testFileWrapper);
34+
await operation._start();
35+
stub.restore();
36+
37+
Validator.assertEquals(operation.getState(), SDL.manager._Task.FINISHED);
38+
});
39+
});
40+
41+
describe('when overwriting', function () {
42+
it('should send the upload RPCs', async function () {
43+
const stub = sinon.stub(fileManager, 'fileNeedsUpload')
44+
.callsFake((file) => false);
45+
const testFileName = 'TestSmallMemory';
46+
const testFileData = 'test1234';
47+
const testFile = new SDL.manager.file.filetypes.SdlFile(testFileName, SDL.rpc.enums.FileType.BINARY, testFileData, true)
48+
.setOverwrite(true);
49+
50+
const testFileWrapper = new SDL.manager.file._SdlFileWrapper(testFile, (success, bytesAvailable, fileNames, errorMessage) => {
51+
Validator.assertTrue(!success);
52+
Validator.assertNull(bytesAvailable);
53+
Validator.assertNull(fileNames);
54+
Validator.assertNotNullUndefined(errorMessage);
55+
Validator.assertEquals(errorMessage, uploadOverwriteFailure);
56+
});
57+
const operation = new SDL.manager.file._UploadFileOperation(sdlManager._lifecycleManager, sdlManager.getFileManager(), testFileWrapper);
58+
await operation._start();
59+
stub.restore();
60+
61+
Validator.assertNotEquals(operation.getState(), SDL.manager._Task.FINISHED);
62+
});
63+
});
64+
});
65+
});
66+
};

0 commit comments

Comments
 (0)