longganhua
2019-07-18 6f6e748602c0694712564cdf54164ca7f903222b
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
package agent
 
import (
    "fmt"
    "io"
    "math/rand"
    "net"
    "os"
    "time"
 
    "github.com/hashicorp/serf/serf"
    "github.com/hashicorp/serf/testutil"
)
 
func init() {
    // Seed the random number generator
    rand.Seed(time.Now().UnixNano())
}
 
func drainEventCh(ch <-chan string) {
    for {
        select {
        case <-ch:
        default:
            return
        }
    }
}
 
func getRPCAddr() string {
    for i := 0; i < 500; i++ {
        l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", rand.Int31n(25000)+1024))
        if err == nil {
            l.Close()
            return l.Addr().String()
        }
    }
 
    panic("no listener")
}
 
func testAgent(logOutput io.Writer) *Agent {
    return testAgentWithConfig(DefaultConfig(), serf.DefaultConfig(), logOutput)
}
 
func testAgentWithConfig(agentConfig *Config, serfConfig *serf.Config,
    logOutput io.Writer) *Agent {
 
    if logOutput == nil {
        logOutput = os.Stderr
    }
    serfConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
    serfConfig.MemberlistConfig.BindAddr = testutil.GetBindAddr().String()
    serfConfig.NodeName = serfConfig.MemberlistConfig.BindAddr
 
    agent, err := Create(agentConfig, serfConfig, logOutput)
    if err != nil {
        panic(err)
    }
    return agent
}