Skip to content

Commit

Permalink
Add gRPC health checking (yorkie-team#176)
Browse files Browse the repository at this point in the history
Co-authored-by: Hackerwins <[email protected]>
  • Loading branch information
dc7303 and hackerwins authored Apr 16, 2021
1 parent 6c0ab3d commit e028d48
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion test/integration/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func TestArray(t *testing.T) {
clients := getActivatedClients(t, 2)
clients := createActivatedClients(t, 2)
c1 := clients[0]
c2 := clients[1]
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func TestCounter(t *testing.T) {
clients := getActivatedClients(t, 2)
clients := createActivatedClients(t, 2)
c1 := clients[0]
c2 := clients[1]
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

func TestDocument(t *testing.T) {
clients := getActivatedClients(t, 2)
clients := createActivatedClients(t, 2)
c1 := clients[0]
c2 := clients[1]
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func TestGarbageCollection(t *testing.T) {
clients := getActivatedClients(t, 2)
clients := createActivatedClients(t, 2)
c1 := clients[0]
c2 := clients[1]
defer func() {
Expand Down
40 changes: 40 additions & 0 deletions test/integration/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// +build integration

/*
* Copyright 2021 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package integration

import (
"context"
"testing"

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

func TestHealthCheck(t *testing.T) {
conn, err := createConn()
assert.NoError(t, err)
defer func() {
assert.NoError(t, conn.Close())
}()

cli := healthpb.NewHealthClient(conn)
resp, err := cli.Check(context.Background(), &healthpb.HealthCheckRequest{})
assert.NoError(t, err)
assert.Equal(t, resp.Status, healthpb.HealthCheckResponse_SERVING)
}
12 changes: 11 additions & 1 deletion test/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"

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

"github.com/yorkie-team/yorkie/client"
"github.com/yorkie-team/yorkie/pkg/document"
Expand Down Expand Up @@ -82,7 +83,16 @@ func syncClientsThenAssertEqual(t *testing.T, pairs []clientAndDocPair) {
}
}

func getActivatedClients(t *testing.T, n int) (clients []*client.Client) {
func createConn() (*grpc.ClientConn, error) {
conn, err := grpc.Dial(testYorkie.RPCAddr(), grpc.WithInsecure())
if err != nil {
return nil, err
}

return conn, nil
}

func createActivatedClients(t *testing.T, n int) (clients []*client.Client) {
for i := 0; i < n; i++ {
c, err := client.Dial(
testYorkie.RPCAddr(),
Expand Down
2 changes: 1 addition & 1 deletion test/integration/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func TestObject(t *testing.T) {
clients := getActivatedClients(t, 2)
clients := createActivatedClients(t, 2)
c1 := clients[0]
c2 := clients[1]
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/primitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func TestPrimitive(t *testing.T) {
clients := getActivatedClients(t, 2)
clients := createActivatedClients(t, 2)
c1 := clients[0]
c2 := clients[1]
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func TestSnapshot(t *testing.T) {
clients := getActivatedClients(t, 2)
clients := createActivatedClients(t, 2)
c1 := clients[0]
c2 := clients[1]
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func TestText(t *testing.T) {
clients := getActivatedClients(t, 2)
clients := createActivatedClients(t, 2)
c1 := clients[0]
c2 := clients[1]
defer func() {
Expand Down
8 changes: 7 additions & 1 deletion yorkie/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/status"

"github.com/yorkie-team/yorkie/api"
Expand Down Expand Up @@ -85,9 +87,13 @@ func NewServer(conf *Config, be *backend.Backend) (*Server, error) {
opts = append(opts, grpc.Creds(creds))
}

grpcServer := grpc.NewServer(opts...)
healthServer := health.NewServer()
healthpb.RegisterHealthServer(grpcServer, healthServer)

rpcServer := &Server{
conf: conf,
grpcServer: grpc.NewServer(opts...),
grpcServer: grpcServer,
backend: be,
}
api.RegisterYorkieServer(rpcServer.grpcServer, rpcServer)
Expand Down

0 comments on commit e028d48

Please sign in to comment.