Skip to content

Commit

Permalink
Includes regression test for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
robotlolita committed Feb 18, 2018
1 parent 1b3a09e commit 5d520e2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/source/browser/browser-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ process.env.FOLKTALE_ASSERTIONS = 'none';
require('../specs/base/adt/union');
require('../specs/base/concurrency/future');
require('../specs/base/concurrency/task');
require('../specs/base/concurrency/regression/task-cancellation-174');
require('../specs/base/core/lambda');
require('../specs/base/core/object');
require('../specs/base/fantasy-land/fantasy-land');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//----------------------------------------------------------------------
//
// This source file is part of the Folktale project.
//
// Licensed under MIT. See LICENCE for full licence information.
// See CONTRIBUTORS for the list of contributors to the project.
//
//----------------------------------------------------------------------

// See https://github.com/origamitower/folktale/issues/174 for details

const { task } = require('folktale/concurrency/task');

it('concurrency Regression: chained tasks are not cancelled if partially settled', (done) => {
const started = [];
const finished = [];
let isCancelled = false;

const delay = (ms, data) => task(resolver => {
started.push(data);
const timer = setTimeout(() => {
finished.push(data);
if (isCancelled) return;
resolver.resolve(data)
}, ms);
resolver.onCancelled(() => clearTimeout(timer));
});

const chain = delay(100, 'a')
.chain(x => delay(100, 'b'))
.chain(x => delay(100, 'c'));

const execution = chain.run();
setTimeout(() => {
isCancelled = true;
execution.cancel()
}, 150);

execution.listen({
onCancelled: () => {
if (started.join(',') === 'a,b' && finished.join(',') === 'a') {
done(null);
} else {
done(new Error(`Task was cancelled, but expected a and b to be started, but only a to be finished.
Got ${started.join(', ')} started and ${finished.join(', ')} finished`));
}
},

onResolved: () => {
done(new Error(`Expected task chain to be cancelled, but they were resolved.`));
},

onRejected: () => {
done(new Error(`Expected task chain to be cancelled, but they were rejected.`));
}
})


});

0 comments on commit 5d520e2

Please sign in to comment.