|
1 | 1 | package integration_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "fmt"
|
| 6 | + "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/message" |
5 | 7 | "sync"
|
6 | 8 | "time"
|
7 | 9 |
|
8 | 10 | . "github.com/onsi/ginkgo/v2"
|
9 | 11 | . "github.com/onsi/gomega"
|
10 | 12 |
|
11 | 13 | "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
|
12 |
| - "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/message" |
13 | 14 | stream "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
|
14 | 15 | )
|
15 | 16 |
|
@@ -146,4 +147,97 @@ var _ = Describe("StreamIntegration", func() {
|
146 | 147 | Expect(consumer.GetOffset()).To(BeNumerically("==", expectedCurrentOffset))
|
147 | 148 | })
|
148 | 149 | })
|
| 150 | + |
| 151 | + Context("Initial timestamp offset when no messages exist", func() { |
| 152 | + var ( |
| 153 | + addresses []string = []string{ |
| 154 | + "rabbitmq-stream://guest:guest@localhost:5552/"} |
| 155 | + streamName string = "empty-test-stream" |
| 156 | + streamEnv *stream.Environment |
| 157 | + ) |
| 158 | + |
| 159 | + // init empty stream |
| 160 | + BeforeEach(func() { |
| 161 | + var err error |
| 162 | + streamEnv, err = stream.NewEnvironment( |
| 163 | + stream.NewEnvironmentOptions().SetUris(addresses)) |
| 164 | + Expect(err).ToNot(HaveOccurred()) |
| 165 | + |
| 166 | + err = streamEnv.DeclareStream(streamName, |
| 167 | + stream.NewStreamOptions().SetMaxLengthBytes(stream.ByteCapacity{}.GB(2))) |
| 168 | + Expect(err).ToNot(HaveOccurred()) |
| 169 | + }) |
| 170 | + |
| 171 | + AfterEach(func() { |
| 172 | + Expect(streamEnv.DeleteStream(streamName)). |
| 173 | + To(SatisfyAny( |
| 174 | + Succeed(), |
| 175 | + MatchError(stream.StreamDoesNotExist), |
| 176 | + )) |
| 177 | + }) |
| 178 | + |
| 179 | + It("correctly handles offsets using timestamps when no messages exist", func() { |
| 180 | + var err error |
| 181 | + const consumerName = "timestamp-offset-consumer" |
| 182 | + |
| 183 | + lastMinute := time.Now().Add(-time.Minute).UnixMilli() |
| 184 | + |
| 185 | + // Implement the UpdateConsumer function to return a timestamp-based offset if no offset exists |
| 186 | + // For example, we add a new consumer to the incoming stream and don't want to reread it from the beginning. |
| 187 | + updateConsumer := func(streamName string, isActive bool) stream.OffsetSpecification { |
| 188 | + offset, err := streamEnv.QueryOffset(consumerName, streamName) |
| 189 | + if errors.Is(err, stream.OffsetNotFoundError) { |
| 190 | + return stream.OffsetSpecification{}.Timestamp(lastMinute) |
| 191 | + } |
| 192 | + |
| 193 | + Expect(err).ToNot(HaveOccurred()) |
| 194 | + |
| 195 | + return stream.OffsetSpecification{}.Offset(offset + 1) |
| 196 | + } |
| 197 | + |
| 198 | + options := stream.NewConsumerOptions(). |
| 199 | + SetConsumerName(consumerName). |
| 200 | + SetAutoCommit(stream.NewAutoCommitStrategy(). |
| 201 | + SetFlushInterval(time.Second)). |
| 202 | + SetSingleActiveConsumer(stream.NewSingleActiveConsumer(updateConsumer)) |
| 203 | + |
| 204 | + // Create the consumer |
| 205 | + consumer, err := streamEnv.NewConsumer( |
| 206 | + streamName, |
| 207 | + func(ctx stream.ConsumerContext, msg *amqp.Message) {}, |
| 208 | + options, |
| 209 | + ) |
| 210 | + Expect(err).NotTo(HaveOccurred()) |
| 211 | + |
| 212 | + // Wait for a flush without messages |
| 213 | + // An incorrect offset is stored during this flush |
| 214 | + time.Sleep(time.Millisecond * 1200) |
| 215 | + Expect(consumer.Close()).ToNot(HaveOccurred()) |
| 216 | + |
| 217 | + // Re-create the consumer |
| 218 | + consumeIsStarted := make(chan struct{}) |
| 219 | + handleMessages := func(ctx stream.ConsumerContext, msg *amqp.Message) { |
| 220 | + close(consumeIsStarted) |
| 221 | + } |
| 222 | + |
| 223 | + consumer, err = streamEnv.NewConsumer(streamName, handleMessages, options) |
| 224 | + Expect(err).NotTo(HaveOccurred()) |
| 225 | + |
| 226 | + producer, err := streamEnv.NewProducer(streamName, nil) |
| 227 | + Expect(err).ToNot(HaveOccurred()) |
| 228 | + body := `{"name": "item-1}` |
| 229 | + err = producer.Send(amqp.NewMessage([]byte(body))) |
| 230 | + Expect(err).ToNot(HaveOccurred()) |
| 231 | + |
| 232 | + // check if messages are consumed |
| 233 | + select { |
| 234 | + case <-consumeIsStarted: |
| 235 | + case <-time.After(time.Second * 1): |
| 236 | + Fail("Timeout waiting for consumer to start") |
| 237 | + } |
| 238 | + |
| 239 | + Expect(consumer.GetOffset()).To(BeNumerically("<=", 0)) |
| 240 | + }) |
| 241 | + |
| 242 | + }) |
149 | 243 | })
|
0 commit comments