Skip to content

Commit 56ad539

Browse files
authored
Merge pull request #119 from arangodb/bug-fix/stream-cursor-test
VST stream cursor test fixes & VST fail-quick fix
2 parents 7e5ec05 + b8dc762 commit 56ad539

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ __test_go_test:
298298
-e TEST_CONNECTION=$(TEST_CONNECTION) \
299299
-e TEST_CVERSION=$(TEST_CVERSION) \
300300
-e TEST_CONTENT_TYPE=$(TEST_CONTENT_TYPE) \
301+
-e TEST_PPROF=$(TEST_PPROF) \
301302
-w /usr/code/ \
302303
golang:$(GOVERSION) \
303304
go test $(TAGS) $(TESTOPTIONS) $(TESTVERBOSEOPTIONS) $(TESTS)

test/client_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package test
2525
import (
2626
"context"
2727
"crypto/tls"
28+
"log"
2829
httplib "net/http"
2930
"os"
3031
"strconv"
@@ -33,14 +34,17 @@ import (
3334
"testing"
3435
"time"
3536

37+
_ "net/http/pprof"
38+
3639
driver "github.com/arangodb/go-driver"
3740
"github.com/arangodb/go-driver/http"
3841
"github.com/arangodb/go-driver/vst"
3942
"github.com/arangodb/go-driver/vst/protocol"
4043
)
4144

4245
var (
43-
logEndpointsOnce sync.Once
46+
logEndpointsOnce sync.Once
47+
runPProfServerOnce sync.Once
4448
)
4549

4650
// skipBelowVersion skips the test if the current server version is less than
@@ -152,6 +156,16 @@ func createConnectionFromEnv(t testEnv) driver.Connection {
152156

153157
// createClientFromEnv initializes a Client from information specified in environment variables.
154158
func createClientFromEnv(t testEnv, waitUntilReady bool, connection ...*driver.Connection) driver.Client {
159+
runPProfServerOnce.Do(func() {
160+
if os.Getenv("TEST_PPROF") != "" {
161+
go func() {
162+
// Start pprof server on port 6060
163+
// To use it in the test, run a command like:
164+
// docker exec -it go-driver-test sh -c "apk add -U curl && curl http://localhost:6060/debug/pprof/goroutine?debug=1"
165+
log.Println(httplib.ListenAndServe("localhost:6060", nil))
166+
}()
167+
}
168+
})
155169
conn := createConnectionFromEnv(t)
156170
if len(connection) == 1 {
157171
*connection[0] = conn

test/cursor_test.go

+10-15
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ func TestCreateStreamCursor(t *testing.T) {
261261
t.Fatalf("Expected success, got %s", describe(err))
262262
}
263263
}
264+
t.Log("Completed inserting 10k docs")
264265

265266
const expectedResults int = 10 * 10000
266267
query := "FOR doc IN cursor_stream_test RETURN doc"
@@ -270,14 +271,10 @@ func TestCreateStreamCursor(t *testing.T) {
270271
// create a bunch of read-only cursors
271272
for i := 0; i < 10; i++ {
272273
cursor, err := db.Query(ctx2, query, nil)
273-
if err == nil {
274-
// Close upon exit of the function
275-
defer cursor.Close()
276-
}
277274
if err != nil {
278-
t.Errorf("Expected success in query %d (%s), got '%s'", i, query, describe(err))
279-
continue
275+
t.Fatalf("Expected success in query %d (%s), got '%s'", i, query, describe(err))
280276
}
277+
defer cursor.Close()
281278
count := cursor.Count()
282279
if count != 0 {
283280
t.Errorf("Expected count of 0, got %d in query %d (%s)", count, i, query)
@@ -294,25 +291,24 @@ func TestCreateStreamCursor(t *testing.T) {
294291
cursors = append(cursors, cursor)
295292
}
296293

294+
t.Logf("Created %d cursors", len(cursors))
295+
297296
// start a write query on the same collection inbetween
298297
// contrary to normal cursors which are executed right
299298
// away this will block until all read cursors are resolved
300299
testReady := make(chan bool)
301300
go func() {
302301
query = "FOR doc IN 1..5 LET y = SLEEP(0.01) INSERT {name:'Peter', age:0} INTO cursor_stream_test"
303302
cursor, err := db.Query(ctx2, query, nil) // should not return immediately
304-
if err == nil {
305-
// Close upon exit of the function
306-
defer cursor.Close()
307-
}
308303
if err != nil {
309-
t.Errorf("Expected success in write-query %s, got '%s'", query, describe(err))
304+
t.Fatalf("Expected success in write-query %s, got '%s'", query, describe(err))
310305
}
306+
defer cursor.Close()
311307

312308
for cursor.HasMore() {
313309
var data interface{}
314310
if _, err := cursor.ReadDocument(ctx2, &data); err != nil {
315-
t.Errorf("Failed to read document, err: %s", describe(err))
311+
t.Fatalf("Failed to read document, err: %s", describe(err))
316312
}
317313
}
318314
testReady <- true // signal write done
@@ -325,7 +321,7 @@ func TestCreateStreamCursor(t *testing.T) {
325321
for cursor.HasMore() {
326322
var user UserDoc
327323
if _, err := cursor.ReadDocument(ctx2, &user); err != nil {
328-
t.Errorf("Failed to result document %d: %s", i, describe(err))
324+
t.Fatalf("Failed to result document %d: %s", i, describe(err))
329325
}
330326
readCount++
331327
}
@@ -335,10 +331,9 @@ func TestCreateStreamCursor(t *testing.T) {
335331

336332
writeDone := false
337333
readDone := false
338-
deadline := time.Now().Add(time.Second * 30)
339334
for {
340335
select {
341-
case <-time.After(time.Until(deadline)):
336+
case <-ctx.Done():
342337
t.Fatal("Timeout")
343338
case v := <-testReady:
344339
if v {

vst/connection.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ func (c *vstConnection) Do(ctx context.Context, req driver.Request) (driver.Resp
142142

143143
// Do performs a given request, returning its response.
144144
func (c *vstConnection) do(ctx context.Context, req driver.Request, transport messageTransport) (driver.Response, error) {
145+
if ctx == nil {
146+
ctx = context.Background()
147+
}
145148
vstReq, ok := req.(*vstRequest)
146149
if !ok {
147150
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "request is not a *vstRequest"})
@@ -158,10 +161,16 @@ func (c *vstConnection) do(ctx context.Context, req driver.Request, transport me
158161
vstReq.WroteRequest()
159162

160163
// Wait for response
161-
msg, ok := <-resp
162-
if !ok {
163-
// Message was cancelled / timeout
164-
return nil, driver.WithStack(context.DeadlineExceeded)
164+
var msg protocol.Message
165+
select {
166+
case msg, ok = <-resp:
167+
if !ok {
168+
// Message was canceled / timeout
169+
return nil, driver.WithStack(context.DeadlineExceeded)
170+
}
171+
case <-ctx.Done():
172+
// Context canceled while waiting here
173+
return nil, driver.WithStack(ctx.Err())
165174
}
166175

167176
//fmt.Printf("Received msg: %d\n", msg.ID)

0 commit comments

Comments
 (0)