-
Notifications
You must be signed in to change notification settings - Fork 18
/
broker_test.go
148 lines (131 loc) · 4.84 KB
/
broker_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
package healer
import (
"errors"
"io"
"net"
"sync"
"testing"
"github.com/bytedance/mockey"
"github.com/smartystreets/goconvey/convey"
)
func newMockBroker() *Broker {
return &Broker{
address: "localhost:9092",
nodeID: 0,
config: &BrokerConfig{},
conn: &MockConn{},
}
}
func TestNewBroker(t *testing.T) {
mockey.PatchConvey("TestNewBroker", t, func() {
mockey.Mock((*Broker).requestAPIVersions).Return(APIVersionsResponse{}, nil).Build()
mockey.Mock((*net.Dialer).Dial).Return(&MockConn{}, nil).Build()
broker, err := NewBroker("127.0.0.1:9092", 0, DefaultBrokerConfig())
convey.So(err, convey.ShouldBeNil)
convey.So(broker, convey.ShouldNotBeNil)
})
}
func TestLockInNewBroker(t *testing.T) {
mockey.PatchConvey("failed Dial could not result in dead lock", t, func() {
errMock := errors.New("mock dial error")
dial := mockey.Mock((*net.Dialer).Dial).Return(nil, errMock).Build()
broker, err := NewBroker("127.0.0.1:9092", 0, DefaultBrokerConfig())
convey.So(dial.Times(), convey.ShouldEqual, 1)
convey.So(err, convey.ShouldNotBeNil)
convey.So(broker, convey.ShouldBeNil)
})
}
func TestGetHighestAvailableAPIVersion(t *testing.T) {
mockey.PatchConvey("TestgetHighestAvailableAPIVersion", t, func() {
key := API_MetadataRequest
for _, c := range []struct {
apiVersion APIVersion
want uint16
}{
{apiVersion: APIVersion{ApiKey(key), 1, 10}, want: 7},
{apiVersion: APIVersion{ApiKey(key), 1, 6}, want: 4},
{apiVersion: APIVersion{ApiKey(key), 2, 10}, want: 7},
{apiVersion: APIVersion{ApiKey(key), 5, 6}, want: 0},
} {
broker := &Broker{
apiVersions: []APIVersion{
c.apiVersion,
},
}
got := broker.getHighestAvailableAPIVersion(key)
convey.So(got, convey.ShouldEqual, c.want)
}
broker := &Broker{}
got := broker.getHighestAvailableAPIVersion(1024)
convey.So(got, convey.ShouldEqual, 0)
})
}
func TestReopenConn(t *testing.T) {
mockey.PatchConvey("conn EOF and reopen new conn", t, func() {
mockey.Mock(newAPIVersionsResponse).Return(APIVersionsResponse{}, nil).Build()
mockey.Mock((*net.Dialer).Dial).Return(&MockConn{}, nil).Build()
mockey.Mock(NewMetadataResponse).Return(MetadataResponse{}, nil).Build()
brokerCloseOrigin := (*Broker).Close
brokerClose := mockey.Mock((*Broker).Close).To(func(broker *Broker) { (brokerCloseOrigin)(broker) }).Origin(&brokerCloseOrigin).Build()
ensureOpenOrigin := (*Broker).ensureOpen
ensureOpen := mockey.Mock((*Broker).ensureOpen).To(func(broker *Broker) error { return (ensureOpenOrigin)(broker) }).Origin(&ensureOpenOrigin).Build()
createConnOrigin := (*Broker).createConn
createConn := mockey.Mock((*Broker).createConn).To(func(broker *Broker) error { return (createConnOrigin)(broker) }).Origin(&createConnOrigin).Build()
broker, err := NewBroker("127.0.0.1:9092", 0, DefaultBrokerConfig())
convey.So(err, convey.ShouldBeNil)
convey.So(broker, convey.ShouldNotBeNil)
convey.So(ensureOpen.Times(), convey.ShouldEqual, 0)
convey.So(createConn.Times(), convey.ShouldEqual, 1)
failCount := 0
mockey.Mock(defaultReadParser.Read).
To(func() ([]byte, error) {
if failCount == 0 {
failCount++
return nil, io.EOF
}
return make([]byte, 0), nil
}).Build()
req := NewMetadataRequest("healer-unittest", []string{"test-topic"})
_, err = broker.RequestAndGet(req) // EOF
convey.So(errors.Is(err, io.EOF), convey.ShouldBeTrue)
convey.So(brokerClose.Times(), convey.ShouldEqual, 1)
convey.So(ensureOpen.Times(), convey.ShouldEqual, 1)
convey.So(createConn.Times(), convey.ShouldEqual, 1)
_, err = broker.RequestAndGet(req)
convey.So(err, convey.ShouldBeNil)
convey.So(brokerClose.Times(), convey.ShouldEqual, 1)
convey.So(ensureOpen.Times(), convey.ShouldEqual, 2)
convey.So(createConn.Times(), convey.ShouldEqual, 2)
})
}
func TestRequestLock(t *testing.T) {
mockey.PatchConvey("ONE broker do Request in multi goroutines in the same time. One closes because of EOF, others should reopen and then complete Request, no nil point panic", t, func() {
mockey.Mock(newAPIVersionsResponse).Return(APIVersionsResponse{}, nil).Build()
mockey.Mock((*net.Dialer).Dial).Return(&MockConn{}, nil).Build()
mockey.Mock(NewMetadataResponse).Return(MetadataResponse{}, nil).Build()
broker, err := NewBroker("127.0.0.1:9092", 0, DefaultBrokerConfig())
convey.So(err, convey.ShouldBeNil)
convey.So(broker, convey.ShouldNotBeNil)
failCount := 0
mockey.Mock(defaultReadParser.Read).
To(func() ([]byte, error) {
if failCount%2 == 0 {
failCount++
return nil, io.EOF
}
return make([]byte, 0), nil
}).Build()
count := 10
wg := sync.WaitGroup{}
wg.Add(count)
for i := 0; i < count; i++ {
go func() {
req := NewMetadataRequest("healer-unittest", []string{"test-topic"})
_, err = broker.RequestAndGet(req)
wg.Done()
}()
}
wg.Wait()
convey.So(err, convey.ShouldBeNil)
})
}