Skip to content

Commit d8adf9e

Browse files
authored
Fix rpc test issue (#5291)
Currently in the rpc test, a shared fakeStream is used among multiple runs. This will accumulate the Send data and make the test failed with the latest github.com/stretchr/testify v1.11.1 version. The issue is ignored in the current testify v1.11.0 version due to an incomplete validation. The patchset fixes the issue by creating a new fakeStream in each run. Change-Id: Icab00394dd7635fb552379cbfdf1fe111087b7db Signed-off-by: Baohua Yang <[email protected]>
1 parent e1c5a1f commit d8adf9e

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

orderer/common/cluster/rpc_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,11 @@ func TestSend(t *testing.T) {
210210
}
211211

212212
l := &sync.Mutex{}
213-
var tst testCase
214213

215214
sent := make(chan struct{})
216215

217216
var sendCalls uint32
218217

219-
fakeStream1 := &mocks.StepClientStream{}
220-
fakeStream1.On("Context", mock.Anything).Return(context.Background())
221-
fakeStream1.On("Auth").Return(nil)
222-
fakeStream1.On("Send", mock.Anything).Return(func(*orderer.StepRequest) error {
223-
l.Lock()
224-
defer l.Unlock()
225-
atomic.AddUint32(&sendCalls, 1)
226-
sent <- struct{}{}
227-
return tst.sendReturns
228-
})
229-
230218
for _, tst := range []testCase{
231219
{
232220
name: "Send and Receive submit succeed",
@@ -272,6 +260,17 @@ func TestSend(t *testing.T) {
272260
l.Unlock()
273261

274262
t.Run(testCase.name, func(t *testing.T) {
263+
fakeStream := &mocks.StepClientStream{}
264+
fakeStream.On("Context", mock.Anything).Return(context.Background())
265+
fakeStream.On("Auth").Return(nil)
266+
fakeStream.On("Send", mock.Anything).Return(func(req *orderer.StepRequest) error {
267+
l.Lock()
268+
defer l.Unlock()
269+
atomic.AddUint32(&sendCalls, 1)
270+
sent <- struct{}{}
271+
return testCase.sendReturns
272+
})
273+
275274
atomic.StoreUint32(&sendCalls, 0)
276275
isSend := testCase.receiveReturns == nil
277276
comm := &mocks.Communicator{}
@@ -282,7 +281,7 @@ func TestSend(t *testing.T) {
282281
Logger: flogging.MustGetLogger("test"),
283282
ProbeConn: func(_ *grpc.ClientConn) error { return nil },
284283
GetStreamFunc: func(ctx context.Context) (cluster.StepClientStream, error) {
285-
return fakeStream1, nil
284+
return fakeStream, nil
286285
},
287286
Channel: "mychannel",
288287
}
@@ -303,7 +302,7 @@ func TestSend(t *testing.T) {
303302
}
304303

305304
if testCase.remoteError == nil && testCase.expectedErr == "" && isSend {
306-
fakeStream1.AssertCalled(t, "Send", testCase.sendCalledWith)
305+
fakeStream.AssertCalled(t, "Send", testCase.sendCalledWith)
307306
// Ensure that if we succeeded - only 1 stream was created despite 2 calls
308307
// to Send() were made
309308
err := testCase.method(rpc)

0 commit comments

Comments
 (0)