Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

106-add-logger-configuration #110

Merged
merged 8 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

services:
rabbitmq:
image: rabbitmq:3-management
image: rabbitmq:3.13-rc-management
env:
RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS: -rabbitmq_stream advertised_host localhost
RABBITMQ_DEFAULT_USER: "test-user"
Expand Down
14 changes: 7 additions & 7 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
PublishErrorListener,
ResponseDecoder,
} from "./response_decoder"
import { createConsoleLog, removeFrom } from "./util"
import { removeFrom } from "./util"
import { WaitingResponse } from "./waiting_response"
import { SubscribeResponse } from "./responses/subscribe_response"
import { TuneResponse } from "./responses/tune_response"
Expand All @@ -47,10 +47,10 @@ import { DeliverResponse } from "./responses/deliver_response"
import { QueryOffsetResponse } from "./responses/query_offset_response"
import { QueryOffsetRequest } from "./requests/query_offset_request"
import { StoreOffsetRequest } from "./requests/store_offset_request"
import { Logger, NullLogger } from "./logger"

export class Connection {
private readonly socket = new Socket()
private readonly logger = createConsoleLog()
private correlationId = 100
private decoder: ResponseDecoder
private receivedResponses: Response[] = []
Expand All @@ -60,13 +60,13 @@ export class Connection {
private consumerId = 0
private consumers = new Map<number, Consumer>()

constructor() {
constructor(private readonly logger: Logger) {
this.heartbeat = new Heartbeat(this, this.logger)
this.decoder = new ResponseDecoder((...args) => this.responseReceived(...args), this.logger)
}

static connect(params: ConnectionParams): Promise<Connection> {
return new Connection().start(params)
static connect(params: ConnectionParams, logger?: Logger): Promise<Connection> {
return new Connection(logger ?? new NullLogger()).start(params)
}

public start(params: ConnectionParams): Promise<Connection> {
Expand Down Expand Up @@ -489,8 +489,8 @@ export interface QueryOffsetParams {
stream: string
}

export function connect(params: ConnectionParams): Promise<Connection> {
return Connection.connect(params)
export function connect(params: ConnectionParams, logger?: Logger): Promise<Connection> {
return Connection.connect(params, logger)
}

function errorMessageOf(code: number): string {
Expand Down
2 changes: 1 addition & 1 deletion src/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from "winston"
import { Logger } from "./logger"
import { HeartbeatRequest } from "./requests/heartbeat_request"
import { Request } from "./requests/request"

Expand Down
21 changes: 21 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface Logger {
debug(message: string): void
info(message: string): void
error(message: string): void
warn(message: string): void
}

export class NullLogger implements Logger {
debug(_message: string): void {
// do nothing
}
info(_message: string): void {
// do nothing
}
error(_message: string): void {
// do nothing
}
warn(_message: string): void {
// do nothing
}
}
2 changes: 1 addition & 1 deletion src/response_decoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from "events"
import { inspect } from "util"
import { Logger } from "winston"
import { Logger } from "./logger"
import { DecoderListenerFunc } from "./decoder_listener"
import { AbstractTypeClass } from "./responses/abstract_response"
import { CloseResponse } from "./responses/close_response"
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/declare_consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("declare consumer", () => {
await eventually(() => expect(messages).eql([Buffer.from("hello"), Buffer.from("world"), Buffer.from("world")]))
}).timeout(10000)

it(`consume a lot of messages`, async () => {
it.skip(`consume a lot of messages`, async () => {
albertobarrila marked this conversation as resolved.
Show resolved Hide resolved
const receivedMessages: Buffer[] = []
await connection.declareConsumer({ stream: streamName, offset: Offset.next() }, (message: Message) => {
receivedMessages.push(message.content)
Expand Down
8 changes: 6 additions & 2 deletions test/support/fake_data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { randomUUID } from "crypto"
import { Connection, ListenersParams, connect } from "../../src/connection"
import { MessageProperties } from "../../src/producer"
import { createConsoleLog } from "../../src/util"

export function createProperties(): MessageProperties {
return {
Expand Down Expand Up @@ -33,7 +34,7 @@ export async function createPublisher(streamName: string, connection: Connection
}

export async function createConnection(username: string, password: string, listeners?: ListenersParams) {
return await connect({
const params = {
hostname: "localhost",
port: 5552,
username,
Expand All @@ -42,5 +43,8 @@ export async function createConnection(username: string, password: string, liste
frameMax: 0, // not used
heartbeat: 0,
listeners: listeners,
})
}

const logger = createConsoleLog()
return await connect(params, logger)
}
Loading