Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up tests by reducing sleep/wait loop #6198

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions db/blip_connected_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (bh *blipHandler) handleFunction(rq *blip.Message) error {
}

bh.logEndpointEntry(rq.Profile(), fmt.Sprintf("name: %s", name))
return WithTimeout(bh.loggingCtx, UserFunctionTimeout, func(ctx context.Context) error {
return WithTimeout(bh.loggingCtx, bh.db.UserFunctionTimeout, func(ctx context.Context) error {
// Call the function:
fn, err := bh.db.GetUserFunction(name, requestParams, true, ctx)
if err != nil {
Expand Down Expand Up @@ -169,7 +169,7 @@ func (bh *blipHandler) handleGraphQL(rq *blip.Message) error {
}

bh.logEndpointEntry(rq.Profile(), fmt.Sprintf("query: %s", query))
return WithTimeout(bh.loggingCtx, UserFunctionTimeout, func(ctx context.Context) error {
return WithTimeout(bh.loggingCtx, bh.db.UserFunctionTimeout, func(ctx context.Context) error {
result, err := bh.db.UserGraphQLQuery(query, operationName, variables, true, ctx)
if err != nil {
return err
Expand Down
22 changes: 12 additions & 10 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type DatabaseContext struct {
NoX509HTTPClient *http.Client // A HTTP Client from gocb to use the management endpoints
ServerContextHasStarted chan struct{} // Closed via PostStartup once the server has fully started
userFunctions *UserFunctions // client-callable JavaScript functions
UserFunctionTimeout time.Duration // Default timeout for N1QL, JavaScript and GraphQL queries. (Applies to REST and BLIP requests.)
graphQL *GraphQL // GraphQL query evaluator
Scopes map[string]Scope // A map keyed by scope name containing a set of scopes/collections. Nil if running with only _default._default
CollectionByID map[uint32]*DatabaseCollection // A map keyed by collection ID to Collection
Expand Down Expand Up @@ -408,16 +409,17 @@ func NewDatabaseContext(ctx context.Context, dbName string, bucket base.Bucket,
return nil, err
}
dbContext := &DatabaseContext{
Name: dbName,
UUID: cbgt.NewUUID(),
MetadataStore: metadataStore,
Bucket: bucket,
StartTime: time.Now(),
autoImport: autoImport,
Options: options,
DbStats: dbStats,
CollectionByID: make(map[uint32]*DatabaseCollection),
ServerUUID: serverUUID,
Name: dbName,
UUID: cbgt.NewUUID(),
MetadataStore: metadataStore,
Bucket: bucket,
StartTime: time.Now(),
autoImport: autoImport,
Options: options,
DbStats: dbStats,
CollectionByID: make(map[uint32]*DatabaseCollection),
ServerUUID: serverUUID,
UserFunctionTimeout: defaultUserFunctionTimeout,
}

// Initialize metadata ID and keys
Expand Down
3 changes: 1 addition & 2 deletions db/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
/* This is the interface to the functions and GraphQL APIs implemented in the functions package. */

// Timeout for N1QL, JavaScript and GraphQL queries. (Applies to REST and BLIP requests.)
const UserFunctionTimeout = 60 * time.Second
const defaultUserFunctionTimeout = 60 * time.Second

//////// USER FUNCTIONS

Expand Down Expand Up @@ -144,7 +144,6 @@ func EstimateSizeOfJSON(args any) int {
}
return size
default:
//log.Printf("*** sizeOfArgs doesn't handle %T", arg)
return 1
}
}
Expand Down
4 changes: 2 additions & 2 deletions rest/functions_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (h *handler) handleFunctionCall() error {
}
canMutate := h.rq.Method != "GET"

return db.WithTimeout(h.ctx(), db.UserFunctionTimeout, func(ctx context.Context) error {
return db.WithTimeout(h.ctx(), h.db.UserFunctionTimeout, func(ctx context.Context) error {
fn, err := h.db.GetUserFunction(fnName, fnParams, canMutate, ctx)
if err != nil {
return err
Expand Down Expand Up @@ -224,7 +224,7 @@ func (h *handler) handleGraphQL() error {
return base.HTTPErrorf(http.StatusBadRequest, "Missing/empty `query` property")
}

return db.WithTimeout(h.ctx(), db.UserFunctionTimeout, func(ctx context.Context) error {
return db.WithTimeout(h.ctx(), h.db.UserFunctionTimeout, func(ctx context.Context) error {
result, err := h.db.UserGraphQLQuery(queryString, operationName, variables, canMutate, ctx)
if err == nil {
h.writeJSON(result)
Expand Down
10 changes: 6 additions & 4 deletions rest/functionsapitest/graphql_queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/graphql-go/graphql"

"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db"
"github.com/couchbase/sync_gateway/db/functions"
"github.com/couchbase/sync_gateway/rest"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -284,18 +284,20 @@ func TestContextDeadline(t *testing.T) {
return
}
defer rt.Close()
timeout := 500 * time.Millisecond
rt.GetDatabase().UserFunctionTimeout = timeout
t.Run("AsAdmin - exceedContextDeadline", func(t *testing.T) {
requestQuery := fmt.Sprintf(`{"query": "query{ checkContextDeadline(Timeout:%d) }"}`, db.UserFunctionTimeout.Milliseconds()*2)
requestQuery := fmt.Sprintf(`{"query": "query{ checkContextDeadline(Timeout:%d) }"}`, timeout.Milliseconds()*2)
response := rt.SendAdminRequest("POST", "/db/_graphql", requestQuery)

assert.Equal(t, 200, response.Result().StatusCode)
testErrorMessage(t, response, "context deadline exceeded")
})
t.Run("AsAdmin - doNotExceedContextDeadline", func(t *testing.T) {
requestQuery := fmt.Sprintf(`{"query": "query{ checkContextDeadline(Timeout:%d) }"}`, db.UserFunctionTimeout.Milliseconds()/2)
requestQuery := `{"query": "query{ checkContextDeadline(Timeout:1) }"}`
response := rt.SendAdminRequest("POST", "/db/_graphql", requestQuery)

assert.Equal(t, 200, response.Result().StatusCode)

assert.Equal(t, `{"data":{"checkContextDeadline":0}}`, string(response.BodyBytes()))
})
}
Expand Down
10 changes: 5 additions & 5 deletions rest/functionsapitest/user_functions_queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
"net/http"
"strings"
"testing"

"github.com/couchbase/sync_gateway/db"
"time"

"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db/functions"
Expand Down Expand Up @@ -454,17 +453,18 @@ func TestFunctionTimeout(t *testing.T) {
return
}
defer rt.Close()

timeout := 500 * time.Millisecond
rt.GetDatabase().UserFunctionTimeout = timeout
// positive case:
reqBody := fmt.Sprintf(`{"ms": %d}`, db.UserFunctionTimeout.Milliseconds()/2)
t.Run("under time limit", func(t *testing.T) {
reqBody := `{"ms": 1}`
response := rt.SendAdminRequest("POST", "/db/_function/sleep", reqBody)
assert.Equal(t, 200, response.Result().StatusCode)
})

// negative case:
reqBody = fmt.Sprintf(`{"ms": %d}`, 2*db.UserFunctionTimeout.Milliseconds())
t.Run("over time limit", func(t *testing.T) {
reqBody := fmt.Sprintf(`{"ms": %d}`, 2*timeout)
response := rt.SendAdminRequest("POST", "/db/_function/sleep", reqBody)
assert.Equal(t, 500, response.Result().StatusCode)
})
Expand Down