suspenders-js
Version:
Asynchronous programming library utilizing coroutines, functional reactive programming and structured concurrency.
201 lines • 6.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Flow_1 = require("./Flow");
const Scope_1 = require("./Scope");
const Util_1 = require("./Util");
describe(`Scope tests`, () => {
it(`sibling coroutine is canceled when scope is canceled`, (done) => {
const scope = new Scope_1.Scope();
scope.launch(function* () {
try {
yield Util_1.wait(5);
}
finally {
done();
}
});
scope.launch(function* () {
throw new Error();
});
});
it(`coroutine is canceled when scope is canceled`, (done) => {
const scope = new Scope_1.Scope();
scope.launch(function* () {
try {
yield Util_1.awaitCancelation();
}
finally {
done();
}
});
scope.cancel();
});
it(`canceling a coroutine doesn't cancel it's scope`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
const cancelFunction = scope.launch(function* () {
yield Util_1.awaitCancelation();
});
cancelFunction();
scope.launch(function* () {
done();
});
});
it(`throwing in non-canceling scope doesn't cancel it`, (done) => {
const scope = new Scope_1.Scope({ isCancelable: false, errorCallback: (error) => { done(error); } });
scope.launch(function* () {
throw new Error();
});
if (!scope.isActive()) {
done(`scope is no longer active after throw`);
}
scope.launch(function* () {
done();
});
});
it(`calling coroutine asynchronously`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
scope.launch(function* () {
const asyncValue = this.callAsync(function* () {
return 1;
});
const value = yield* this.suspend(asyncValue);
if (value === 1) {
done();
}
else {
done(`unexpected result`);
}
});
});
it(`calling coroutine asynchronously in finally block`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
const cancel = scope.launch(function* () {
try {
yield Util_1.awaitCancelation();
}
finally {
const asyncValue = this.callAsync(function* () {
return 1;
});
const value = yield* this.suspend(asyncValue);
if (value === 1) {
done();
}
else {
done(`unexpected result`);
}
}
});
cancel();
});
it(`calling coroutine asynchronously in finally block on canceled scope`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
scope.launch(function* () {
try {
yield Util_1.awaitCancelation();
}
finally {
const asyncValue = this.callAsync(function* () {
return 1;
});
const value = yield* this.suspend(asyncValue);
if (value === 1) {
done();
}
else {
done(`unexpected result`);
}
}
});
scope.cancel();
});
it(`calling another coroutine`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
scope.launch(function* () {
const value = yield* this.call(function* () {
return 1;
});
if (value === 1) {
done();
}
else {
done(`unexpected result`);
}
});
});
it(`calling another coroutine in finally block`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
const cancel = scope.launch(function* () {
try {
yield Util_1.awaitCancelation();
}
finally {
yield Util_1.wait(1);
const value = yield* this.call(function* () {
yield Util_1.wait(1);
return 1;
});
if (value === 1) {
done();
}
else {
done(`unexpected result`);
}
}
});
cancel();
});
it(`calling another coroutine in finally block on canceled scope`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
scope.launch(function* () {
try {
yield Util_1.awaitCancelation();
}
finally {
yield Util_1.wait(1);
const value = yield* this.call(function* () {
yield Util_1.wait(1);
return 1;
});
if (value === 1) {
done();
}
else {
done(`unexpected result`);
}
}
});
scope.cancel();
});
it(`completed coroutines are not longer referenced by scope`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
scope.launch(function* () {
yield Util_1.wait(1);
});
setTimeout(() => {
if (scope._cancelCallbacks.size === 0) {
done();
}
else {
done(`scope still has reference to coroutine`);
}
}, 5);
});
it(`completed flows are not longer referenced by scope`, (done) => {
const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } });
Flow_1.flowOf((observer) => function* () {
observer.emit(1);
observer.emit(2);
observer.emit(3);
}).launchIn(scope);
setTimeout(() => {
if (scope._cancelCallbacks.size === 0) {
done();
}
else {
done(`scope still has reference to coroutine`);
}
}, 5);
});
});
//# sourceMappingURL=Scope.spec.js.map