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

Fix setting deliver policy in Fetch() for OrderedConsumer #1693

Merged
merged 1 commit into from
Aug 7, 2024
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
3 changes: 2 additions & 1 deletion jetstream/ordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ func (c *orderedConsumer) getConsumerConfig() *ConsumerConfig {
cfg.InactiveThreshold = c.cfg.InactiveThreshold
}

if c.serial != 1 {
// if the cursor is not yet set, use the provided deliver policy
if c.cursor.streamSeq != 0 {
return cfg
}

Expand Down
65 changes: 65 additions & 0 deletions jetstream/test/ordered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,71 @@ func TestOrderedConsumerFetch(t *testing.T) {
}
})

t.Run("with custom deliver policy", func(t *testing.T) {
srv := RunBasicJetStreamServer()
defer shutdownJSServerAndRemoveStorage(t, srv)
nc, err := nats.Connect(srv.ClientURL())
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

js, err := jetstream.New(nc)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer nc.Close()

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
s, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "foo", Subjects: []string{"FOO.*"}})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
msgs := make([]jetstream.Msg, 0)

for i := 0; i < 5; i++ {
if _, err := js.Publish(context.Background(), "FOO.A", []byte("msg")); err != nil {
t.Fatalf("Unexpected error during publish: %s", err)
}
}
for i := 0; i < 5; i++ {
if _, err := js.Publish(context.Background(), "FOO.B", []byte("msg")); err != nil {
t.Fatalf("Unexpected error during publish: %s", err)
}
}

c, err := s.OrderedConsumer(ctx, jetstream.OrderedConsumerConfig{
DeliverPolicy: jetstream.DeliverLastPerSubjectPolicy,
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

res, err := c.Fetch(int(c.CachedInfo().NumPending), jetstream.FetchMaxWait(1*time.Second))
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}

for msg := range res.Messages() {
msgs = append(msgs, msg)
}

if res.Error() != nil {
t.Fatalf("Unexpected error: %s", err)
}

if len(msgs) != 2 {
t.Fatalf("Expected %d messages; got: %d", 2, len(msgs))
}
expectedSubjects := []string{"FOO.A", "FOO.B"}

for i := range msgs {
if msgs[i].Subject() != expectedSubjects[i] {
t.Fatalf("Expected subject: %s; got: %s", expectedSubjects[i], msgs[i].Subject())
}
}
})

t.Run("consumer used as consume", func(t *testing.T) {
srv := RunBasicJetStreamServer()
defer shutdownJSServerAndRemoveStorage(t, srv)
Expand Down