-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomm_test.go
More file actions
64 lines (53 loc) · 1.36 KB
/
comm_test.go
File metadata and controls
64 lines (53 loc) · 1.36 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package stash
import (
"bytes"
"errors"
"log"
"testing"
"time"
"github.com/maximp/stash/client"
"github.com/maximp/stash/server"
)
func TestServerComm(t *testing.T) {
stop := make(chan struct{})
var buf bytes.Buffer
cfg := server.Config{
Logger: log.New(&buf, "", 0),
Stop: stop,
}
var cmd string
var arg string
handler := func(c []byte, a []byte) ([]byte, error) {
cmd = string(c)
arg = string(a)
if cmd == "error" {
return nil, errors.New("error")
}
return []byte("ok"), nil
}
var err error
stopped := make(chan struct{})
go func() {
err = server.ListenAndServe("", handler, &cfg)
stopped <- struct{}{}
}()
time.Sleep(10 * time.Millisecond)
if err != nil {
t.Fatal(err)
}
if conn, err := client.Dial("127.0.0.1:7777"); err != nil {
t.Error(err)
} else {
defer conn.Close()
code, line, err := conn.Cmd("command")
if code != server.ServerOperationOk || line != "ok" || err != nil || cmd != "command" || arg != "" {
t.Errorf("error command processing: code=%d, line=%s, err=%v, cmd=%s, arg=%s", code, line, err, cmd, arg)
}
code, line, err = conn.Cmd("error")
if code != server.ServerOperationError || line != "error" || err != nil || cmd != "error" || arg != "" {
t.Errorf("error command processing: code=%d, line=%s, err=%v, cmd=%s, arg=%s", code, line, err, cmd, arg)
}
}
stop <- struct{}{}
<-stopped
}