From 6d90249c6b0756ca056be5d24b02f72ea74b4cd3 Mon Sep 17 00:00:00 2001 From: Ryan Grove Date: Tue, 22 Aug 2023 15:28:16 -0700 Subject: [PATCH] Add tests for the `noDelay` option --- .../src/test/spec/client.spec.ts | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/memcache-client/src/test/spec/client.spec.ts b/packages/memcache-client/src/test/spec/client.spec.ts index c90519f..d82ff02 100644 --- a/packages/memcache-client/src/test/spec/client.spec.ts +++ b/packages/memcache-client/src/test/spec/client.spec.ts @@ -14,7 +14,7 @@ import Path from "path"; import { text1, text2, poem1, poem2, poem3, poem4, poem5 } from "../data/text"; import NullLoger from "../../lib/null-logger"; import memcached, { MemcachedServer } from "memcached-njs"; -import { AddressInfo } from "net"; +import { AddressInfo, Socket } from "net"; describe("memcache client", function () { process.on("unhandledRejection", (e) => { @@ -279,6 +279,40 @@ describe("memcache client", function () { }); }); + it("should not enable TCP_NODELAY by default", async () => { + await startSingleServer(); + + const _setNoDelay = Socket.prototype.setNoDelay; + const mockNoDelay = jest.fn(); + + try { + Socket.prototype.setNoDelay = mockNoDelay; + const x = new MemcacheClient({ server: server }); + await x.set("foo", "bar"); + } finally { + Socket.prototype.setNoDelay = _setNoDelay; + } + + expect(mockNoDelay).not.toHaveBeenCalled(); + }); + + it("should enable TCP_NODELAY when options.noDelay is true", async () => { + await startSingleServer(); + + const _setNoDelay = Socket.prototype.setNoDelay; + const mockNoDelay = jest.fn(); + + try { + Socket.prototype.setNoDelay = mockNoDelay; + const x = new MemcacheClient({ server: server, noDelay: true }); + await x.set("foo", "bar"); + } finally { + Socket.prototype.setNoDelay = _setNoDelay; + } + + expect(mockNoDelay).toHaveBeenCalled(); + }); + const testMulti = (maxConnections: number = 1, done: () => void) => { const key1 = "text1维基百科"; const key2 = "blah";