Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
yoavniran committed Aug 22, 2022
2 parents 5c0fec2 + e0a2199 commit bd70e11
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
6 changes: 3 additions & 3 deletions cypress/integration/retry-hooks/RetryHooks-queue-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ describe("RetryHooks - Queue", () => {
);
});

it("should use queue with retry", () => {
interceptWithDelay(800);
it.only("should use queue with retry", () => {
interceptWithDelay(700);

uploadFile(fileName, () => {
uploadFile(fileName2, () => {
cy.wait(WAIT_MEDIUM);
cy.wait(WAIT_SHORT);

cy.get("button[data-test='abort-button']:last")
.click();
Expand Down
12 changes: 9 additions & 3 deletions packages/core/abort/src/abort.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FILE_STATES, logger } from "@rpldy/shared";
import { fastAbortAll, fastAbortBatch } from "./fastAbort";

import type { Batch, BatchItem, UploadOptions } from "@rpldy/shared";
import type { AbortResult, AbortsMap, FinalizeRequestMethod } from "./types";
import type { AbortResult, AbortsMap, ItemsQueue, FinalizeRequestMethod } from "./types";

const abortNonUploadingItem = (item, aborts, finalizeItem) => {
logger.debugLog(`abort: aborting ${item.state} item - `, item);
Expand Down Expand Up @@ -58,10 +58,15 @@ const getIsFastAbortNeeded = (count, threshold: ?number) => {
const abortAll = (
items: { [string]: BatchItem },
aborts: AbortsMap,
queue: ItemsQueue,
finalizeItem: FinalizeRequestMethod,
options: UploadOptions
) : AbortResult => {
const itemIds = Object.keys(items);
//$FlowIssue[incompatible-type] - no flat
const itemIds : string[] = Object
.values(queue)
.flat();

const isFastAbort = getIsFastAbortNeeded(itemIds.length, options.fastAbortThreshold);

logger.debugLog(`abort: doing abort-all (${isFastAbort ? "fast" : "normal"} abort)`);
Expand All @@ -80,13 +85,14 @@ const abortBatch = (
batch: Batch,
batchOptions: UploadOptions,
aborts: AbortsMap,
queue: ItemsQueue,
finalizeItem: FinalizeRequestMethod,
options: UploadOptions,
) : AbortResult => {
const threshold = batchOptions.fastAbortThreshold === 0 ? 0 :
(batchOptions.fastAbortThreshold || options.fastAbortThreshold);

const isFastAbort = getIsFastAbortNeeded(batch.items.length, threshold);
const isFastAbort = getIsFastAbortNeeded(queue[batch.id].length, threshold);

logger.debugLog(`abort: doing abort-batch on: ${batch.id} (${isFastAbort ? "fast" : "normal"} abort)`);

Expand Down
34 changes: 26 additions & 8 deletions packages/core/abort/src/tests/abort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,16 @@ describe("abort tests", () => {
items: items || [
{ id: "i1", state: FILE_STATES.ADDED },
{ id: "i2", state: FILE_STATES.UPLOADING },
{ id: "i3", state: FILE_STATES.CANCELLED }
{ id: "i3", state: FILE_STATES.CANCELLED },
{ id: "i4", state: FILE_STATES.FINISHED }
],
});

it("should abort items in batch", () => {
const batch = getBatch();
const abort = jest.fn();

abortBatch(batch, {}, { "i2": abort }, finalizeMock, { fastAbortThreshold: 100 });
abortBatch(batch, {}, { "i2": abort }, { "b1": ["i1", "i2", "i3"] }, finalizeMock, { fastAbortThreshold: 4 });

expect(finalizeMock).toHaveBeenCalledTimes(1);
expect(abort).toHaveBeenCalledTimes(1);
Expand All @@ -103,7 +104,9 @@ describe("abort tests", () => {
const { isFast } = abortBatch(batch, { fastAbortThreshold: 3 }, {
"i1": abort,
"i2": abort
}, finalizeMock, { fastAbortThreshold: 100 });
},
{ "b1": ["i1", "i2", "i3"] },
finalizeMock, { fastAbortThreshold: 100 });

expect(isFast).toBe(true);
expect(finalizeMock).not.toHaveBeenCalled();
Expand All @@ -117,7 +120,9 @@ describe("abort tests", () => {
const { isFast } = abortBatch(batch, { fastAbortThreshold: 0 }, {
"i1": abort,
"i2": abort
}, finalizeMock, { fastAbortThreshold: 1 });
},
{ "b1": ["i1", "i2", "i3"] },
finalizeMock, { fastAbortThreshold: 1 });

expect(isFast).toBe(false);
expect(finalizeMock).toHaveBeenCalledTimes(1);
Expand All @@ -131,7 +136,7 @@ describe("abort tests", () => {
const { isFast } = abortBatch(batch, {}, {
"i1": abort,
"i2": abort
}, finalizeMock, { fastAbortThreshold: 0 });
}, { "b1": ["i1", "i2", "i3"] }, finalizeMock, { fastAbortThreshold: 0 });

expect(isFast).toBe(false);
expect(finalizeMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -164,7 +169,17 @@ describe("abort tests", () => {
batchId: "b1",
state: FILE_STATES.FINISHED,
},
}, { "u1": abort, "u2": abort, "u5": abort }, finalizeMock, { fastAbortThreshold: 5 });
"u7": {
id: "u7",
batchId: "b2",
state: FILE_STATES.FINISHED,
},
},
{ "u1": abort, "u2": abort, "u5": abort },
{"b1": ["u1", "u2", "u3"], "b2": ["u4"] },
finalizeMock,
{ fastAbortThreshold: 5 }
);

expect(isFast).toBe(false);
expect(abort).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -192,10 +207,13 @@ describe("abort tests", () => {
},
"u4": {
id: "u4",
batchId: "b1",
batchId: "b2",
state: FILE_STATES.FINISHED,
},
}, { "u1": abort, "u2": abort }, finalizeMock, { fastAbortThreshold: 4 });
},
{ "u1": abort, "u2": abort },
{"b1": ["u1", "u2", "u3"], "b2": ["u4"] },
finalizeMock, { fastAbortThreshold: 4 });

expect(isFast).toBe(true);
expect(fastAbortAll).toHaveBeenCalled();
Expand Down
6 changes: 4 additions & 2 deletions packages/core/abort/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ export type AbortMethod = () => boolean;

export type AbortsMap = { [string]: AbortMethod };

export type ItemsQueue = { [string]: string[] };

export type AbortResult = { isFast: boolean };

type InExactRawCreateOptions = { ...RawCreateOptions };

export type AbortBatchMethod = (Batch, InExactRawCreateOptions, AbortsMap, FinalizeRequestMethod, InExactRawCreateOptions) => AbortResult;
export type AbortBatchMethod = (Batch, InExactRawCreateOptions, AbortsMap, ItemsQueue, FinalizeRequestMethod, InExactRawCreateOptions) => AbortResult;

export type AbortAllMethod = ({ [string]: BatchItem }, AbortsMap, FinalizeRequestMethod, InExactRawCreateOptions) => AbortResult;
export type AbortAllMethod = ({ [string]: BatchItem }, AbortsMap, ItemsQueue, FinalizeRequestMethod, InExactRawCreateOptions) => AbortResult;

export type AbortItemMethod = (string, { [string]: BatchItem }, AbortsMap, FinalizeRequestMethod) => boolean;

Expand Down
2 changes: 2 additions & 0 deletions packages/core/uploader/src/queue/processAbort.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const processAbortBatch = (queue: QueueState, id: string): void => {
batch,
batchData.batchOptions,
state.aborts,
state.itemQueue,
getFinalizeAbortedItem(queue),
queue.getOptions()
);
Expand All @@ -73,6 +74,7 @@ const processAbortAll = (queue: QueueState): void => {
const { isFast } = abortAllMethod(
state.items,
state.aborts,
state.itemQueue,
getFinalizeAbortedItem(queue),
queue.getOptions()
);
Expand Down
5 changes: 3 additions & 2 deletions packages/core/uploader/src/queue/tests/processAbort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("processAbort tests", () => {
expect(queueState.trigger).toHaveBeenCalledWith(UPLOADER_EVENTS.ALL_ABORT);
expect(queueState.clearAllUploads).not.toHaveBeenCalled();

abortAll.mock.calls[0][2](1, "data");
abortAll.mock.calls[0][3](1, "data");

expect(processFinishedRequest).toHaveBeenCalledWith(queueState, [{
id: 1,
Expand Down Expand Up @@ -92,11 +92,12 @@ describe("processAbort tests", () => {
expect.objectContaining(batch),
expect.objectContaining(batchOptions),
expect.any(Object),
expect.any(Object),
expect.any(Function),
{ abortBatch }
);

abortBatch.mock.calls[0][3](1, "data");
abortBatch.mock.calls[0][4](1, "data");

expect(processFinishedRequest).toHaveBeenCalledWith(queueState, [{
id: 1,
Expand Down

0 comments on commit bd70e11

Please sign in to comment.