Browse Source

integration: fix bind-addr-in-use

The bug happens when restarted member wants to listen on its original
port, but finds out that it has been occupied by some client.

Use well-known port instead of ephemeral port, so client cannot occupy
the listen port anymore.
Yicheng Qin 10 năm trước cách đây
mục cha
commit
8e79fd85cb
1 tập tin đã thay đổi với 7 bổ sung1 xóa
  1. 7 1
      integration/cluster_test.go

+ 7 - 1
integration/cluster_test.go

@@ -27,6 +27,7 @@ import (
 	"sort"
 	"sort"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
+	"sync/atomic"
 	"testing"
 	"testing"
 	"time"
 	"time"
 
 
@@ -49,6 +50,10 @@ const (
 
 
 var (
 var (
 	electionTicks = 10
 	electionTicks = 10
+
+	// integration test uses well-known ports to listen for each running member,
+	// which ensures restarted member could listen on specific port again.
+	nextListenPort int64 = 20000
 )
 )
 
 
 func init() {
 func init() {
@@ -596,7 +601,8 @@ func isMembersEqual(membs []client.Member, wmembs []client.Member) bool {
 }
 }
 
 
 func newLocalListener(t *testing.T) net.Listener {
 func newLocalListener(t *testing.T) net.Listener {
-	l, err := net.Listen("tcp", "127.0.0.1:0")
+	port := atomic.AddInt64(&nextListenPort, 1)
+	l, err := net.Listen("tcp", "127.0.0.1:"+strconv.FormatInt(port, 10))
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}