First pass at headers awareness for server

Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
Derek Collison
2020-04-30 17:05:37 -07:00
parent 3ab203d164
commit d51566881e
9 changed files with 370 additions and 80 deletions

View File

@@ -235,70 +235,38 @@ func TestParsePubArg(t *testing.T) {
size int
szb string
}{
{arg: "a 2",
subject: "a", reply: "", size: 2, szb: "2"},
{arg: "a 222",
subject: "a", reply: "", size: 222, szb: "222"},
{arg: "foo 22",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: " foo 22",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo 22 ",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo 22",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: " foo 22 ",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: " foo 22 ",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo bar 22",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: " foo bar 22",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "foo bar 22 ",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "foo bar 22",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: " foo bar 22 ",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: " foo bar 22 ",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: " foo bar 2222 ",
subject: "foo", reply: "bar", size: 2222, szb: "2222"},
{arg: " foo 2222 ",
subject: "foo", reply: "", size: 2222, szb: "2222"},
{arg: "a\t2",
subject: "a", reply: "", size: 2, szb: "2"},
{arg: "a\t222",
subject: "a", reply: "", size: 222, szb: "222"},
{arg: "foo\t22",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "\tfoo\t22",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo\t22\t",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo\t\t\t22",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "\tfoo\t22\t",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "\tfoo\t\t\t22\t",
subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo\tbar\t22",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "\tfoo\tbar\t22",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "foo\tbar\t22\t",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "foo\t\tbar\t\t22",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "\tfoo\tbar\t22\t",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "\t \tfoo\t \t \tbar\t \t22\t \t",
subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "\t\tfoo\t\t\tbar\t\t2222\t\t",
subject: "foo", reply: "bar", size: 2222, szb: "2222"},
{arg: "\t \tfoo\t \t \t\t\t2222\t \t",
subject: "foo", reply: "", size: 2222, szb: "2222"},
{arg: "a 2", subject: "a", reply: "", size: 2, szb: "2"},
{arg: "a 222", subject: "a", reply: "", size: 222, szb: "222"},
{arg: "foo 22", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: " foo 22", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo 22 ", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo 22", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: " foo 22 ", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: " foo 22 ", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo bar 22", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: " foo bar 22", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "foo bar 22 ", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "foo bar 22", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: " foo bar 22 ", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: " foo bar 22 ", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: " foo bar 2222 ", subject: "foo", reply: "bar", size: 2222, szb: "2222"},
{arg: " foo 2222 ", subject: "foo", reply: "", size: 2222, szb: "2222"},
{arg: "a\t2", subject: "a", reply: "", size: 2, szb: "2"},
{arg: "a\t222", subject: "a", reply: "", size: 222, szb: "222"},
{arg: "foo\t22", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "\tfoo\t22", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo\t22\t", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo\t\t\t22", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "\tfoo\t22\t", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "\tfoo\t\t\t22\t", subject: "foo", reply: "", size: 22, szb: "22"},
{arg: "foo\tbar\t22", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "\tfoo\tbar\t22", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "foo\tbar\t22\t", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "foo\t\tbar\t\t22", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "\tfoo\tbar\t22\t", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "\t \tfoo\t \t \tbar\t \t22\t \t", subject: "foo", reply: "bar", size: 22, szb: "22"},
{arg: "\t\tfoo\t\t\tbar\t\t2222\t\t", subject: "foo", reply: "bar", size: 2222, szb: "2222"},
{arg: "\t \tfoo\t \t \t\t\t2222\t \t", subject: "foo", reply: "", size: 2222, szb: "2222"},
} {
t.Run(test.arg, func(t *testing.T) {
if err := c.processPub([]byte(test.arg)); err != nil {
@@ -329,6 +297,134 @@ func TestParsePubBadSize(t *testing.T) {
}
}
func TestParseHeaderPub(t *testing.T) {
c := dummyClient()
hpub := []byte("HPUB foo 12 17\r\nname:derek\r\nHELLO\r")
if err := c.parse(hpub); err != nil || c.state != MSG_END_N {
t.Fatalf("Unexpected: %d : %v\n", c.state, err)
}
if !bytes.Equal(c.pa.subject, []byte("foo")) {
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject)
}
if c.pa.reply != nil {
t.Fatalf("Did not parse reply correctly: 'nil' vs '%s'\n", c.pa.reply)
}
if c.pa.hdr != 12 {
t.Fatalf("Did not parse msg header size correctly: 12 vs %d\n", c.pa.hdr)
}
if c.pa.size != 17 {
t.Fatalf("Did not parse msg size correctly: 17 vs %d\n", c.pa.size)
}
// Clear snapshots
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START
hpub = []byte("HPUB foo INBOX.22 12 17\r\nname:derek\r\nHELLO\r")
if err := c.parse(hpub); err != nil || c.state != MSG_END_N {
t.Fatalf("Unexpected: %d : %v\n", c.state, err)
}
if !bytes.Equal(c.pa.subject, []byte("foo")) {
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject)
}
if !bytes.Equal(c.pa.reply, []byte("INBOX.22")) {
t.Fatalf("Did not parse reply correctly: 'INBOX.22' vs '%s'\n", c.pa.reply)
}
if c.pa.hdr != 12 {
t.Fatalf("Did not parse msg header size correctly: 12 vs %d\n", c.pa.hdr)
}
if c.pa.size != 17 {
t.Fatalf("Did not parse msg size correctly: 17 vs %d\n", c.pa.size)
}
// Clear snapshots
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START
hpub = []byte("HPUB foo INBOX.22 0 5\r\nHELLO\r")
if err := c.parse(hpub); err != nil || c.state != MSG_END_N {
t.Fatalf("Unexpected: %d : %v\n", c.state, err)
}
if !bytes.Equal(c.pa.subject, []byte("foo")) {
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject)
}
if !bytes.Equal(c.pa.reply, []byte("INBOX.22")) {
t.Fatalf("Did not parse reply correctly: 'INBOX.22' vs '%s'\n", c.pa.reply)
}
if c.pa.hdr != 0 {
t.Fatalf("Did not parse msg header size correctly: 0 vs %d\n", c.pa.hdr)
}
if c.pa.size != 5 {
t.Fatalf("Did not parse msg size correctly: 5 vs %d\n", c.pa.size)
}
}
func TestParseHeaderPubArg(t *testing.T) {
c := dummyClient()
for _, test := range []struct {
arg string
subject string
reply string
hdr int
size int
szb string
}{
{arg: "a 2 4", subject: "a", reply: "", hdr: 2, size: 4, szb: "4"},
{arg: "a 22 222", subject: "a", reply: "", hdr: 22, size: 222, szb: "222"},
{arg: "foo 3 22", subject: "foo", reply: "", hdr: 3, size: 22, szb: "22"},
{arg: " foo 1 22", subject: "foo", reply: "", hdr: 1, size: 22, szb: "22"},
{arg: "foo 0 22 ", subject: "foo", reply: "", hdr: 0, size: 22, szb: "22"},
{arg: "foo 0 22", subject: "foo", reply: "", hdr: 0, size: 22, szb: "22"},
{arg: " foo 1 22 ", subject: "foo", reply: "", hdr: 1, size: 22, szb: "22"},
{arg: " foo 3 22 ", subject: "foo", reply: "", hdr: 3, size: 22, szb: "22"},
{arg: "foo bar 1 22", subject: "foo", reply: "bar", hdr: 1, size: 22, szb: "22"},
{arg: " foo bar 11 22", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: "foo bar 11 22 ", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: "foo bar 11 22", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: " foo bar 11 22 ", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: " foo bar 11 22 ", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: " foo bar 22 2222 ", subject: "foo", reply: "bar", hdr: 22, size: 2222, szb: "2222"},
{arg: " foo 1 2222 ", subject: "foo", reply: "", hdr: 1, size: 2222, szb: "2222"},
{arg: "a\t2\t22", subject: "a", reply: "", hdr: 2, size: 22, szb: "22"},
{arg: "a\t2\t\t222", subject: "a", reply: "", hdr: 2, size: 222, szb: "222"},
{arg: "foo\t2 22", subject: "foo", reply: "", hdr: 2, size: 22, szb: "22"},
{arg: "\tfoo\t11\t 22", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"},
{arg: "foo\t11\t22\t", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"},
{arg: "foo\t\t\t11 22", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"},
{arg: "\tfoo\t11\t \t 22\t", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"},
{arg: "\tfoo\t\t\t11 22\t", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"},
{arg: "foo\tbar\t2 22", subject: "foo", reply: "bar", hdr: 2, size: 22, szb: "22"},
{arg: "\tfoo\tbar\t11\t22", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: "foo\tbar\t11\t\t22\t ", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: "foo\t\tbar\t\t11\t\t\t22", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: "\tfoo\tbar\t11\t22\t", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: "\t \tfoo\t \t \tbar\t \t11\t 22\t \t", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"},
{arg: "\t\tfoo\t\t\tbar\t\t22\t\t\t2222\t\t", subject: "foo", reply: "bar", hdr: 22, size: 2222, szb: "2222"},
{arg: "\t \tfoo\t \t \t\t\t11\t\t 2222\t \t", subject: "foo", reply: "", hdr: 11, size: 2222, szb: "2222"},
} {
t.Run(test.arg, func(t *testing.T) {
if err := c.processHeaderPub([]byte(test.arg)); err != nil {
t.Fatalf("Unexpected parse error: %v\n", err)
}
if !bytes.Equal(c.pa.subject, []byte(test.subject)) {
t.Fatalf("Mismatched subject: '%s'\n", c.pa.subject)
}
if !bytes.Equal(c.pa.reply, []byte(test.reply)) {
t.Fatalf("Mismatched reply subject: '%s'\n", c.pa.reply)
}
if !bytes.Equal(c.pa.szb, []byte(test.szb)) {
t.Fatalf("Bad size buf: '%s'\n", c.pa.szb)
}
if c.pa.hdr != test.hdr {
t.Fatalf("Bad header size: %d\n", c.pa.hdr)
}
if c.pa.size != test.size {
t.Fatalf("Bad size: %d\n", c.pa.size)
}
})
}
}
func TestParseRouteMsg(t *testing.T) {
c := dummyRouteClient()