From 2b48fa829783b61515f47e037418536bdb88e153 Mon Sep 17 00:00:00 2001 From: Galvin Hsiu Date: Sun, 21 Jan 2018 01:14:50 -0800 Subject: [PATCH] Added in unit tests along with reset logic etc --- client/js/uploader.basic.api.js | 9 ++++ test/unit/chunked-uploads.js | 87 +++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/client/js/uploader.basic.api.js b/client/js/uploader.basic.api.js index b02f03894..c634cc37e 100644 --- a/client/js/uploader.basic.api.js +++ b/client/js/uploader.basic.api.js @@ -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) { @@ -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) { diff --git a/test/unit/chunked-uploads.js b/test/unit/chunked-uploads.js index 04c3d5ad3..92f2392f8 100644 --- a/test/unit/chunked-uploads.js +++ b/test/unit/chunked-uploads.js @@ -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; @@ -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, @@ -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) {