Skip to content

Commit

Permalink
[FIXED] Inboxes suffix would contain many zeros
Browse files Browse the repository at this point in the history
This was introduced by PR #767
when the nuidSize was used instead of replySuffixLen to just build
the reply suffix. This meant that the suffix part with 22 characters
long and had lots of zeros at the end.

Added a test to make sure we don't break that in the future.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
  • Loading branch information
kozlovic committed Sep 1, 2021
1 parent 2ea8d39 commit eced299
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go_test.mod
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/golang/protobuf v1.4.2
github.com/nats-io/nats-server/v2 v2.3.5-0.20210825221009-41a253dabb43
github.com/nats-io/nats-server/v2 v2.4.1-0.20210831212757-2539bbb957ef
github.com/nats-io/nkeys v0.3.0
github.com/nats-io/nuid v1.0.1
google.golang.org/protobuf v1.23.0
Expand Down
6 changes: 3 additions & 3 deletions go_test.sum
Expand Up @@ -19,9 +19,9 @@ github.com/nats-io/jwt v1.2.2 h1:w3GMTO969dFg+UOKTmmyuu7IGdusK+7Ytlt//OYH/uU=
github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q=
github.com/nats-io/jwt/v2 v2.0.3 h1:i/O6cmIsjpcQyWDYNcq2JyZ3/VTF8SJ4JWluI5OhpvI=
github.com/nats-io/jwt/v2 v2.0.3/go.mod h1:VRP+deawSXyhNjXmxPCHskrR6Mq50BqpEI5SEcNiGlY=
github.com/nats-io/nats-server/v2 v2.3.5-0.20210825221009-41a253dabb43 h1:Sbb4QxNsccsPERg0C7uQX7/xgOCOTMIvDH9Ytb5MXsU=
github.com/nats-io/nats-server/v2 v2.3.5-0.20210825221009-41a253dabb43/go.mod h1:jgHRB+EfZisUr6j50/g7Gcah7AR8qtk3as42DJmESCk=
github.com/nats-io/nats.go v1.11.1-0.20210819195927-9053aa4200f0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nats-server/v2 v2.4.1-0.20210831212757-2539bbb957ef h1:fVi/sPmt1MjO1bA4M4jFlrCs9DYuZbpHPeKr7IQRM2Q=
github.com/nats-io/nats-server/v2 v2.4.1-0.20210831212757-2539bbb957ef/go.mod h1:TUAhMFYh1VISyY/D4WKJUMuGHg8yHtoUTuxkbiej1lc=
github.com/nats-io/nats.go v1.12.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s=
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
Expand Down
3 changes: 2 additions & 1 deletion nats.go
Expand Up @@ -3566,6 +3566,7 @@ func (nc *Conn) oldRequest(subj string, hdr, data []byte, timeout time.Duration)
const (
InboxPrefix = "_INBOX."
inboxPrefixLen = len(InboxPrefix)
replySuffixLen = 8 // Gives us 62^8
rdigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
base = 62
)
Expand Down Expand Up @@ -3615,7 +3616,7 @@ func (nc *Conn) newRespInbox() string {
sb.WriteString(nc.respSubPrefix)

rn := nc.respRand.Int63()
for i := 0; i < nuidSize; i++ {
for i := 0; i < replySuffixLen; i++ {
sb.WriteByte(rdigits[rn%base])
rn /= base
}
Expand Down
34 changes: 34 additions & 0 deletions nats_test.go
Expand Up @@ -2702,3 +2702,37 @@ func TestCustomInboxPrefix(t *testing.T) {
t.Fatalf("did not receive ok: %q", resp.Data)
}
}

func TestRespInbox(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()

if _, err := nc.Subscribe("foo", func(msg *Msg) {
lastDot := strings.LastIndex(msg.Reply, ".")
if lastDot == -1 {
msg.Respond([]byte(fmt.Sprintf("Invalid reply subject: %q", msg.Reply)))
return
}
lastToken := msg.Reply[lastDot+1:]
if len(lastToken) != replySuffixLen {
msg.Respond([]byte(fmt.Sprintf("Invalid last token: %q", lastToken)))
return
}
msg.Respond(nil)
}); err != nil {
t.Fatalf("subscribe failed: %s", err)
}
resp, err := nc.Request("foo", []byte("check inbox"), time.Second)
if err != nil {
t.Fatalf("Request failed: %v", err)
}
if len(resp.Data) > 0 {
t.Fatalf("Error: %s", resp.Data)
}
}

0 comments on commit eced299

Please sign in to comment.