This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
/
client_test.go
160 lines (127 loc) · 5.03 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cherami
import (
"testing"
"time"
"github.com/uber/cherami-client-go/common/metrics"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/uber-common/bark"
"github.com/uber/tchannel-go"
)
type ClientSuite struct {
*require.Assertions // override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test, not merely log an error
suite.Suite
}
func TestClientSuite(t *testing.T) {
suite.Run(t, new(ClientSuite))
}
func (s *ClientSuite) SetupTest() {
s.Assertions = require.New(s.T()) // Have to define our overridden assertions in the test setup. If we did it earlier, s.T() will return nil
}
// TestClientNoOptions tests to make sure we setup a default logger
// and a null reporter even if the options are nil
func (s *ClientSuite) TestClientNoOptions() {
ip, err := tchannel.ListenIP()
s.Nil(err)
listenIP := ip.String()
// pass nil options
cheramiClient, err := NewClient("cherami-client-test-nil", listenIP, 0, nil)
s.Nil(err)
cheramiImpl := cheramiClient.(*clientImpl)
// Make sure the reporter is null reporter
assert.IsType(s.T(), &metrics.NullReporter{}, cheramiImpl.options.MetricsReporter)
// Make sure logger is not nil as well
s.NotNil(cheramiImpl.options.Logger)
cheramiClient.Close()
}
// TestClientOptionsOnlyTimeout tests to make sure we setup the logger
// and reporter even if the options has only the timeout set.
func (s *ClientSuite) TestClientOptionsOnlyTimeout() {
ip, err := tchannel.ListenIP()
s.Nil(err)
listenIP := ip.String()
// setup options with just the timeout set
options := &ClientOptions{
Timeout: 1 * time.Minute,
}
cheramiClient, err := NewClient("cherami-client-test-both", listenIP, 0, options)
s.Nil(err)
cheramiImpl := cheramiClient.(*clientImpl)
// Make sure the reporter is null reporter
assert.IsType(s.T(), &metrics.NullReporter{}, cheramiImpl.options.MetricsReporter)
// Make sure logger is not nil as well
s.NotNil(cheramiImpl.options.Logger)
cheramiClient.Close()
}
// TestClientOptionsNoReporter tests to make sure we setup the null reporter
// even if the options has a logger set
func (s *ClientSuite) TestClientOptionsNoReporter() {
ip, err := tchannel.ListenIP()
s.Nil(err)
listenIP := ip.String()
// setup options with just the logger
options := &ClientOptions{
Timeout: 1 * time.Minute,
Logger: bark.NewLoggerFromLogrus(log.StandardLogger()),
}
cheramiClient, err := NewClient("cherami-client-test-reporter", listenIP, 0, options)
s.Nil(err)
cheramiImpl := cheramiClient.(*clientImpl)
// Make sure the reporter is null reporter
assert.IsType(s.T(), &metrics.NullReporter{}, cheramiImpl.options.MetricsReporter)
cheramiClient.Close()
}
// TestClientOptionsNoLogger tests to make sure we setup the default logger even
// if the options is valid and has just the reporter set
func (s *ClientSuite) TestClientOptionsNoLogger() {
ip, err := tchannel.ListenIP()
s.Nil(err)
listenIP := ip.String()
// setup options with just the reporter
options := &ClientOptions{
Timeout: 1 * time.Minute,
MetricsReporter: metrics.NewNullReporter(),
}
cheramiClient, err := NewClient("cherami-client-test-logger", listenIP, 0, options)
s.Nil(err)
cheramiImpl := cheramiClient.(*clientImpl)
// Make sure logger is not nil as well
s.NotNil(cheramiImpl.options.Logger)
cheramiClient.Close()
}
// TestClientOptionsBypassAuthProvider tests the client with BypassAuthProvider
func (s *ClientSuite) TestClientOptionsBypassAuthProvider() {
ip, err := tchannel.ListenIP()
s.Nil(err)
listenIP := ip.String()
// setup options with just the AuthProvider
options := &ClientOptions{
AuthProvider: NewBypassAuthProvider(),
}
cheramiClient, err := NewClient("cherami-client-test-logger", listenIP, 0, options)
s.Nil(err)
cheramiImpl := cheramiClient.(*clientImpl)
s.NotNil(cheramiImpl.options.AuthProvider)
cheramiClient.Close()
}