mirror of https://github.com/status-im/consul.git
Testing Catalog.Register endpoint
This commit is contained in:
parent
7dc6662a93
commit
6b3578bc26
|
@ -6,7 +6,7 @@ import (
|
|||
|
||||
// Catalog endpoint is used to manipulate the service catalog
|
||||
type Catalog struct {
|
||||
*Server
|
||||
srv *Server
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -23,14 +23,14 @@ type Catalog struct {
|
|||
|
||||
// Register is used register that a node is providing a given service.
|
||||
func (c *Catalog) Register(args *rpc.RegisterRequest, reply *struct{}) error {
|
||||
if done, err := c.forward("Catalog.Register", args.Datacenter, args, reply); done {
|
||||
if done, err := c.srv.forward("Catalog.Register", args.Datacenter, args, reply); done {
|
||||
return err
|
||||
}
|
||||
|
||||
// Run it through raft
|
||||
_, err := c.raftApply(rpc.RegisterRequestType, args)
|
||||
_, err := c.srv.raftApply(rpc.RegisterRequestType, args)
|
||||
if err != nil {
|
||||
c.logger.Printf("[ERR] Register failed: %v", err)
|
||||
c.srv.logger.Printf("[ERR] Register failed: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package consul
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/consul/rpc"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCatalogRegister(t *testing.T) {
|
||||
dir1, s1 := testServer(t)
|
||||
defer os.RemoveAll(dir1)
|
||||
defer s1.Shutdown()
|
||||
client := rpcClient(t, s1)
|
||||
defer client.Close()
|
||||
|
||||
arg := rpc.RegisterRequest{
|
||||
Datacenter: "dc1",
|
||||
Node: "foo",
|
||||
Address: "127.0.0.1",
|
||||
ServiceName: "db",
|
||||
ServiceTag: "master",
|
||||
ServicePort: 8000,
|
||||
}
|
||||
var out struct{}
|
||||
|
||||
err := client.Call("Catalog.Register", &arg, &out)
|
||||
if err == nil || err.Error() != "No cluster leader" {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
// Wait for leader
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/hashicorp/consul/rpc"
|
||||
"github.com/ugorji/go/codec"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
|
@ -74,7 +75,9 @@ func (s *Server) handleConsulConn(conn net.Conn) {
|
|||
rpcCodec := codec.GoRpc.ServerCodec(conn, &codec.MsgpackHandle{})
|
||||
for !s.shutdown {
|
||||
if err := s.rpcServer.ServeRequest(rpcCodec); err != nil {
|
||||
if err != io.EOF {
|
||||
s.logger.Printf("[ERR] RPC error: %v (%v)", err, conn)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,8 +35,18 @@ func testServer(t *testing.T) (string, *Server) {
|
|||
config.RPCAddr = fmt.Sprintf("127.0.0.1:%d", p)
|
||||
config.SerfLANConfig.MemberlistConfig.BindAddr = "127.0.0.1"
|
||||
config.SerfLANConfig.MemberlistConfig.Port = getPort()
|
||||
config.SerfLANConfig.MemberlistConfig.ProbeTimeout = 200 * time.Millisecond
|
||||
config.SerfLANConfig.MemberlistConfig.ProbeInterval = time.Second
|
||||
config.SerfLANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
||||
|
||||
config.SerfWANConfig.MemberlistConfig.BindAddr = "127.0.0.1"
|
||||
config.SerfWANConfig.MemberlistConfig.Port = getPort()
|
||||
config.SerfWANConfig.MemberlistConfig.ProbeTimeout = 200 * time.Millisecond
|
||||
config.SerfWANConfig.MemberlistConfig.ProbeInterval = time.Second
|
||||
config.SerfWANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
||||
|
||||
config.RaftConfig.HeartbeatTimeout = 50 * time.Millisecond
|
||||
config.RaftConfig.ElectionTimeout = 50 * time.Millisecond
|
||||
|
||||
server, err := NewServer(config)
|
||||
if err != nil {
|
||||
|
|
|
@ -38,7 +38,7 @@ func TestStatusLeader(t *testing.T) {
|
|||
t.Fatalf("unexpected leader: %v", leader)
|
||||
}
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
if err := client.Call("Status.Leader", arg, &leader); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
|
|
Loading…
Reference in New Issue