Created
January 10, 2015 02:52
-
-
Save peterflynn/17694e1d2f0e9d9151c9 to your computer and use it in GitHub Desktop.
Unit tests for Brackets Async module with concurrent array mutation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe("Concurrent mutation", function () { | |
| function runTest(asyncAPI, arrayMutate) { | |
| var items = [1, 2, 3]; | |
| var deferreds = []; | |
| var nextExpectedItem = 1; | |
| var masterPromise = asyncAPI(items, function (item, i) { | |
| expect(item).toBe(nextExpectedItem); | |
| expect(i).toBe(nextExpectedItem - 1); | |
| nextExpectedItem++; | |
| arrayMutate(items, item, i); | |
| // console.log(". " + items); | |
| var d = new $.Deferred(); | |
| deferreds.push(d); | |
| return d; | |
| }); | |
| // console.log("> " + items); | |
| expect(deferreds.length).toBe(3); // make sure we didn't stop early | |
| // Resolve all the dummy operations now & verify Async's master promise resolves at the right time | |
| deferreds.forEach(function (d) { | |
| expect(masterPromise.state()).toBe("pending"); // not resolved early... | |
| d.resolve(); | |
| }); | |
| expect(masterPromise.state()).toBe("resolved"); // ..but is resolved now | |
| return items; | |
| } | |
| it("concurrent adds ahead don't break doInParallel()", function () { | |
| var finalItems = runTest(Async.doInParallel, function (items, currentItem, i) { | |
| if (i === 0) { | |
| // Add item in the position that will be next in the iteration | |
| items.splice(i + 1, 0, "bad" + i); | |
| } | |
| }); | |
| expect(finalItems).toEqual([1, "bad0", 2, 3]); | |
| }); | |
| it("concurrent adds behind don't break doInParallel()", function () { | |
| var finalItems = runTest(Async.doInParallel, function (items, currentItem, i) { | |
| if (i === 0) { | |
| // Add item to front, pushing the current item to the slot that will be next again | |
| items.unshift("bad" + i); | |
| } | |
| }); | |
| expect(finalItems).toEqual(["bad0", 1, 2, 3]); | |
| }); | |
| it("concurrent removals ahead don't break doInParallel()", function () { | |
| var finalItems = runTest(Async.doInParallel, function (items, currentItem, i) { | |
| if (i === 0) { | |
| // Remove item that would be next in the iteration | |
| items.splice(i + 1, 1); | |
| } | |
| }); | |
| expect(finalItems).toEqual([1, 3]); | |
| }); | |
| it("concurrent removals ahead don't break doInParallel()", function () { | |
| var finalItems = runTest(Async.doInParallel, function (items, currentItem, i) { | |
| // Remove first item, moving next item to index we're currently already on | |
| items.shift(); | |
| }); | |
| expect(finalItems).toEqual([]); | |
| }); | |
| // TODO: same tests with Async.doSequentially as well | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment