Skip to content

Commit 204a928

Browse files
authored
Make stream creation idempotent (#101)
* Make stream creation idempotent closes #99
1 parent 71eff1d commit 204a928

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ err = env.DeclareStream(streamName,
176176
SetMaxLengthBytes(stream.ByteCapacity{}.GB(2)))
177177
```
178178

179-
Note: The function `DeclareStream` returns `stream.StreamAlreadyExists` if a stream is already defined.
179+
The function `DeclareStream` doesn't return errors if a stream is already defined with the same parameters.
180+
Note that it returns the precondition failed when it doesn't have the same parameters
181+
Use `StreamExists` to check if a stream exists.
180182

181183

182184
### Publish messages

pkg/stream/client_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,19 @@ var _ = Describe("Streaming testEnvironment", func() {
115115
It("Create two times Stream", func() {
116116
Expect(testEnvironment.DeclareStream(testStreamName, nil)).NotTo(HaveOccurred())
117117
err := testEnvironment.DeclareStream(testStreamName, nil)
118-
Expect(err).To(HaveOccurred())
119-
Expect(err).To(Equal(StreamAlreadyExists))
118+
Expect(err).NotTo(HaveOccurred())
119+
Expect(testEnvironment.DeleteStream(testStreamName)).NotTo(HaveOccurred())
120+
})
121+
122+
It("Stream Exists", func() {
123+
exists, err := testEnvironment.StreamExists(testStreamName)
124+
Expect(err).NotTo(HaveOccurred())
125+
Expect(exists).To(Equal(false))
126+
127+
Expect(testEnvironment.DeclareStream(testStreamName, nil)).NotTo(HaveOccurred())
128+
exists, err = testEnvironment.StreamExists(testStreamName)
129+
Expect(err).NotTo(HaveOccurred())
130+
Expect(exists).To(Equal(true))
120131
Expect(testEnvironment.DeleteStream(testStreamName)).NotTo(HaveOccurred())
121132
})
122133

pkg/stream/enviroment.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ func (env *Environment) DeclareStream(streamName string, options *StreamOptions)
104104
if err != nil {
105105
return err
106106
}
107-
return client.DeclareStream(streamName, options)
107+
if err := client.DeclareStream(streamName, options); err != nil && err != StreamAlreadyExists {
108+
return err
109+
}
110+
return nil
111+
108112
}
109113

110114
func (env *Environment) DeleteStream(streamName string) error {

0 commit comments

Comments
 (0)