Skip to content

Commit

Permalink
Test (#6)
Browse files Browse the repository at this point in the history
* Added tests for Builder

* Added test for ExponentialBackoff

* Updated README.md

* Added default-values for constructor-parameters

* Bumped version to rc5
  • Loading branch information
jjxxs authored Sep 15, 2020
1 parent 53e32f5 commit a4ebb94
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,63 @@ A client-websocket written in TypeScript to be used from within browsers with fo
- With easy-to-configure parameters (time between retries)
- Optional pending-messages
- With easy-to-configure buffers (size, behaviour)
- Builder-class for easy initialization and configuration
- Builder-class for easy initialization and configuration

## Usage
New instances can be easily created through the Builder-class.

```typescript
const ws = new Builder('ws://localhost:42421').build();
```

#### Callbacks
You can register callbacks for `onOpen`-, `onClose`-, `onError`- and `onMessage`-events. The callbacks get called with the websocket-instance that caused the event plus the event as parameters.
```typescript
const ws = new Builder('ws://localhost:42421')
.onOpen((i, e) => { console.log("opened") })
.onClose((i, e) => { console.log("closed") })
.onError((i, e) => { console.log("error") })
.onMessage((i, e) => { i.send(e.data) })
.build();
```

It is possible to register multiple callbacks for the same event, they are called in stack-order:
```typescript
const ws = new Builder('ws://localhost:42421')
.onMessage((i, e) => { console.log("sent echo") })
.onMessage((i, e) => { i.send(e.data) })
.onMessage((i, e) => { console.log("message received") })
.build();
```

#### Buffer
To buffer pending messages while your websocket is disconnected, configure it to use a Buffer. These pending messages
will be sent out as soon as the connection is (re)-established.

```typescript
const ws = new Builder('ws://localhost:42421')
.withBuffer(new LRUBuffer(100)) // buffers up to 100 messages, substitutes old messages with new ones
.build();
```

```typescript
const ws = new Builder('ws://localhost:42421')
.withBuffer(new TimeBuffer(5 * 60 * 1000)) // buffers messages that were written within the last 5 minutes
.build();
```

#### Reconnect / Backoff
To configure the websocket to automatically reconnect when the connection gets lost, provide it with a Backoff.
The type of backoff provided decides the delay between connection-retries.

```typescript
const ws = new Builder('ws://localhost:42421')
.withBackoff(new ConstantBackoff(500)) // Always waits 500 ms between retries
.build();
```

```typescript
const ws = new Builder('ws://localhost:42421')
.withBackoff(new ExponentialBackoff(100)) // Doubles the time between reconnects with every try
.build();
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "websocket-ts",
"version": "1.0.0-rc4",
"version": "1.0.0-rc5",
"main": "lib/index.js",
"types": "lib/",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/backoff/exponentialbackoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ExponentialBackoff implements Backoff {
private readonly expMax: number;
private expCur: number;

constructor(k: number, expMin: number, expMax: number) {
constructor(k: number, expMin: number = 0, expMax: number = 5) {
this.k = k;
this.expMin = expMin;
this.expMax = expMax;
Expand Down
18 changes: 15 additions & 3 deletions test/backoff/exponentialbackoff.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import {ExponentialBackoff} from "../../src/backoff/exponentialbackoff";

describe("Testsuite for ExponentialBackoff", () => {
const p = 0, expMin = 0, expMax = 0;
const k = 0, expMin = 0, expMax = 8;
let backoff: ExponentialBackoff;

beforeEach(() => {
backoff = new ExponentialBackoff(p, expMin, expMax);
backoff = new ExponentialBackoff(k, expMin, expMax);
});

test("ExponentialBackoff should increase exponentially", () => {
expect(true).toBe(true);
let expCur = expMin;
const calc = (): number => {
const next = k * Math.pow(2, expCur);
if (expMax > expCur)
expCur++;
return next;
}
for (let i = 0; i < expMax + 5; i++)
expect(backoff.Next()).toBe(calc());
backoff.Reset()
expCur = expMin;
for (let i = 0; i < expMax + 5; i++)
expect(backoff.Next()).toBe(calc());
});
});
40 changes: 40 additions & 0 deletions test/builder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Builder, LRUBuffer, Websocket} from "../src";
import {ConstantBackoff} from "../src/backoff/constantbackoff";

describe("Testsuite for Builder", () => {
const url = "ws://localhost:42421";
let builder: Builder

beforeEach(() => {
builder = new Builder(url);
});

test("Builder should set protocols", () => {
const protocols = ["p1", "p2", "p3"];
builder.withProtocols(protocols);
expect(builder["protocols"]).toBe(protocols);
});

test("Builder should set backoff", () => {
const backoff = new ConstantBackoff(100);
builder.withBackoff(backoff);
expect(builder["backoff"]).toBe(backoff);
});

test("Builder should set buffer", () => {
const buffer = new LRUBuffer(10);
builder.withBuffer(buffer);
expect(builder["buffer"]).toBe(buffer);
});

test("Builder should set onOpen-callbacks", () => {
let i = 0;
const cb1 = () => i += 2
const cb2 = () => i += 3
builder.onOpen(cb1).onOpen(cb2);
const fns = builder["onOpenChain"]
expect(fns).not.toBeUndefined();
fns!(null as unknown as Websocket, null as unknown as Event);
expect(i).toBe(5);
})
});

0 comments on commit a4ebb94

Please sign in to comment.