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

ensure response message goes to the reply subject in RespondMsg #609

Merged
merged 1 commit into from
Nov 16, 2020
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
1 change: 1 addition & 0 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -3888,6 +3888,7 @@ func (m *Msg) RespondMsg(msg *Msg) error {
if m.Reply == "" {
return ErrMsgNoReply
}
msg.Subject = m.Reply
m.Sub.mu.Lock()
nc := m.Sub.conn
m.Sub.mu.Unlock()
Expand Down
37 changes: 37 additions & 0 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2645,3 +2645,40 @@ func TestJSSubscribe(t *testing.T) {
t.Fatalf("subscriber not subscribed to the delivery subject")
}
}

func TestMsg_RespondMsg(t *testing.T) {
s := RunServerOnPort(-1)
defer s.Shutdown()

nc, err := Connect(s.ClientURL())
if err != nil {
t.Fatalf("Expected to connect to server, got %v", err)
}
defer nc.Close()

sub, err := nc.SubscribeSync(NewInbox())
if err != nil {
t.Fatalf("subscribe failed: %s", err)
}

nc.PublishMsg(&Msg{Reply: sub.Subject, Subject: sub.Subject, Data: []byte("request")})
req, err := sub.NextMsg(time.Second)
if err != nil {
t.Fatalf("NextMsg failed: %s", err)
}

// verifies that RespondMsg sets the reply subject on msg based on req
err = req.RespondMsg(&Msg{Data: []byte("response")})
if err != nil {
t.Fatalf("RespondMsg failed: %s", err)
}

resp, err := sub.NextMsg(time.Second)
if err != nil {
t.Fatalf("NextMsg failed: %s", err)
}

if !bytes.Equal(resp.Data, []byte("response")) {
t.Fatalf("did not get correct response: %q", resp.Data)
}
}