-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add's streamStatsRequest function to connection. This returns an error if one occurs, or a map of stream stats
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { StreamStatsResponse } from "../responses/stream_stats_response" | ||
import { AbstractRequest } from "./abstract_request" | ||
import { DataWriter } from "./data_writer" | ||
|
||
export class StreamStatsRequest extends AbstractRequest { | ||
readonly responseKey = StreamStatsResponse.key | ||
readonly key = 0x001c | ||
|
||
constructor(private streamName: string) { | ||
super() | ||
} | ||
|
||
writeContent(writer: DataWriter) { | ||
writer.writeString(this.streamName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { AbstractResponse } from "./abstract_response" | ||
import { RawResponse } from "./raw_response" | ||
|
||
export class StreamStatsResponse extends AbstractResponse { | ||
static key = 0x801c | ||
readonly statistics: Record<string, bigint> = {} | ||
|
||
constructor(response: RawResponse) { | ||
super(response) | ||
this.verifyKey(StreamStatsResponse) | ||
|
||
const stats = this.response.payload.readInt32() | ||
for (let i = 0; i < stats; i++) { | ||
const statKey = this.response.payload.readString() | ||
const statVal = this.response.payload.readInt64() | ||
this.statistics[statKey] = statVal | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Connection } from "../../src" | ||
import { expect } from "chai" | ||
import { Rabbit } from "../support/rabbit" | ||
import { randomUUID } from "crypto" | ||
import { expectToThrowAsync, username, password } from "../support/util" | ||
import { createConnection } from "../support/fake_data" | ||
|
||
describe("StreamStats", () => { | ||
const rabbit = new Rabbit(username, password) | ||
const testStreamName = "test-stream" | ||
let connection: Connection | ||
let publisherRef: string | ||
|
||
beforeEach(async () => { | ||
publisherRef = randomUUID() | ||
await rabbit.createStream(testStreamName) | ||
connection = await createConnection(username, password) | ||
}) | ||
|
||
afterEach(async () => { | ||
await connection.close() | ||
await rabbit.deleteStream(testStreamName) | ||
}) | ||
|
||
it("gets statistics for a stream", async () => { | ||
const publisher = await connection.declarePublisher({ stream: testStreamName, publisherRef }) | ||
for (let i = 0; i < 5; i++) { | ||
await publisher.send(Buffer.from(`test${randomUUID()}`)) | ||
} | ||
|
||
const stats = await connection.streamStatsRequest(testStreamName) | ||
|
||
expect(stats.committed_chunk_id).to.be.a("BigInt") | ||
expect(stats.first_chunk_id).to.be.a("BigInt") | ||
expect(stats.last_chunk_id).to.be.a("BigInt") | ||
}).timeout(10000) | ||
|
||
it("returns an error when the stream does not exist", async () => { | ||
await expectToThrowAsync( | ||
() => connection.streamStatsRequest("stream-does-not-exist"), | ||
Error, | ||
"Stream Stats command returned error with code 2 - Stream does not exist" | ||
) | ||
}).timeout(10000) | ||
}) |