Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krapie committed Dec 7, 2023
1 parent 75d29cc commit 3329871
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 48 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (c *Client) Watch(
break
}
if err = stream.Err(); err != nil {
return nil, connect.NewError(connect.CodeUnknown, err)
return nil, connect.NewError(connect.CodeUnavailable, err)
}

go func() {
Expand Down
4 changes: 4 additions & 0 deletions server/rpc/connecthelper/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package connecthelper

import (
"context"
"errors"
"fmt"

Expand Down Expand Up @@ -81,6 +82,9 @@ var errorToCode = map[error]connect.Code{
auth.ErrUnexpectedStatusCode: connect.CodeUnauthenticated,
auth.ErrWebhookTimeout: connect.CodeUnauthenticated,
database.ErrMismatchedPassword: connect.CodeUnauthenticated,

// Canceled means the operation was canceled (typically by the caller).
context.Canceled: connect.CodeCanceled,
}

func detailsFromError(err error) (protoiface.MessageV1, bool) {
Expand Down
11 changes: 5 additions & 6 deletions server/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,16 @@ func (s *Server) listenAndServe() error {
func (s *Server) Shutdown(graceful bool) {
s.yorkieServiceCancel()

// TODO(krapie): find graceful way to shutdown http server
if graceful {
err := s.httpServer.Shutdown(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

err := s.httpServer.Shutdown(ctx)
if err != nil {
return
}
} else {
err := s.httpServer.Shutdown(context.Background())
if err != nil {
return
}
// TODO(krapie): find a way to shutdown http server immediately
}
}

Expand Down
4 changes: 2 additions & 2 deletions server/rpc/yorkie_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@ func (s *yorkieServer) WatchDocument(
for {
select {
case <-s.serviceCtx.Done():
return nil
return context.Canceled
case <-ctx.Done():
return nil
return context.Canceled
case event := <-subscription.Events():
eventType, err := converter.ToDocEventType(event.Type)
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions test/integration/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
"sync"
"testing"

"connectrpc.com/connect"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/yorkie-team/yorkie/admin"
"github.com/yorkie-team/yorkie/client"
Expand Down Expand Up @@ -65,7 +64,7 @@ func TestAdmin(t *testing.T) {

// 01. admin tries to remove document that does not exist.
err = adminCli.RemoveDocument(ctx, "default", d1.Key().String(), true)
assert.Equal(t, codes.NotFound, status.Convert(err).Code())
assert.Equal(t, connect.CodeNotFound, connect.CodeOf(err))

// 02. client creates a document then admin removes the document.
assert.NoError(t, cli.Attach(ctx, d1))
Expand Down Expand Up @@ -135,12 +134,12 @@ func TestAdmin(t *testing.T) {

// 01. try to remove document that does not exist.
err = adminCli.RemoveDocument(ctx, "default", doc.Key().String(), false)
assert.Equal(t, codes.NotFound, status.Convert(err).Code())
assert.Equal(t, connect.CodeNotFound, connect.CodeOf(err))

// 02. try to remove document that is attached by the client.
assert.NoError(t, cli.Attach(ctx, doc))
err = adminCli.RemoveDocument(ctx, "default", doc.Key().String(), false)
assert.Equal(t, codes.FailedPrecondition, status.Convert(err).Code())
assert.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))
assert.Equal(t, document.StatusAttached, doc.Status())

// 03. remove document that is detached by the client.
Expand Down
5 changes: 2 additions & 3 deletions test/integration/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
"sync"
"testing"

"connectrpc.com/connect"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/yorkie-team/yorkie/client"
"github.com/yorkie-team/yorkie/pkg/document"
Expand Down Expand Up @@ -57,7 +56,7 @@ func TestServer(t *testing.T) {
assert.Fail(t, "unexpected ctx done")
return
case wr := <-wrch:
if wr.Err == io.EOF || status.Code(wr.Err) == codes.Canceled {
if wr.Err == io.EOF || connect.CodeOf(wr.Err) == connect.CodeCanceled {
assert.Len(t, wr.Presences, 0)
wg.Done()
return
Expand Down
15 changes: 7 additions & 8 deletions test/integration/auth_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ import (
"testing"
"time"

"connectrpc.com/connect"
"github.com/rs/xid"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/yorkie-team/yorkie/api/types"
"github.com/yorkie-team/yorkie/client"
Expand Down Expand Up @@ -127,7 +126,7 @@ func TestProjectAuthWebhook(t *testing.T) {
assert.NoError(t, err)
defer func() { assert.NoError(t, cliWithoutToken.Close()) }()
err = cliWithoutToken.Activate(ctx)
assert.Equal(t, codes.Unauthenticated, status.Convert(err).Code())
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))

// client with invalid token
cliWithInvalidToken, err := client.Dial(
Expand All @@ -138,7 +137,7 @@ func TestProjectAuthWebhook(t *testing.T) {
assert.NoError(t, err)
defer func() { assert.NoError(t, cliWithInvalidToken.Close()) }()
err = cliWithInvalidToken.Activate(ctx)
assert.Equal(t, codes.Unauthenticated, status.Convert(err).Code())
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))
})

t.Run("Selected method authorization webhook test", func(t *testing.T) {
Expand Down Expand Up @@ -176,7 +175,7 @@ func TestProjectAuthWebhook(t *testing.T) {

doc := document.New(helper.TestDocKey(t))
err = cli.Attach(ctx, doc)
assert.Equal(t, codes.Unauthenticated, status.Convert(err).Code())
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))

_, err = cli.Watch(ctx, doc)
assert.Equal(t, client.ErrDocumentNotAttached, err)
Expand Down Expand Up @@ -264,7 +263,7 @@ func TestAuthWebhook(t *testing.T) {
defer func() { assert.NoError(t, cli.Close()) }()

err = cli.Activate(ctx)
assert.Equal(t, codes.Unauthenticated, status.Convert(err).Code())
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))
})

t.Run("authorized request cache test", func(t *testing.T) {
Expand Down Expand Up @@ -395,14 +394,14 @@ func TestAuthWebhook(t *testing.T) {
// 01. multiple requests.
for i := 0; i < 3; i++ {
err = cli.Activate(ctx)
assert.Equal(t, codes.Unauthenticated, status.Convert(err).Code())
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))
}

// 02. multiple requests after eviction by ttl.
time.Sleep(unauthorizedTTL)
for i := 0; i < 3; i++ {
err = cli.Activate(ctx)
assert.Equal(t, codes.Unauthenticated, status.Convert(err).Code())
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))
}
assert.Equal(t, 2, reqCnt)
})
Expand Down
1 change: 0 additions & 1 deletion test/integration/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)

func TestHealthCheck(t *testing.T) {
Expand Down
17 changes: 1 addition & 16 deletions test/integration/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build integration
//gdo:build integration

/*
* Copyright 2020 The Yorkie Authors. All rights reserved.
Expand All @@ -25,8 +25,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/yorkie-team/yorkie/client"
"github.com/yorkie-team/yorkie/pkg/document"
Expand Down Expand Up @@ -90,19 +88,6 @@ func syncClientsThenAssertEqual(t *testing.T, pairs []clientAndDocPair) {
}
}

// clientConn is a helper function to create a client connection.
func clientConn() (*grpc.ClientConn, error) {
conn, err := grpc.Dial(
defaultServer.RPCAddr(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}

return conn, nil
}

// activeClients creates and activates the given number of clients.
func activeClients(t *testing.T, n int) (clients []*client.Client) {
for i := 0; i < n; i++ {
Expand Down
11 changes: 5 additions & 6 deletions test/integration/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import (
"context"
"testing"

"connectrpc.com/connect"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/yorkie-team/yorkie/test/helper"
)
Expand All @@ -39,18 +38,18 @@ func TestUser(t *testing.T) {
password := "password123!"

_, err := adminCli.LogIn(ctx, username, password)
assert.Equal(t, codes.NotFound, status.Convert(err).Code())
assert.Equal(t, connect.CodeNotFound, connect.CodeOf(err))

_, err = adminCli.SignUp(ctx, "name !@#", password)
assert.Equal(t, codes.InvalidArgument, status.Convert(err).Code())
assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))

_, err = adminCli.SignUp(ctx, username, "pass")
assert.Equal(t, codes.InvalidArgument, status.Convert(err).Code())
assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))

_, err = adminCli.SignUp(ctx, username, password)
assert.NoError(t, err)

_, err = adminCli.LogIn(ctx, username, "asdf")
assert.Equal(t, codes.Unauthenticated, status.Convert(err).Code())
assert.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))
})
}

0 comments on commit 3329871

Please sign in to comment.