Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package grpc |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "github.com/stretchr/testify/assert" |
| 21 | "google.golang.org/grpc" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | // A Mock Probe that returns the Ready member using the IsReady() func |
| 26 | type MockReadyProbe struct { |
| 27 | Ready bool |
| 28 | } |
| 29 | |
| 30 | func (m *MockReadyProbe) IsReady() bool { |
| 31 | return m.Ready |
| 32 | } |
| 33 | |
| 34 | // A Mock handler that returns the request as its result |
| 35 | func MockUnaryHandler(ctx context.Context, req interface{}) (interface{}, error) { |
| 36 | _ = ctx |
| 37 | return req, nil |
| 38 | } |
| 39 | |
| 40 | func TestNewGrpcServer(t *testing.T) { |
| 41 | server := NewGrpcServer("127.0.0.1", 1234, nil, false) |
| 42 | assert.NotNil(t, server) |
| 43 | } |
| 44 | |
| 45 | func TestMkServerInterceptorNoProbe(t *testing.T) { |
| 46 | server := NewGrpcServer("127.0.0.1", 1234, nil, false) |
| 47 | assert.NotNil(t, server) |
| 48 | |
| 49 | f := mkServerInterceptor(server) |
| 50 | assert.NotNil(t, f) |
| 51 | |
| 52 | req := "SomeRequest" |
| 53 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 54 | |
| 55 | result, err := f(context.Background(), |
| 56 | req, |
| 57 | &serverInfo, |
| 58 | MockUnaryHandler) |
| 59 | |
| 60 | assert.Nil(t, err) |
| 61 | assert.Equal(t, "SomeRequest", result) |
| 62 | } |
| 63 | |
| 64 | func TestMkServerInterceptorReady(t *testing.T) { |
| 65 | server := NewGrpcServer("127.0.0.1", 1234, nil, false) |
| 66 | assert.NotNil(t, server) |
| 67 | |
| 68 | f := mkServerInterceptor(server) |
| 69 | assert.NotNil(t, f) |
| 70 | |
| 71 | req := "SomeRequest" |
| 72 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 73 | |
| 74 | probe := &MockReadyProbe{Ready: true} |
| 75 | server.AttachReadyProbe(probe) |
| 76 | |
| 77 | result, err := f(context.Background(), |
| 78 | req, |
| 79 | &serverInfo, |
| 80 | MockUnaryHandler) |
| 81 | |
| 82 | assert.Nil(t, err) |
| 83 | assert.NotNil(t, result) |
| 84 | } |
| 85 | |
| 86 | func TestMkServerInterceptorNotReady(t *testing.T) { |
| 87 | server := NewGrpcServer("127.0.0.1", 1234, nil, false) |
| 88 | assert.NotNil(t, server) |
| 89 | |
| 90 | f := mkServerInterceptor(server) |
| 91 | assert.NotNil(t, f) |
| 92 | |
| 93 | req := "SomeRequest" |
| 94 | serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"} |
| 95 | |
| 96 | probe := &MockReadyProbe{Ready: false} |
| 97 | server.AttachReadyProbe(probe) |
| 98 | |
| 99 | result, err := f(context.Background(), |
| 100 | req, |
| 101 | &serverInfo, |
| 102 | MockUnaryHandler) |
| 103 | |
| 104 | assert.NotNil(t, err) |
| 105 | assert.Nil(t, result) |
| 106 | } |