open-nomad/client/client_test.go
2015-08-20 16:12:28 -07:00

96 lines
2.2 KiB
Go

package client
import (
"fmt"
"net"
"sync/atomic"
"testing"
"time"
"github.com/hashicorp/nomad/nomad"
"github.com/hashicorp/nomad/testutil"
)
var nextPort uint32 = 16000
func getPort() int {
return int(atomic.AddUint32(&nextPort, 1))
}
func testServer(t *testing.T, cb func(*nomad.Config)) (*nomad.Server, string) {
// Setup the default settings
config := nomad.DefaultConfig()
config.Build = "unittest"
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
config.SerfConfig.MemberlistConfig.RetransmitMult = 2
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 := nomad.NewServer(config)
if err != nil {
t.Fatalf("err: %v", err)
}
return server, config.RPCAddr.String()
}
func testClient(t *testing.T, cb func(c *Config)) *Client {
conf := DefaultConfig()
if cb != nil {
cb(conf)
}
client, err := NewClient(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
return client
}
func TestClient_StartStop(t *testing.T) {
client := testClient(t, nil)
if err := client.Shutdown(); err != nil {
t.Fatalf("err: %v", err)
}
}
func TestClient_RPC(t *testing.T) {
s1, addr := testServer(t, nil)
defer s1.Shutdown()
c1 := testClient(t, func(c *Config) {
c.Servers = []string{addr}
})
defer c1.Shutdown()
// RPC should succeed
testutil.WaitForResult(func() (bool, error) {
var out struct{}
err := c1.RPC("Status.Ping", struct{}{}, &out)
return err == nil, err
}, func(err error) {
t.Fatalf("err: %v", err)
})
}