blob: df3911c92695511e975d5b411968d93ea674ea13 [file] [log] [blame]
Scott Baker104b67d2019-10-29 15:56:27 -07001/*
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 */
16package grpc
17
18import (
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
26type MockReadyProbe struct {
27 Ready bool
28}
29
30func (m *MockReadyProbe) IsReady() bool {
31 return m.Ready
32}
33
34// A Mock handler that returns the request as its result
35func MockUnaryHandler(ctx context.Context, req interface{}) (interface{}, error) {
36 _ = ctx
37 return req, nil
38}
39
40func TestNewGrpcServer(t *testing.T) {
Scott Bakerdefa2bf2019-11-08 12:03:56 -080041 server := NewGrpcServer("127.0.0.1", 1234, nil, false, nil)
Scott Baker104b67d2019-10-29 15:56:27 -070042 assert.NotNil(t, server)
43}
44
45func TestMkServerInterceptorNoProbe(t *testing.T) {
Scott Bakerdefa2bf2019-11-08 12:03:56 -080046 server := NewGrpcServer("127.0.0.1", 1234, nil, false, nil)
Scott Baker104b67d2019-10-29 15:56:27 -070047 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
64func TestMkServerInterceptorReady(t *testing.T) {
Scott Bakerdefa2bf2019-11-08 12:03:56 -080065 probe := &MockReadyProbe{Ready: true}
66
67 server := NewGrpcServer("127.0.0.1", 1234, nil, false, probe)
Scott Baker104b67d2019-10-29 15:56:27 -070068 assert.NotNil(t, server)
69
70 f := mkServerInterceptor(server)
71 assert.NotNil(t, f)
72
73 req := "SomeRequest"
74 serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"}
75
Scott Baker104b67d2019-10-29 15:56:27 -070076 result, err := f(context.Background(),
77 req,
78 &serverInfo,
79 MockUnaryHandler)
80
81 assert.Nil(t, err)
82 assert.NotNil(t, result)
83}
84
85func TestMkServerInterceptorNotReady(t *testing.T) {
Scott Bakerdefa2bf2019-11-08 12:03:56 -080086 probe := &MockReadyProbe{Ready: false}
87
88 server := NewGrpcServer("127.0.0.1", 1234, nil, false, probe)
Scott Baker104b67d2019-10-29 15:56:27 -070089 assert.NotNil(t, server)
90
91 f := mkServerInterceptor(server)
92 assert.NotNil(t, f)
93
94 req := "SomeRequest"
95 serverInfo := grpc.UnaryServerInfo{Server: nil, FullMethod: "somemethod"}
96
Scott Baker104b67d2019-10-29 15:56:27 -070097 result, err := f(context.Background(),
98 req,
99 &serverInfo,
100 MockUnaryHandler)
101
102 assert.NotNil(t, err)
103 assert.Nil(t, result)
104}