This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
thriftset_test.go
168 lines (132 loc) · 3.28 KB
/
thriftset_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
161
162
163
164
165
166
167
168
package thriftset
import (
"testing"
"time"
"github.com/strava/go.serversets/fixedset"
"github.com/strava/go.serversets/internal/endpoints"
"github.com/apache/thrift/lib/go/thrift"
)
func TestThriftSet(t *testing.T) {
// this will not compile if ThriftSet does not
// implement the pooler interface.
var _ endpoints.Pooler = New(fixedset.New([]string{}))
}
func TestThriftSetClose(t *testing.T) {
count := 0
event := make(chan struct{}, 1)
watcherClosed = func() {
count++
event <- struct{}{}
}
ts := New(fixedset.New([]string{"endpoint"}))
socketBuilder = func(string, time.Duration) (*thrift.TSocket, error) {
return &thrift.TSocket{}, nil
}
c, _ := ts.GetConn()
c.Release()
ts.Close()
// little event channel to allow the other goroutine to run and quit as it should
<-event
if count != 1 {
t.Error("should close watching goroutine on watcher channel close")
}
// can all closed multiple times.
ts.Close()
ts.Close()
ts.Close()
if v := ts.IsClosed(); !v {
t.Errorf("should not be closed, because it is")
}
// This get request must releases the lock
_, err := ts.GetConn()
if err != ErrGetOnClosedSet {
t.Errorf("wrong error, got %v", err)
}
_, err = ts.GetConn()
if err != ErrGetOnClosedSet {
t.Errorf("wrong error, got %v", err)
}
// reset
watcherClosed = func() {}
}
func TestThriftSetGetConn(t *testing.T) {
socketBuilder = func(string, time.Duration) (*thrift.TSocket, error) {
return &thrift.TSocket{}, nil
}
fs := fixedset.New([]string{})
ts := New(fs)
defer ts.Close()
_, err := ts.GetConn()
if err != ErrNoEndpoints {
t.Errorf("incorrect error, got %v", err)
}
fs.SetEndpoints([]string{"endpoint"})
<-ts.Event()
_, err = ts.GetConn()
if err != nil {
t.Errorf("should have server, got %v", err)
}
}
func TestConn(t *testing.T) {
type testStruct struct {
Something int
}
fs := fixedset.New([]string{"host"})
ts := New(fs)
defer ts.Close()
object := &testStruct{123}
c, _ := ts.GetConn()
c.Client = object
c.Release()
c, _ = ts.GetConn()
if c.Client != object {
t.Errorf("should have same object in new conn")
}
c.Release()
}
func TestConnRelease(t *testing.T) {
ts := New(fixedset.New([]string{"endpoint"}))
defer ts.Close()
s1 := &thrift.TSocket{}
socketBuilder = func(string, time.Duration) (*thrift.TSocket, error) {
return s1, nil
}
c, _ := ts.GetConn()
if c.Socket != s1 {
t.Errorf("should get correct socket")
}
c.Release()
c, _ = ts.GetConn()
if c.Socket != s1 {
t.Errorf("should still get the same socket")
}
s2 := &thrift.TSocket{}
socketBuilder = func(string, time.Duration) (*thrift.TSocket, error) {
return s2, nil
}
c, _ = ts.GetConn()
if c.Socket != s2 {
t.Errorf("should get new second socket because first not released")
}
}
func TestConnClose(t *testing.T) {
ts := New(fixedset.New([]string{"endpoint"}))
defer ts.Close()
s1 := &thrift.TSocket{}
socketBuilder = func(string, time.Duration) (*thrift.TSocket, error) {
return s1, nil
}
c, _ := ts.GetConn()
if c.Socket != s1 {
t.Errorf("should get correct socket")
}
c.Close()
s2 := &thrift.TSocket{}
socketBuilder = func(string, time.Duration) (*thrift.TSocket, error) {
return s2, nil
}
c, _ = ts.GetConn()
if c.Socket != s2 {
t.Errorf("should get another socket because first closed and not returned")
}
}