-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeek_test.go
245 lines (232 loc) · 5.44 KB
/
geek_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package geek
import (
"log"
"os"
"os/exec"
"os/user"
"strconv"
"syscall"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
var qClientPort = 9999
var qProcessPort = 9998
var qEnginePort = 9997
func TestMain(m *testing.M) {
log.Println("Starting q processes")
var qClient = exec.Command("q", "-p", strconv.Itoa(qClientPort), "-q")
var qProcess = exec.Command("q", "-p", strconv.Itoa(qProcessPort), "-q")
qClient.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGTERM,
}
qProcess.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGTERM,
}
stdin, err := qProcess.StdinPipe()
if err != nil {
log.Fatal("Failed to connect to stdin:", err)
}
stdout, err := qProcess.StdoutPipe()
if err != nil {
log.Fatal("Failed to connect to stdout:", err)
}
stderr, err := qProcess.StderrPipe()
if err != nil {
log.Fatal("Failed to connect to stderr:", err)
}
err = qProcess.Start()
if err != nil {
log.Fatal("Failed to start q client:", err)
}
err = qClient.Start()
if err != nil {
log.Fatal("Failed to start q server:", err)
}
go func() {
buf := make([]byte, 512)
for {
n, err := stderr.Read(buf)
log.Printf("q stderr: %s", string(buf[:n]))
if err != nil {
log.Printf("Failed to read q stderr: %s", err)
return
}
}
}()
go func() {
buf := make([]byte, 512)
for {
n, err := stdout.Read(buf)
log.Printf("q stdout: %s", string(buf[:n]))
if err != nil {
log.Printf("Failed to read q stdout: %s", err)
return
}
}
}()
// confirm process started
stdin.Write([]byte("0\n"))
stdout.Read([]byte{0, 0})
// change user password for test authentication
stdin.Write([]byte(".z.pw:{and[x~`test;y~\"test\"]}\n"))
exitVal := m.Run()
os.Exit(exitVal)
}
func TestDial(t *testing.T) {
q := QProcess{Port: qProcessPort, User: "test", Password: "test"}
err := q.Dial()
if err != nil {
t.Errorf("Fail to connect to q process: %s", err)
}
q.Close()
q = QProcess{Port: qProcessPort, User: "test", Password: "wrong"}
err = q.Dial()
if err == nil {
t.Errorf("Should fail because of wrong credential")
}
}
func TestServer(t *testing.T) {
// hdb, rdb, rte etc.
qProcess := QProcess{Port: qProcessPort, User: "test", Password: "test"}
err := qProcess.Dial()
if err != nil {
t.Errorf("Failed to connect q process %v", err)
return
}
pool := ConnPool{
RetryTimes: 2,
ReviveInterval: 2 * time.Second,
Timeout: time.Minute,
}
pool.Put(&qProcess)
err = pool.Serving()
if err != nil {
t.Error(err)
return
}
qEngine := Engine{
Port: qEnginePort,
Auth: func(u, p string) error { return nil },
Pool: &pool,
}
qClient := QProcess{Port: qClientPort, User: "", Password: ""}
err = qClient.Dial()
if err != nil {
t.Errorf("Failed to connect q client %v", err)
return
}
done := make(chan bool)
defer close(done)
go func() {
log.Println("Start q Engine")
go qEngine.Run()
done <- true
}()
time.AfterFunc(1*time.Second, func() {
log.Printf("Query via q engine @%d", qEnginePort)
var k string
err := qClient.Sync(&k, []byte("`::9997 (`.Q.dd;`7203;`T)"))
if err != nil {
t.Error(err)
done <- true
return
}
if diff := cmp.Diff("7203.T", k); diff != "" {
t.Error(diff)
}
var geekUser string
qProcess.Sync(&geekUser, GeekUser)
osUser, _ := user.Current()
if diff := cmp.Diff(geekUser, osUser.Username); diff != "" {
t.Error("User is not matached")
t.Error(diff)
}
done <- true
})
// wait for goroutine complete
<-done
<-done
// sync will fail, q process -> dead conns -> revive
qProcess.Close()
var k string
err = pool.Sync(&k, []string{".Q.dd", "9984", "T"})
if err != nil {
t.Error(err)
}
if diff := cmp.Diff("9984.T", k); diff != "" {
t.Error(diff)
}
pool.AllowedAPI = make(map[string]bool)
err = qClient.Sync(&k, []byte("`::9997 (`.Q.dd;`2229;`T)"))
if err.Error() != "`"+ErrNotAllowedAPI.Error() {
t.Error(err)
}
pool.AllowedAPI[".Q.dd"] = true
err = qClient.Sync(&k, []byte("`::9997 (`.Q.dd;`2229;`T)"))
if err != nil {
t.Error(err)
}
if diff := cmp.Diff("2229.T", k); diff != "" {
t.Error(diff)
}
pool.RetryTimes = 1
qProcess.Close()
err = pool.Sync(&k, []string{".Q.dd", "9984", "T"})
if err != ErrMaxRetryTimesReached {
t.Error(err)
}
qProcess.Dial()
var h1, h2 int64
pool.reservedConns[qProcessPort].Sync(&h1, []byte("{`long$.z.w}()"))
pool.Conns[qProcessPort].Sync(&h2, []byte("{`long$.z.w}()"))
if h1 == 0 && h2 == 0 && h1 == h2 {
t.Error("Should be different handles")
}
pool.Reload()
qClient.Close()
}
func TestIPC(t *testing.T) {
q := QProcess{Port: qProcessPort, User: "test", Password: "test"}
err := q.Dial()
if err != nil {
t.Errorf("Fail to connect to q process: %s", err)
}
args := struct {
F []byte
P1 string
P2 int64
}{
[]byte("set"), "a", 18,
}
err = q.Async(args)
if err != nil {
t.Errorf("Fail to send async msg to q process: %s", err)
}
var a int64
err = q.Sync(&a, "a")
if err != nil {
t.Errorf("Fail to send sync msg to q process: %s", err)
}
if a != 18 {
t.Errorf("a should equal to 18")
}
q.Close()
}
func TestDisconnect(t *testing.T) {
q := QProcess{Port: qClientPort, User: "test", Password: "test"}
err := q.Dial()
if err != nil {
t.Errorf("Fail to connect to q client: %s", err)
}
q.Async([]byte("\\\\"))
var time time.Time
err = q.Sync(&time, []byte(".z.P"))
// should be syscall.ECONNRESET error(connection reset by peer)
if err == nil {
t.Errorf("Should fail to send sync msg")
}
if q.IsConnected() {
t.Errorf("Should be disconnected to qClient")
}
}