From a778d942b3a01c7d6e3c336e13d1dd4bbff6700f Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 10 Jan 2014 18:05:09 +0800 Subject: [PATCH] test(server_test.go) Fix a deadlock. We stop all the servers before finishing the MultiNode test. The mock transporter directly calls to the function of the target server. If the target is stopped, the transporter will just hang there waiting for a reply. So the sender server will also hang for a reply. We add a timeout for the reply to solve this problem. --- server_test.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/server_test.go b/server_test.go index 3ac36811b0..4994faeddb 100644 --- a/server_test.go +++ b/server_test.go @@ -501,7 +501,19 @@ func TestServerMultiNode(t *testing.T) { clonedReq := &RequestVoteRequest{} json.Unmarshal(b, clonedReq) - return target.RequestVote(clonedReq) + c := make(chan *RequestVoteResponse) + + go func() { + c <- target.RequestVote(clonedReq) + }() + + select { + case resp := <-c: + return resp + case <-time.After(time.Millisecond * 200): + return nil + } + } transporter.sendAppendEntriesRequestFunc = func(s Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse { mutex.RLock() @@ -512,7 +524,18 @@ func TestServerMultiNode(t *testing.T) { clonedReq := &AppendEntriesRequest{} json.Unmarshal(b, clonedReq) - return target.AppendEntries(clonedReq) + c := make(chan *AppendEntriesResponse) + + go func() { + c <- target.AppendEntries(clonedReq) + }() + + select { + case resp := <-c: + return resp + case <-time.After(time.Millisecond * 200): + return nil + } } disTransporter := &testTransporter{}