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

make create stream idempotent #32

Merged
merged 2 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { CreateStreamResponse } from "./responses/create_stream_response"
import { CreateStreamRequest, CreateStreamArguments } from "./requests/create_stream_request"
import { Heartbeat } from "./heartbeat"
import { TuneRequest } from "./requests/tune_request"
import { STREAM_ALREADY_EXISTS_ERROR_CODE } from "./error_codes"

export class Connection {
private readonly socket = new Socket()
Expand Down Expand Up @@ -184,9 +185,13 @@ export class Connection {
async createStream(params: { stream: string; arguments: CreateStreamArguments }): Promise<true> {
this.logger.debug(`Create Stream...`)
const res = await this.sendAndWait<CreateStreamResponse>(new CreateStreamRequest(params))
if (res.code === STREAM_ALREADY_EXISTS_ERROR_CODE) {
return true
}
if (!res.ok) {
throw new Error(`Create Stream command returned error with code ${res.code}`)
}

this.logger.debug(`Create Stream response: ${res.ok} - with arguments: '${inspect(params.arguments)}'`)
return res.ok
}
Expand Down
1 change: 1 addition & 0 deletions src/error_codes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const STREAM_ALREADY_EXISTS_ERROR_CODE = 0x05
17 changes: 7 additions & 10 deletions test/unit/create_stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,18 @@ describe("Stream", () => {
expect(result.name).to.be.eql(streamName)
})

it("Should detect a duplicate Stream", async () => {
it("Should be idempotent and ignore a duplicate Stream error", async () => {
await connection.createStream({
stream: streamName,
arguments: payload,
})

await expectToThrowAsync(
() =>
connection.createStream({
stream: streamName,
arguments: payload,
}),
Error,
"Create Stream command returned error with code 5"
)
const resp = await connection.createStream({
stream: streamName,
arguments: payload,
})

expect(resp).to.be.true
})

it("Should raise an error if creation goes wrong", async () => {
Expand Down