Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
Added in unit tests along with reset logic etc
Browse files Browse the repository at this point in the history
  • Loading branch information
galvin-hsiu committed Jan 21, 2018
1 parent 7944338 commit 2b48fa8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
9 changes: 9 additions & 0 deletions client/js/uploader.basic.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@
self._options.callbacks.onUploadChunk(id, name, chunkData);
},
onUploadChunkSuccess: function(id, chunkData, result, xhr) {
self._onUploadChunkSuccess(id, chunkData);
self._options.callbacks.onUploadChunkSuccess.apply(self, arguments);
},
onResume: function(id, name, chunkData) {
Expand Down Expand Up @@ -1574,6 +1575,14 @@
//nothing to do in the base uploader
},

_onUploadChunkSuccess: function(id, chunkData) {
var uploadData = this._uploadData.retrieve({id: id});

if (!this._preventRetries[id] && this._options.retry.enableAuto && uploadData.status !== qq.status.PAUSED) {
this._autoRetries[id] = 0;
}
},

_onUploadStatusChange: function(id, oldStatus, newStatus) {
// Make sure a "queued" retry attempt is canceled if the upload has been paused
if (newStatus === qq.status.PAUSED) {
Expand Down
87 changes: 87 additions & 0 deletions test/unit/chunked-uploads.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ if (qqtest.canDownloadFileAsBlob) {
request.respond(200, null, JSON.stringify({success: true, testParam: "testVal"}));
}, 10);
},
onAutoRetry: function(id, name, attemptNumber) {
assert.fail("This should not be called");
},
onUploadChunkSuccess: function (id, chunkData, response, xhr) {
var request = fileTestHelper.getRequests()[fileTestHelper.getRequests().length - 1],
requestParams;
Expand Down Expand Up @@ -197,6 +200,86 @@ if (qqtest.canDownloadFileAsBlob) {
});
}

function testChunkedEveryFailureAndRecovery(done) {
assert.expect(6 + (expectedChunks * 17) + (expectedChunks * 6), done);

var alreadyFailed = false,
uploader = new qq.FineUploaderBasic({
request: {
endpoint: testUploadEndpoint
},
chunking: {
enabled: true,
partSize: chunkSize
},
retry: {
autoAttemptDelay: 0,
enableAuto: true
},
callbacks: {
onUploadChunk: function (id, name, chunkData) {
chunksSent++;

assert.equal(id, 0, "Wrong ID passed to onUpoadChunk");
assert.equal(name, uploader.getName(id), "Wrong name passed to onUploadChunk");
assert.equal(chunkData.partIndex, chunksSent - 1, "Wrong partIndex passed to onUploadChunk");
assert.equal(chunkData.startByte, (chunksSent - 1) * chunkSize + 1, "Wrong startByte passed to onUploadChunk");
assert.equal(chunkData.endByte, chunksSent === expectedChunks ? expectedFileSize : chunkData.startByte + chunkSize - 1, "Wrong startByte passed to onUploadChunk");
assert.equal(chunkData.totalParts, expectedChunks, "Wrong totalParts passed to onUploadChunk");

setTimeout(function () {
var request = fileTestHelper.getRequests()[fileTestHelper.getRequests().length - 1];

if (!alreadyFailed) {
alreadyFailed = true;

chunksSent--;
request.respond(500, null, JSON.stringify({testParam: "testVal"}));
}
else {
alreadyFailed = false;
request.respond(200, null, JSON.stringify({success: true, testParam: "testVal"}));
}
}, 10);
},
onAutoRetry: function(id, name, attemptNumber) {
assert.equal(id, 0, "Wrong ID passed to onAutoRetry");
assert.equal(name, uploader.getName(id), "Wrong name passed to onAutoRetry");
assert.equal(attemptNumber, 1, "Wrong auto retry attempt #");
},
onUploadChunkSuccess: function (id, chunkData, response, xhr) {
var request = fileTestHelper.getRequests()[fileTestHelper.getRequests().length - 1],
requestParams = request.requestBody.fields;

chunksSucceeded++;

assert.equal(requestParams.qquuid, uploader.getUuid(id), "Wrong uuid param");
assert.equal(requestParams.qqpartindex, chunksSent - 1, "Wrong part index param");
assert.equal(requestParams.qqpartbyteoffset, (chunksSent - 1) * chunkSize, "Wrong part byte offset param");
assert.equal(requestParams.qqtotalfilesize, expectedFileSize, "Wrong total file size param");
assert.equal(requestParams.qqtotalparts, expectedChunks, "Wrong total parts param");
assert.equal(requestParams.qqfilename, uploader.getName(id), "Wrong filename param");
assert.equal(requestParams.qqchunksize, requestParams.qqfile.size, "Wrong chunk size param");
assert.equal(id, 0, "Wrong ID passed to onUpoadChunkSuccess");

assert.equal(response.testParam, "testVal");
},
onComplete: function (id, name, response) {
assert.equal(expectedChunks, chunksSent, "Wrong # of chunks sent.");
assert.equal(expectedChunks, chunksSucceeded, "Wrong # of chunks succeeded");
assert.equal(response.testParam, "testVal");
}
}
}),
chunksSent = 0,
chunksSucceeded = 0;

qqtest.downloadFileAsBlob("up.jpg", "image/jpeg").then(function (blob) {
fileTestHelper.mockXhr();
uploader.addFiles({name: "test", blob: blob});
});
}

it("sends proper number of chunks when chunking is enabled, MPE", function(done) {
testChunkedUpload({
mpe: true,
Expand Down Expand Up @@ -246,6 +329,10 @@ if (qqtest.canDownloadFileAsBlob) {
testChunkedFailureAndRecovery(true, done);
});

it("fails every chunk once, then recovers and ensure attemptNumber is 1", function(done) {
testChunkedEveryFailureAndRecovery(done);
});

describe("resume feature tests", function() {
var nativeLocalStorageSetItem = window.localStorage.setItem,
acknowledgeRequests = function(endpoint) {
Expand Down

0 comments on commit 2b48fa8

Please sign in to comment.