11package machsvr_test
22
33import (
4+ "context"
45 "os"
6+ "runtime"
7+ "strings"
58 "testing"
69
710 "github.com/machbase/neo-client/api"
@@ -31,6 +34,11 @@ func TestAll(t *testing.T) {
3134 testsuite .CreateTestTables (machsvrDB )
3235 testsuite .TestAll (t , machsvrDB ,
3336 tcSetMaxConn ,
37+ tcSetMaxQuery ,
38+ tcDatabaseError ,
39+ tcWatcherRegistry ,
40+ tcKillConnection ,
41+ tcCancelConnection ,
3442 )
3543 testsuite .DropTestTables (machsvrDB )
3644}
@@ -41,9 +49,170 @@ func tcSetMaxConn(t *testing.T) {
4149 require .NotZero (t , expectLimit )
4250 require .LessOrEqual (t , - 1 , open )
4351
52+ engine .SetMaxOpenConn (- 1 )
53+ limit , open := engine .MaxOpenConn ()
54+ require .Equal (t , - 1 , limit )
55+ require .Equal (t , - 1 , open )
56+
57+ engine .SetMaxOpenConn (0 )
58+ limit , open = engine .MaxOpenConn ()
59+ require .Equal (t , runtime .NumCPU ()* 2 , limit )
60+ require .Equal (t , limit , open )
61+
4462 expectLimit = 1000
4563 engine .SetMaxOpenConn (expectLimit )
46- limit , open : = engine .MaxOpenConn ()
64+ limit , open = engine .MaxOpenConn ()
4765 require .Equal (t , expectLimit , limit )
66+ require .Equal (t , expectLimit , open )
67+ }
68+
69+ func tcSetMaxQuery (t * testing.T ) {
70+ engine := machsvrDB .(* machsvr.Database )
71+ expectLimit , open := engine .MaxOpenQuery ()
72+ require .NotZero (t , expectLimit )
4873 require .LessOrEqual (t , - 1 , open )
74+
75+ engine .SetMaxOpenQuery (- 1 )
76+ limit , open := engine .MaxOpenQuery ()
77+ require .Equal (t , - 1 , limit )
78+ require .Equal (t , - 1 , open )
79+
80+ engine .SetMaxOpenQuery (0 )
81+ limit , open = engine .MaxOpenQuery ()
82+ require .Equal (t , runtime .NumCPU ()* 2 , limit )
83+ require .Equal (t , limit , open )
84+
85+ expectLimit = 1000
86+ engine .SetMaxOpenQuery (expectLimit )
87+ limit , open = engine .MaxOpenQuery ()
88+ require .Equal (t , expectLimit , limit )
89+ require .Equal (t , expectLimit , open )
90+ }
91+
92+ func tcDatabaseError (t * testing.T ) {
93+ engine := machsvrDB .(* machsvr.Database )
94+ _ , err := machsvrDB .Connect (context .Background (), api .WithPassword ("sys" , "wrong-password" ))
95+ require .Error (t , err )
96+
97+ lastErr := engine .Error ()
98+ require .Error (t , lastErr )
99+ require .True (t , strings .Contains (lastErr .Error (), "Invalid username/password" ) || strings .Contains (lastErr .Error (), "invalid username/password" ))
100+ }
101+
102+ func tcWatcherRegistry (t * testing.T ) {
103+ engine := machsvrDB .(* machsvr.Database )
104+ watcherKey := "registry-test"
105+ engine .RemoveWatcher (watcherKey )
106+ t .Cleanup (func () {
107+ engine .RemoveWatcher (watcherKey )
108+ })
109+
110+ engine .RegisterWatcher (watcherKey , nil )
111+ state , ok := engine .GetWatcher (watcherKey )
112+ require .True (t , ok )
113+ require .NotNil (t , state )
114+ require .False (t , state .CreatedTime .IsZero ())
115+ require .Empty (t , state .Id )
116+ require .Empty (t , state .LatestSql )
117+ require .True (t , state .LatestTime .IsZero ())
118+ expectCreatedTime := state .CreatedTime
119+
120+ engine .ListWatcher (nil )
121+
122+ found := false
123+ engine .ListWatcher (func (state * machsvr.ConnState ) bool {
124+ if state .Id == "" && state .CreatedTime .Equal (expectCreatedTime ) {
125+ found = true
126+ return false
127+ }
128+ return true
129+ })
130+ require .True (t , found )
131+
132+ engine .RemoveWatcher (watcherKey )
133+ _ , ok = engine .GetWatcher (watcherKey )
134+ require .False (t , ok )
135+ }
136+
137+ func tcKillConnection (t * testing.T ) {
138+ engine := machsvrDB .(* machsvr.Database )
139+ require .EqualError (t , engine .KillConnection ("missing-watcher" , true ), "connection 'missing-watcher' not found" )
140+
141+ invalidKey := "invalid-watcher"
142+ engine .SetWatcher (invalidKey , & machsvr.ConnWatcher {})
143+ t .Cleanup (func () {
144+ engine .RemoveWatcher (invalidKey )
145+ })
146+ require .EqualError (t , engine .KillConnection (invalidKey , true ), "invalid connection 'invalid-watcher'" )
147+ engine .RemoveWatcher (invalidKey )
148+
149+ before := watcherStates (engine )
150+ conn , err := machsvrDB .Connect (context .Background (), api .WithPassword ("sys" , "manager" ))
151+ require .NoError (t , err )
152+
153+ after := watcherStates (engine )
154+ watcherID := newWatcherID (before , after )
155+ require .NotEmpty (t , watcherID )
156+
157+ require .NoError (t , engine .KillConnection (watcherID , true ))
158+ require .EqualError (t , engine .KillConnection (watcherID , true ), "connection '" + watcherID + "' not found" )
159+ require .NoError (t , conn .Close ())
160+ }
161+
162+ func tcCancelConnection (t * testing.T ) {
163+ engine := machsvrDB .(* machsvr.Database )
164+
165+ before := watcherStates (engine )
166+ conn , err := machsvrDB .Connect (context .Background (), api .WithPassword ("sys" , "manager" ))
167+ require .NoError (t , err )
168+
169+ after := watcherStates (engine )
170+ watcherID := newWatcherID (before , after )
171+ require .NotEmpty (t , watcherID )
172+
173+ require .NoError (t , engine .KillConnection (watcherID , false ))
174+ require .EqualError (t , engine .KillConnection (watcherID , false ), "connection '" + watcherID + "' not found" )
175+ require .NoError (t , conn .Close ())
176+
177+ before = watcherStates (engine )
178+ conn , err = machsvrDB .Connect (context .Background (), api .WithPassword ("sys" , "manager" ))
179+ require .NoError (t , err )
180+
181+ machConn , ok := conn .(* machsvr.Conn )
182+ require .True (t , ok )
183+
184+ after = watcherStates (engine )
185+ watcherID = newWatcherID (before , after )
186+ require .NotEmpty (t , watcherID )
187+
188+ require .NoError (t , machConn .Cancel ())
189+ require .EqualError (t , engine .KillConnection (watcherID , false ), "connection '" + watcherID + "' not found" )
190+ require .NoError (t , conn .Close ())
191+ }
192+
193+ func watcherStates (engine * machsvr.Database ) []* machsvr.ConnState {
194+ states := []* machsvr.ConnState {}
195+ engine .ListWatcher (func (state * machsvr.ConnState ) bool {
196+ states = append (states , state )
197+ return true
198+ })
199+ return states
200+ }
201+
202+ func newWatcherID (before []* machsvr.ConnState , after []* machsvr.ConnState ) string {
203+ known := map [string ]struct {}{}
204+ for _ , state := range before {
205+ if state != nil && state .Id != "" {
206+ known [state .Id ] = struct {}{}
207+ }
208+ }
209+ for _ , state := range after {
210+ if state == nil || state .Id == "" {
211+ continue
212+ }
213+ if _ , ok := known [state .Id ]; ! ok {
214+ return state .Id
215+ }
216+ }
217+ return ""
49218}
0 commit comments