2015-06-04 10:33:12 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-04 11:11:35 +00:00
|
|
|
"io/ioutil"
|
2015-06-04 10:33:12 +00:00
|
|
|
"net"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var nextPort uint32 = 15000
|
|
|
|
|
|
|
|
func getPort() int {
|
|
|
|
return int(atomic.AddUint32(&nextPort, 1))
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:11:35 +00:00
|
|
|
func tmpDir(t *testing.T) string {
|
|
|
|
dir, err := ioutil.TempDir("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2015-06-04 10:33:12 +00:00
|
|
|
func testServer(t *testing.T, cb func(*Config)) *Server {
|
|
|
|
// Setup the default settings
|
|
|
|
config := DefaultConfig()
|
|
|
|
config.DevMode = true
|
|
|
|
config.RPCAddr = &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: getPort(),
|
|
|
|
}
|
|
|
|
config.NodeName = fmt.Sprintf("Node %d", config.RPCAddr.Port)
|
|
|
|
|
|
|
|
// Tighten the Serf timing
|
|
|
|
config.SerfConfig.MemberlistConfig.BindAddr = "127.0.0.1"
|
|
|
|
config.SerfConfig.MemberlistConfig.BindPort = getPort()
|
|
|
|
config.SerfConfig.MemberlistConfig.SuspicionMult = 2
|
2015-06-04 11:02:39 +00:00
|
|
|
config.SerfConfig.MemberlistConfig.RetransmitMult = 2
|
2015-06-04 10:33:12 +00:00
|
|
|
config.SerfConfig.MemberlistConfig.ProbeTimeout = 50 * time.Millisecond
|
|
|
|
config.SerfConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
|
|
|
|
config.SerfConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
|
|
|
|
|
|
|
// Tighten the Raft timing
|
|
|
|
config.RaftConfig.LeaderLeaseTimeout = 20 * time.Millisecond
|
|
|
|
config.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond
|
|
|
|
config.RaftConfig.ElectionTimeout = 40 * time.Millisecond
|
|
|
|
|
|
|
|
// Invoke the callback if any
|
|
|
|
if cb != nil {
|
|
|
|
cb(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create server
|
|
|
|
server, err := NewServer(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return server
|
|
|
|
}
|
2015-06-05 22:11:16 +00:00
|
|
|
|
2015-06-07 19:03:05 +00:00
|
|
|
func testJoin(t *testing.T, s1 *Server, other ...*Server) {
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfConfig.MemberlistConfig.BindPort)
|
|
|
|
for _, s2 := range other {
|
|
|
|
if num, err := s2.Join([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
} else if num != 1 {
|
|
|
|
t.Fatalf("bad: %d", num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:11:16 +00:00
|
|
|
func TestServer_RPC(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := s1.RPC("Status.Ping", struct{}{}, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|