forked from distributedio/titan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
95 lines (84 loc) · 1.78 KB
/
server_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package command
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/meitu/titan/context"
"github.com/meitu/titan/db"
"github.com/stretchr/testify/assert"
)
func TestInfo(t *testing.T) {
out := bytes.NewBuffer(nil)
ctx := &Context{
Name: "info",
Args: nil,
In: nil,
Out: out,
Context: context.New(&context.ClientContext{
Namespace: "$unittest",
}, &context.ServerContext{
StartAt: time.Now(),
}),
}
Info(ctx)
// t.Log(out.String())
if strings.Index(out.String(), "ERR") == 0 {
t.Fail()
}
}
func TestMonitor(t *testing.T) {
assert := assert.New(t)
cli := &context.ClientContext{
Namespace: "$unittest",
ID: 1,
RemoteAddr: "127.0.0.1",
DB: &db.DB{Namespace: "$unittest", ID: 0},
}
serv := &context.ServerContext{}
out := bytes.NewBuffer(nil)
ctx := &Context{
Name: "monitor",
Args: nil,
In: nil,
Out: out,
Context: context.New(cli, serv),
}
Monitor(ctx)
assert.Equal("+OK\r\n", out.String())
out.Reset()
ctx = &Context{
Name: "ping",
Args: nil,
In: nil,
Out: ioutil.Discard,
Context: ctx.Context,
}
feedMonitors(ctx)
assert.Contains(out.String(), "[0 127.0.0.1] ping \r\n")
}
func TestClient_List(t *testing.T) {
assert := assert.New(t)
now := time.Now()
cli := &context.ClientContext{
Namespace: "$unittest",
ID: 1,
RemoteAddr: "127.0.0.1",
DB: &db.DB{Namespace: "$unittest", ID: 0},
Created: now,
Updated: now,
}
serv := &context.ServerContext{}
serv.Clients.Store(cli.RemoteAddr, cli)
out := bytes.NewBuffer(nil)
ctx := &Context{
Name: "client",
Args: []string{"list"},
In: nil,
Out: out,
Context: context.New(cli, serv),
}
Client(ctx)
assert.Contains(out.String(), "id=1 addr=127.0.0.1")
}