Skip to content

Instantly share code, notes, and snippets.

@drazisil
Last active June 15, 2023 16:08
Show Gist options
  • Save drazisil/9b1bbfb1c35a57cf49c097d6f9e9186f to your computer and use it in GitHub Desktop.
Save drazisil/9b1bbfb1c35a57cf49c097d6f9e9186f to your computer and use it in GitHub Desktop.
export function TCPListener({
incomingSocket,
config,
log,
onSocketData = onSocketData,
onSocketError = onSocketError,
}: {
incomingSocket: ISocket;
config: TServerConfiguration;
log: TServerLogger;
onSocketData: (
sock: ISocket,
data: Buffer,
log: TServerLogger,
config: TServerConfiguration,
connection: IConnection,
connectionRecord: TSocketWithConnectionInfo
) => void,
onSocketError: (
sock: ISocket,
error: IError,
log: TServerLogger
) => void
}): void {}
it("should handle the data event", () => {
// Arrange
const logOnSocketDataSpy = sinon.spy(onSocketData);
// Act
onSocketData(
fakeSocket,
Buffer.from([
0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100,
0x100,
]),
fakeLog,
fakeConfig,
connection,
fakeConnectionRecord
);
// Assert
expect(logOnSocketDataSpy).to.have.been.called;
});
it("should handle the data event", () => {
// Arrange
const fakeSocket: ISocket = ISocketTestFactory();
const fakeLog: TServerLogger = (level, msg) => {};
const fakeConfig: TServerConfiguration = {
EXTERNAL_HOST: "localhost",
certificateFileContents: "",
privateKeyContents: "",
publicKeyContents: "",
LOG_LEVEL: "debug",
};
const connection = IConnectionFactory();
connection.socket = fakeSocket;
const fakeConnectionRecord: TSocketWithConnectionInfo = {
id: "1234",
localPort: 0,
remoteAddress: "",
socket: fakeSocket,
encryptionSession: undefined,
useEncryption: false,
connectionId: "",
seq: 0,
personaId: 0,
lastMessageTimestamp: 0,
inQueue: false,
};
const logSpy = sinon.spy(fakeLog);
const module = { onSocketData };
const logOnSocketDataSpy = sinon.spy(module, "onSocketData");
const ConnectionManagerStub = sinon.stub(ConnectionManager.prototype);
sinon.stub(MessageHeader, "deserialize").returns({
length: 0,
signature: "",
serialize: () => Buffer.from(""),
});
ConnectionManagerStub.connections = [];
ConnectionManagerStub.connections.push(connection);
// Act
onSocketData(
fakeSocket,
Buffer.from([
0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100, 0x100,
0x100,
]),
fakeLog,
fakeConfig,
connection,
fakeConnectionRecord
);
// Assert
expect(logOnSocketDataSpy).to.have.been.called;
expect(logSpy.callCount).to.equal(1);
expect(logSpy).to.have.been.calledWith(sinon.match("Received data"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment