Skip to content

Commit

Permalink
fix parser
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Oct 25, 2023
1 parent 6cdfe1c commit 90902a1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
14 changes: 11 additions & 3 deletions examples/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func cmd() {
start := time.Now()
p := pool.New()

validator := rotom.NewCodec(rotom.Response).Int(int64(rotom.RES_SUCCESS)).Str("ok").B
delays := cache.NewPercentile()

for i := 0; i < CLIENT_NUM; i++ {
Expand All @@ -48,8 +47,17 @@ func cmd() {
if err != nil {
panic(err)
}
if !bytes.Equal(res, validator) {
panic(base.ErrInvalidResponse)
{
op, args, err := rotom.NewDecoder(res).ParseRecord()
if err != nil || op != rotom.Response {
panic("error")
}
if base.ParseInt[int64](args[0]) != rotom.RES_SUCCESS {
panic("error")
}
if !bytes.Equal(args[1], []byte("ok")) {
panic("error")
}
}

// stat
Expand Down
35 changes: 27 additions & 8 deletions rotom_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rotom

import (
"fmt"
"log/slog"
"os"
"strconv"
Expand Down Expand Up @@ -469,20 +470,38 @@ func TestClient(t *testing.T) {
db, err := Open(NoPersistentConfig)
assert.Nil(err)

go db.Listen("localhost:9876")
// listen
go db.Listen("localhost:7777")
time.Sleep(time.Second)

cli, err := NewClient("localhost:9876")
cli, err := NewClient("localhost:7777")
assert.Nil(err)

validator := NewCodec(Response).Int(int64(RES_SUCCESS)).Str("ok").B

// Set
for i := 0; i < 10000; i++ {
k := gofakeit.Phone()
v := gofakeit.UUID()
k := fmt.Sprintf("key-%d", i)
res, err := cli.Set(k, []byte(k))
assert.Nil(err)
{
op, args, err := NewDecoder(res).ParseRecord()
assert.Nil(err)
assert.Equal(op, Response)
assert.Equal(base.ParseInt[int64](args[0]), RES_SUCCESS)
assert.Equal(args[1], []byte("ok"))
}
}

res, err := cli.Set(k, []byte(v))
assert.Equal(res, validator)
// Get
for i := 0; i < 10000; i++ {
k := fmt.Sprintf("key-%d", i)
res, err := cli.Get(k)
assert.Nil(err)
{
op, args, err := NewDecoder(res).ParseRecord()
assert.Nil(err)
assert.Equal(op, Response)
assert.Equal(base.ParseInt[int64](args[0]), RES_SUCCESS)
assert.Equal(args[1], []byte(k))
}
}
}
16 changes: 6 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// Response code inplements.
type RespCode byte
type RespCode = int64

const (
RES_SUCCESS RespCode = iota + 1
Expand All @@ -30,10 +30,10 @@ func (e *RotomEngine) OnTraffic(conn gnet.Conn) gnet.Action {
msg, err := e.db.handleEvent(buf)
var cd *Codec
if err != nil {
cd = NewCodec(Response).Int(int64(RES_ERROR)).Str(err.Error())
cd = NewCodec(Response).Int(RES_ERROR).Str(err.Error())

} else {
cd = NewCodec(Response).Int(int64(RES_SUCCESS)).Bytes(msg)
cd = NewCodec(Response).Int(RES_SUCCESS).Bytes(msg)
}

// send resp
Expand Down Expand Up @@ -69,13 +69,9 @@ func (e *Engine) handleEvent(line []byte) (msg []byte, err error) {
return nil, base.ErrKeyNotFound

case OpSetTx: // type, key, ts, val
recType := VType(args[0][0])

switch recType {
case TypeString:
ts := base.ParseInt[int64](args[2])
e.SetTx(*b2s(args[1]), args[3], ts)
}
// type is String
ts := base.ParseInt[int64](args[2])
e.SetTx(*b2s(args[1]), args[3], ts)

case OpLPush: // key, item
e.LPush(*b2s(args[0]), *b2s(args[1]))
Expand Down

0 comments on commit 90902a1

Please sign in to comment.