-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathsession_test.go
165 lines (148 loc) · 3.3 KB
/
session_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
//go:build integration
// +build integration
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
package nebula_go
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSession_Execute(t *testing.T) {
config := GetDefaultConf()
host := HostAddress{address, port}
pool, err := NewConnectionPool([]HostAddress{host}, config, DefaultLogger{})
if err != nil {
t.Fatal(err)
}
errCh := make(chan error, 1)
f := func(s *Session) {
time.Sleep(10 * time.Microsecond)
reps, err := s.Execute("yield 1")
if err != nil {
errCh <- err
}
if !reps.IsSucceed() {
t.Fatal(reps.resp.ErrorMsg)
}
// test Ping()
err = s.Ping()
if err != nil {
errCh <- err
}
}
ctx, cancel := context.WithTimeout(context.TODO(), 300*time.Millisecond)
defer cancel()
go func(ctx context.Context) {
sess, err := pool.GetSession("root", "nebula")
if err != nil {
errCh <- err
}
for {
select {
case <-ctx.Done():
break
default:
f(sess)
}
}
}(ctx)
go func(ctx context.Context) {
sess, err := pool.GetSession("root", "nebula")
if err != nil {
errCh <- err
}
for {
select {
case <-ctx.Done():
default:
f(sess)
}
}
}(ctx)
for {
select {
case err := <-errCh:
t.Fatal(err)
case <-ctx.Done():
return
}
}
}
func TestSession_Recover(t *testing.T) {
query := "show hosts"
config := GetDefaultConf()
host := HostAddress{address, port}
pool, err := NewConnectionPool([]HostAddress{host}, config, DefaultLogger{})
if err != nil {
t.Fatal(err)
}
sess, err := pool.GetSession("root", "nebula")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, pool.getActiveConnCount()+pool.getIdleConnCount())
go func() {
for {
_, _ = sess.Execute(query)
}
}()
stopContainer(t, "nebula-docker-compose_graphd0_1")
stopContainer(t, "nebula-docker-compose_graphd1_1")
stopContainer(t, "nebula-docker-compose_graphd2_1")
defer func() {
startContainer(t, "nebula-docker-compose_graphd1_1")
startContainer(t, "nebula-docker-compose_graphd2_1")
}()
<-time.After(3 * time.Second)
startContainer(t, "nebula-docker-compose_graphd0_1")
<-time.After(3 * time.Second)
_, err = sess.Execute(query)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, pool.getActiveConnCount()+pool.getIdleConnCount())
}
func TestSession_CreateSpace_ShowSpaces(t *testing.T) {
config := GetDefaultConf()
host := HostAddress{address, port}
pool, err := NewConnectionPool([]HostAddress{host}, config, DefaultLogger{})
if err != nil {
t.Fatal(err)
}
sess, err := pool.GetSession("root", "nebula")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, pool.getActiveConnCount()+pool.getIdleConnCount())
newSpaceName := "new_created_space"
conf := SpaceConf{
Name: newSpaceName,
Partition: 1,
Replica: 1,
VidType: "FIXED_STRING(12)",
}
_, err = sess.CreateSpace(conf)
if err != nil {
t.Fatal(err)
}
conf.IgnoreIfExists = true
// Create again should work
_, err = sess.CreateSpace(conf)
if err != nil {
t.Fatal(err)
}
spaceNames, err := sess.ShowSpaces()
if err != nil {
t.Fatal(err)
}
var names []string
for _, space := range spaceNames {
names = append(names, space.Name)
}
assert.LessOrEqual(t, 1, len(names))
assert.Contains(t, names, newSpaceName)
}