Skip to content

Commit

Permalink
Add tests for the keepAlive option
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Aug 30, 2023
1 parent a2d4cd2 commit 1e683c2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions packages/memcache-client/src/test/spec/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,54 @@ describe("memcache client", function () {
});
});

it("should enable SO_KEEPALIVE with an initial delay of 60000 ms by default", async () => {
const _setKeepAlive = Socket.prototype.setKeepAlive;
const mockKeepAlive = jest.fn();
const x = new MemcacheClient({ server });

try {
Socket.prototype.setKeepAlive = mockKeepAlive;
await x.set("foo", "bar");
} finally {
Socket.prototype.setKeepAlive = _setKeepAlive;
x.shutdown();
}

expect(mockKeepAlive).toHaveBeenCalledWith(true, 60000);
});

it("should enable SO_KEEPALIVE with a custom initial delay when the keepAlive client option is a number", async () => {
const _setKeepAlive = Socket.prototype.setKeepAlive;
const mockKeepAlive = jest.fn();
const x = new MemcacheClient({ server, keepAlive: 10000 });

try {
Socket.prototype.setKeepAlive = mockKeepAlive;
await x.set("foo", "bar");
} finally {
Socket.prototype.setKeepAlive = _setKeepAlive;
x.shutdown();
}

expect(mockKeepAlive).toHaveBeenCalledWith(true, 10000);
});

it("should not enable SO_KEEPALIVE when the keepAlive client option is `false`", async () => {
const _setKeepAlive = Socket.prototype.setKeepAlive;
const mockKeepAlive = jest.fn();
const x = new MemcacheClient({ server, keepAlive: false });

try {
Socket.prototype.setKeepAlive = mockKeepAlive;
await x.set("foo", "bar");
} finally {
Socket.prototype.setKeepAlive = _setKeepAlive;
x.shutdown();
}

expect(mockKeepAlive).not.toHaveBeenCalled();
});

it("should not enable TCP_NODELAY by default", async () => {
const _setNoDelay = Socket.prototype.setNoDelay;
const mockNoDelay = jest.fn();
Expand Down

0 comments on commit 1e683c2

Please sign in to comment.