-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest_test.go
45 lines (41 loc) · 1.08 KB
/
request_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package exposed
import (
"bufio"
"bytes"
"fmt"
"testing"
)
func TestRequestMarshalUnmarshal(t *testing.T) {
var buf bytes.Buffer
req := acquireRequest()
bw := bufio.NewWriter(&buf)
for i := 0; i < 10; i++ {
op := uint64(1)
value := fmt.Sprintf("payload %d", i)
req.SetOperation(op)
req.SwapPayload([]byte(value))
if err := req.WriteRequest(bw); err != nil {
t.Fatalf("unexpected error when writing request: %s", err)
}
}
if err := bw.Flush(); err != nil {
t.Fatalf("unexpected error when flushing request: %s", err)
}
releaseRequest(req)
req1 := acquireRequest()
br := bufio.NewReader(&buf)
for i := 0; i < 10; i++ {
name := uint64(1)
value := fmt.Sprintf("payload %d", i)
if err := req1.ReadRequest(br); err != nil {
t.Fatalf("unexpected error when reading request: %s", err)
}
if req1.Operation() != name {
t.Fatalf("unexpected request operation read: %q. Expecting %q", req1.Operation(), name)
}
if string(req1.Payload()) != value {
t.Fatalf("unexpected request payload read: %q. Expecting %q", req1.Payload(), value)
}
}
releaseRequest(req1)
}