Skip to content

Commit

Permalink
Merge pull request #609 from ripienaar/606
Browse files Browse the repository at this point in the history
ensure response message goes to the reply subject in RespondMsg
  • Loading branch information
ripienaar authored Nov 16, 2020
2 parents a35836e + 41eef2a commit 93be3c8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -3917,6 +3917,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 @@ -2646,3 +2646,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)
}
}

0 comments on commit 93be3c8

Please sign in to comment.