Skip to content

Commit

Permalink
Moved improper usages of assertRejects to assertThrows for non async …
Browse files Browse the repository at this point in the history
…test assertions
  • Loading branch information
randalf-sr committed Aug 18, 2024
1 parent ad5b0f0 commit 9cabbd5
Show file tree
Hide file tree
Showing 26 changed files with 239 additions and 134 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rdtlabs/ts-utils",
"version": "0.2.36",
"version": "0.2.37",
"exports": {
".": "./src/index.ts",
"./async": "./src/async/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rdt-utils",
"version": "0.2.36",
"version": "0.2.37",
"description": "Library of typescript utilities",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/List.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
/// <reference lib="deno.ns" />

import { assert } from "https://deno.land/[email protected]/assert/assert.ts";
import { assertRejects } from "https://deno.land/[email protected]/assert/assert_rejects.ts";
import { List } from "./List.ts";
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import { Deferred, deferred } from "./async/Deferred.ts";
import { assertThrows } from "./index.ts";

Deno.test("List groupBy function test", () => {
const list = new List(1, 2, 3, 4, 5);
Expand Down Expand Up @@ -43,9 +43,9 @@ Deno.test("List instanceOf test", () => {
assert(list instanceof List);
});

Deno.test("List readonly write error test", async () => {
Deno.test("List readonly write error test", () => {
const list = List.readonly(1, 2, 3, 4, 5);
await assertRejects(async () => {
assertThrows(() => {
(list as any).push(6);
});
assert(list instanceof List);
Expand Down
12 changes: 12 additions & 0 deletions src/async/IterableLike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type IterableLike<T> =
| readonly T[]
| readonly PromiseLike<T>[]
| AsyncGenerator<T>
| AsyncIterable<T>
| Iterable<T>
| Iterable<PromiseLike<T>>
| PromiseLike<readonly T[]>
| PromiseLike<readonly PromiseLike<T>[]>
| PromiseLike<AsyncIterable<T>>
| PromiseLike<Iterable<T>>
| PromiseLike<Iterable<PromiseLike<T>>>;
6 changes: 3 additions & 3 deletions src/async/JobPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { QueueLengthExceededError } from "../errors/QueueLengthExceededError.ts"
import { ShutdownError } from "../errors/ShutdownError.ts";
import { assert } from "https://deno.land/[email protected]/assert/assert.ts";
import { createCancellation } from "../cancellation/createCancellation.ts";
import { assertThrows } from "../index.ts";

Deno.test("JobPool test", async () => {
let counter = 0;
Expand Down Expand Up @@ -81,9 +82,8 @@ Deno.test("JobPool fail max queue test", async () => {
promises.push(pool.submit(() => sig.wait()));
promises.push(pool.submit(() => sig.wait()));

await assertRejects(
// deno-lint-ignore require-await
async () => {
assertThrows(
() => {
promises.push(pool.submit(() => sig.wait()));
},
QueueLengthExceededError
Expand Down
9 changes: 5 additions & 4 deletions src/async/Task.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { CancellationToken } from "../cancellation/CancellationToken.ts";
import type { Callable, TimeoutInput } from "../types.ts";
import { delay } from "./delay.ts";
import { Executor } from "./executors.ts";
import type { Executor } from "./executor.ts";
import { executors } from "./executors.ts";

/**
* Utility object for working with asynchronous tasks.
Expand Down Expand Up @@ -67,15 +68,15 @@ function getScheduler(
scheduler: "micro" | "macro" | "sync" | Executor | undefined,
): Executor {
if (!scheduler || scheduler === "sync") {
return Executor.immediate;
return executors.immediate;
}

if (scheduler === "micro") {
return Executor.micro;
return executors.micro;
}

if (scheduler === "macro") {
return Executor.macro;
return executors.macro;
}

if (typeof scheduler === "object") {
Expand Down
16 changes: 8 additions & 8 deletions src/async/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
/// <reference lib="deno.ns" />

import { assert } from "https://deno.land/[email protected]/assert/assert.ts";
import { Executor } from "./executors.ts";
import { executors } from "./executors.ts";
import { monitor } from "./Monitor.ts";
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import { signal } from "./Signal.ts";
import { delay } from "./delay.ts";

Deno.test("executor immediate test", () => {
let hasRun = false;
Executor.immediate.execute(() => {
executors.immediate.execute(() => {
hasRun = true;
});
assert(hasRun);
});

Deno.test("executor sequential test", async () => {
const executor = Executor.sequential();
const executor = executors.sequential();
const c = monitor();

let counter = -1;
Expand All @@ -34,7 +34,7 @@ Deno.test("executor sequential test", async () => {

executor.execute(async () => {
if (index % 3 === 0) {
await Executor.macro.execute(increment);
await executors.macro.execute(increment);
} else {
increment();
}
Expand All @@ -48,7 +48,7 @@ Deno.test("executor sequential test", async () => {
});

Deno.test("executor task/micro/seqential", async () => {
const executor = Executor.sequential();
const executor = executors.sequential();
const c = monitor();

let counter = -1;
Expand All @@ -62,9 +62,9 @@ Deno.test("executor task/micro/seqential", async () => {

executor.execute(async () => {
if (index % 3 === 0) {
await Executor.macro.execute(increment);
await executors.macro.execute(increment);
} else if (index % 3 === 1) {
await Executor.micro.execute(increment);
await executors.micro.execute(increment);
} else {
increment();
}
Expand All @@ -79,7 +79,7 @@ Deno.test("executor task/micro/seqential", async () => {

Deno.test("executor concurrent", async () => {
let counter = 0;
const executor = Executor.concurrent(3);
const executor = executors.concurrent(3);

const sig = signal();
for (let i = 0; i < 10; i++) {
Expand Down
45 changes: 45 additions & 0 deletions src/async/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Callable } from "../types.ts";
import type { CancellationToken } from "../cancellation/CancellationToken.ts";

/**
* An abstraction for executing tasks.
*/
export interface Executor {
/**
* @param callable The task to execute.
* @param deadline The deadline for the task to complete.
* @returns A promise that resolves with the result of the callable or rejects with an error.
*/
execute: <T>(
callable: Callable<T | PromiseLike<T>>,
deadline?: CancellationToken,
) => Promise<T>;
}

/**
* An executor that executes tasks concurrently.
*/
export interface ConcurrentExecutor extends Executor {
/**
* Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
* @returns A promise that resolves when the executor has completed shutdown.
*/
shutdown(): void;

/**
* Awaits completion of all tasks in the executor.
* @returns A promise that resolves when the executor has completed shutdown.
*/
onShutdown(): Promise<void>;

/**
* @returns `true` if the executor has initiated shutdown.
*/
readonly isShutdownInitiated: boolean;

/**
* @returns `true` if the executor has completed shutdown.
*/
readonly isShutdown: boolean;
}
52 changes: 5 additions & 47 deletions src/async/executors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import type { ErrorLike } from "../types.ts";
import type { CancellationToken } from "../cancellation/CancellationToken.ts";
import { JobPool } from "./JobPool.ts";
import { Promises } from "./Promises.ts";
import type { ConcurrentExecutor, Executor } from "./executor.ts";

export const Executor = Object.freeze({
export const executors = Object.freeze({
concurrent: (
maxConcurrency?: number,
maxQueueLength?: number,
Expand Down Expand Up @@ -41,7 +42,7 @@ export const Executor = Object.freeze({
} as Executor;
},
sequentialize: (executor: Executor): Executor => {
const sequential = Executor.sequential();
const sequential = executors.sequential();
return {
execute: (callable, deadline) => {
return sequential.execute(
Expand All @@ -55,7 +56,7 @@ export const Executor = Object.freeze({
callable: Callable<T | PromiseLike<T>>,
cancellation?: CancellationToken,
): Promise<T> => {
return Executor.invoke(callable, cancellation);
return executors.invoke(callable, cancellation);
},
},
macro: <Executor> {
Expand Down Expand Up @@ -103,7 +104,7 @@ export const Executor = Object.freeze({
executor?: Executor,
cancellation?: CancellationToken,
) => {
return __invokeOn(callable, executor ?? Executor.immediate, cancellation);
return __invokeOn(callable, executor ?? executors.immediate, cancellation);
},
}) as {
/**
Expand Down Expand Up @@ -160,49 +161,6 @@ export const Executor = Object.freeze({
) => Promise<T>;
};

/**
* An abstraction for executing tasks.
*/
export interface Executor {
/**
* @param callable The task to execute.
* @param deadline The deadline for the task to complete.
* @returns A promise that resolves with the result of the callable or rejects with an error.
*/
execute: <T>(
callable: Callable<T | PromiseLike<T>>,
deadline?: CancellationToken,
) => Promise<T>;
}

/**
* An executor that executes tasks concurrently.
*/
export interface ConcurrentExecutor extends Executor {
/**
* Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
* @returns A promise that resolves when the executor has completed shutdown.
*/
shutdown(): void;

/**
* Awaits completion of all tasks in the executor.
* @returns A promise that resolves when the executor has completed shutdown.
*/
onShutdown(): Promise<void>;

/**
* @returns `true` if the executor has initiated shutdown.
*/
readonly isShutdownInitiated: boolean;

/**
* @returns `true` if the executor has completed shutdown.
*/
readonly isShutdown: boolean;
}

const __invoke = <T = void>(
callable: Callable<T | PromiseLike<T>>,
resolve: (value: T | PromiseLike<T>) => void,
Expand Down
2 changes: 1 addition & 1 deletion src/async/flowable/FlowProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CancellationIterableOptions } from "../../cancellation/CancellationIterableOptions.ts";
import type { ErrorLike } from "../../types.ts";
import type { Observable } from "../_rx.types.ts";
import type { IterableLike } from "../fromIterableLike.ts";
import type { IterableLike } from "../IterableLike.ts";
import type { CancellationError } from "../../cancellation/CancellationError.ts";
import type { CancellationToken } from "../../cancellation/CancellationToken.ts";
import type { Maybe } from "../../Maybe.ts";
Expand Down
3 changes: 2 additions & 1 deletion src/async/flowable/Flowable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { BufferStrategyOptions } from "../../buffer/BufferLike.ts";
import type { CancellationToken } from "../../cancellation/CancellationToken.ts";
import type { Observable } from "../_rx.types.ts";
import { type EventOptions, fromEvent } from "../fromEvent.ts";
import { fromIterableLike, type IterableLike } from "../fromIterableLike.ts";
import type { IterableLike } from "../IterableLike.ts";
import { fromIterableLike } from "../fromIterableLike.ts";
import { fromObservable } from "../fromObservable.ts";
import type { FlowProcessor } from "./FlowProcessor.ts";
import type { FlowPublisher } from "./FlowPublisher.ts";
Expand Down
3 changes: 2 additions & 1 deletion src/async/flowable/__utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { createObservable } from "../createObservable.ts";
import { Flowable } from "./Flowable.ts";
import type { FlowProcessor } from "./FlowProcessor.ts";
import * as p from "../pipeable/pipeable-funcs.ts";
import { fromIterableLike, type IterableLike } from "../fromIterableLike.ts";
import { fromIterableLike } from "../fromIterableLike.ts";
import type { IterableLike } from "../IterableLike.ts";
import type { FlowPublisher } from "./FlowPublisher.ts";
import { Maybe } from "../../Maybe.ts";

Expand Down
14 changes: 1 addition & 13 deletions src/async/fromIterableLike.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isThenable } from "../utils.ts";
import type { IterableLike } from "./IterableLike.ts";

/**
* Converts an iterable-like object into an asynchronous generator.
Expand Down Expand Up @@ -47,16 +48,3 @@ async function* iterate(it: Iterable<any>) {
yield await value;
}
}

export type IterableLike<T> =
| readonly T[]
| readonly PromiseLike<T>[]
| AsyncGenerator<T>
| AsyncIterable<T>
| Iterable<T>
| Iterable<PromiseLike<T>>
| PromiseLike<readonly T[]>
| PromiseLike<readonly PromiseLike<T>[]>
| PromiseLike<AsyncIterable<T>>
| PromiseLike<Iterable<T>>
| PromiseLike<Iterable<PromiseLike<T>>>;
2 changes: 2 additions & 0 deletions src/async/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export * from "./workerpool/WorkerPool.ts";
export * from "./JobPool.ts";
export * from "./Semaphore.ts";
export * from "./Mutex.ts";
export * from "./executor.ts";
export * from "./executors.ts";
export * from "./Signal.ts";
export * from "./IterableLike.ts";
export * from "./Deferred.ts";
export * from "./delay.ts";
export * from "./fromEvent.ts";
Expand Down
3 changes: 2 additions & 1 deletion src/async/pipeable/Pipeable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fromIterableLike, type IterableLike } from "../fromIterableLike.ts";
import type { IterableLike } from "../IterableLike.ts";
import { fromIterableLike } from "../fromIterableLike.ts";
import { __fromHandler, __fromHandlerMulti, __ofFunc } from "./__utils.ts";

/**
Expand Down
Loading

0 comments on commit 9cabbd5

Please sign in to comment.