@boringnode/bus
Version:
A simple and lean driver-based service bus implementation
122 lines (121 loc) • 2.6 kB
JavaScript
// test_helpers/chaos_injector.ts
import { setTimeout } from "timers/promises";
var ChaosInjector = class {
/**
* Probability of throwing an error
*/
#throwProbability = 0;
/**
* Minimum delay in milliseconds
*/
#minDelay = 0;
/**
* Maximum delay in milliseconds
*/
#maxDelay = 0;
/**
* Randomly throw an error with the given probability
*/
injectExceptions() {
if (Math.random() < this.#throwProbability) {
throw new Error("Chaos: Random error");
}
}
/**
* Apply a random delay between minDelay and maxDelay
*/
async injectDelay() {
const delay = this.#minDelay + Math.random() * (this.#maxDelay - this.#minDelay);
await setTimeout(delay);
}
/**
* Apply some chaos : delay and/or throw an error
*/
async injectChaos() {
await this.injectDelay();
this.injectExceptions();
}
/**
* Make the cache always throw an error
*/
alwaysThrow() {
this.#throwProbability = 1;
return this;
}
/**
* Reset the throw probability to 0
*/
neverThrow() {
this.#throwProbability = 0;
return this;
}
/**
* Always apply the given delay
*/
alwaysDelay(minDelay, maxDelay) {
this.#minDelay = minDelay;
this.#maxDelay = maxDelay;
return this;
}
};
// test_helpers/chaos_transport.ts
var ChaosTransport = class {
/**
* The inner transport that is wrapped
*/
#innerTransport;
/**
* Reference to the chaos injector
*/
#chaosInjector;
constructor(innerTransport) {
this.#innerTransport = innerTransport;
this.#chaosInjector = new ChaosInjector();
}
setId(id) {
this.#innerTransport.setId(id);
return this.#innerTransport;
}
getInnerTransport() {
return this.#innerTransport;
}
/**
* Make the cache always throw an error
*/
alwaysThrow() {
this.#chaosInjector.alwaysThrow();
return this;
}
/**
* Reset the cache to never throw an error
*/
neverThrow() {
this.#chaosInjector.neverThrow();
return this;
}
async publish(channel, message) {
await this.#chaosInjector.injectChaos();
return this.#innerTransport.publish(channel, message);
}
async subscribe(channel, handler) {
return this.#innerTransport.subscribe(channel, handler);
}
unsubscribe(channel) {
return this.#innerTransport.unsubscribe(channel);
}
disconnect() {
return this.#innerTransport.disconnect();
}
onReconnect(_callback) {
}
};
export {
ChaosTransport
};
/**
* @boringnode/bus
*
* @license MIT
* @copyright BoringNode
*/
//# sourceMappingURL=test_helpers.js.map