This repository was archived by the owner on Sep 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient_test.go
More file actions
305 lines (260 loc) · 7.04 KB
/
client_test.go
File metadata and controls
305 lines (260 loc) · 7.04 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"fmt"
"os"
"sync"
"testing"
"time"
mg "github.com/hypermodeinc/modusgraph"
"github.com/stretchr/testify/require"
)
func TestClientPool(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "ClientPoolWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "ClientPoolWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skip("Skipping test as MODUSGRAPH_TEST_ADDR is not set")
}
// Create a client with pool size 10
client, err := mg.NewClient(tc.uri, mg.WithPoolSize(10))
require.NoError(t, err)
defer client.Close()
// Test concurrent client pool usage
const numWorkers = 20
var wg sync.WaitGroup
var mu sync.Mutex
var clientCount int
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// Get a client from the pool
client, cleanup, err := client.DgraphClient()
require.NoError(t, err)
require.NotNil(t, client)
txn := client.NewReadOnlyTxn()
ctx := context.Background()
_, err = txn.Query(ctx, "query { q(func: uid(1)) { uid } }")
require.NoError(t, err)
err = txn.Discard(ctx)
require.NoError(t, err)
// Verify we got a valid Dgraph client
if client != nil {
mu.Lock()
clientCount++
mu.Unlock()
}
// Clean up the client
cleanup()
}()
}
// Wait for all workers to complete
wg.Wait()
// Verify we got clients from the pool
require.GreaterOrEqual(t, clientCount, 1)
// Get a client before close
beforeClient, cleanupBefore, err := client.DgraphClient()
require.NoError(t, err)
require.NotNil(t, beforeClient)
// Close the client pool
client.Close()
time.Sleep(100 * time.Millisecond) // Give some time for cleanup
// Verify we can still get a new client after close (pool will create a new one)
afterClient, cleanupAfter, err := client.DgraphClient()
require.NoError(t, err)
require.NotNil(t, afterClient)
// Verify the client is actually new
require.NotEqual(t, fmt.Sprintf("%p", beforeClient), fmt.Sprintf("%p", afterClient))
// Clean up the client
cleanupAfter()
// Also clean up the before client if it wasn't already closed
cleanupBefore()
})
}
// Shutdown at the end of the test to ensure the next test can start fresh
mg.Shutdown()
}
func TestClientPoolStress(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "ClientPoolStressWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "ClientPoolStressWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skip("Skipping test as MODUSGRAPH_TEST_ADDR is not set")
}
// Create a client with pool size 10
client, err := mg.NewClient(tc.uri, mg.WithPoolSize(10))
require.NoError(t, err)
defer func() {
client.Close()
}()
// Test concurrent client pool usage with high load
const numWorkers = 20
const iterations = 10
var wg sync.WaitGroup
var successCount int
var errorCount int
var mu sync.Mutex
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < iterations; j++ {
dgraphClient, cleanup, err := client.DgraphClient()
if err != nil {
mu.Lock()
errorCount++
mu.Unlock()
continue
}
if dgraphClient != nil {
// Test the client works
txn := dgraphClient.NewReadOnlyTxn()
ctx := context.Background()
_, err = txn.Query(ctx, "query { q(func: uid(1)) { uid } }")
if err != nil {
err = txn.Discard(ctx)
if err != nil {
mu.Lock()
errorCount++
mu.Unlock()
}
cleanup()
continue
}
err = txn.Discard(ctx)
if err != nil {
mu.Lock()
errorCount++
mu.Unlock()
cleanup()
continue
}
mu.Lock()
successCount++
mu.Unlock()
}
// Clean up the client
cleanup()
}
}()
}
wg.Wait()
require.Greater(t, successCount, 0)
})
mg.Shutdown()
}
}
func TestClientPoolMisuse(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "ClientPoolMisuseWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "ClientPoolMisuseWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skip("Skipping test as MODUSGRAPH_TEST_ADDR is not set")
}
// Create a client with pool size 10
client, err := mg.NewClient(tc.uri, mg.WithPoolSize(10))
require.NoError(t, err)
client.Close()
client.Close()
})
}
// Shutdown at the end of the test to ensure the next test can start fresh
mg.Shutdown()
}
func TestLocalClientSingleton(t *testing.T) {
path := GetTempDir(t)
client, err := mg.NewClient("file://" + path)
require.NoError(t, err)
defer client.Close()
client2, err := mg.NewClient("file://" + path)
require.NoError(t, err)
require.Equal(t, client, client2, "Expected the same client instance")
defer client2.Close()
_, err = mg.NewClient("file://"+path, mg.WithAutoSchema(true)) // will create a new client "key"
require.Error(t, err, "Expected an error when creating a new client with different options")
require.ErrorIs(t, err, mg.ErrSingletonOnly)
}
func TestRemoteClientAccess(t *testing.T) {
if os.Getenv("MODUSGRAPH_TEST_ADDR") == "" {
t.Skip("Skipping test as MODUSGRAPH_TEST_ADDR is not set")
}
client, err := mg.NewClient("dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"))
require.NoError(t, err)
defer client.Close()
client2, err := mg.NewClient("dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"))
require.NoError(t, err)
require.Equal(t, client, client2, "Expected the same client instance")
defer client2.Close()
client3, err := mg.NewClient("dgraph://"+os.Getenv("MODUSGRAPH_TEST_ADDR"), mg.WithAutoSchema(true))
require.NoError(t, err)
require.NotEqual(t, client, client3, "Expected a different client instance")
defer client3.Close()
query := "query { q(func: uid(1)) { uid } }"
ctx := context.Background()
// run the three clients in go routines to test concurrency
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
_, err := client.QueryRaw(ctx, query, nil)
require.NoError(t, err)
}()
go func() {
defer wg.Done()
_, err := client2.QueryRaw(ctx, query, nil)
require.NoError(t, err)
}()
go func() {
defer wg.Done()
_, err := client3.QueryRaw(ctx, query, nil)
require.NoError(t, err)
}()
wg.Wait()
}