blob: a3afc09e27b8a393c2a021dec728093ff50020d4 [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) {
41 server := NewGrpcServer("127.0.0.1", 1234, nil, false)
42 assert.NotNil(t, server)
43}
44
45func 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
64func 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
86func 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}