Skip to content

Commit b4ac39a

Browse files
committed
Fix bugs; improve tests
1 parent cbedafe commit b4ac39a

6 files changed

+78
-11
lines changed

client_test.go

+55-3
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ func testServiceRegisterDeregister(t *testing.T, dbClient API) {
557557
}
558558

559559
// Wait a while to make sure background refresh is working correctly
560-
time.Sleep(5 * time.Millisecond)
560+
time.Sleep(serviceTTL * 2)
561561

562562
resp, err = dbClient.GetService("athena")
563563
if err != nil {
@@ -579,8 +579,60 @@ func testServiceRegisterDeregister(t *testing.T, dbClient API) {
579579
t.Fatalf("Fatal deregistering service. Err: %+v\n", err)
580580
}
581581

582-
// Wait a while to make sure background refresh is working correctly
583-
time.Sleep(5 * time.Millisecond)
582+
resp, err = dbClient.GetService("athena")
583+
if err != nil {
584+
t.Fatalf("Fatal getting service. Err: %+v\n", err)
585+
}
586+
587+
log.Infof("Got service list: %+v\n", resp)
588+
589+
if len(resp) != 0 {
590+
t.Fatalf("Service still in list after deregister")
591+
}
592+
}
593+
594+
func TestEtcdServiceRegisterMultiple(t *testing.T) {
595+
testServiceMultipleRegister(t, etcdClient)
596+
}
597+
598+
func TestConsulServiceRegisterMultiple(t *testing.T) {
599+
testServiceMultipleRegister(t, consulClient)
600+
}
601+
602+
func testServiceMultipleRegister(t *testing.T, dbClient API) {
603+
// Service info
604+
service1Info := ServiceInfo{
605+
ServiceName: "athena",
606+
HostAddr: "10.10.10.10",
607+
Port: 4567,
608+
}
609+
610+
// register it multiple times
611+
for i := 0; i < 5; i++ {
612+
if err := dbClient.RegisterService(service1Info); err != nil {
613+
t.Fatalf("Fatal registering service. Err: %+v\n", err)
614+
}
615+
log.Infof("Registered service: %+v", service1Info)
616+
617+
// sleep for a second
618+
time.Sleep(time.Second)
619+
}
620+
621+
resp, err := dbClient.GetService("athena")
622+
if err != nil {
623+
t.Fatalf("Fatal getting service. Err: %+v\n", err)
624+
}
625+
626+
log.Infof("Got service list: %+v\n", resp)
627+
628+
if (len(resp) != 1) || (resp[0] != service1Info) {
629+
t.Fatalf("Resp service list did not match input")
630+
}
631+
632+
// deregister it
633+
if err := dbClient.DeregisterService(service1Info); err != nil {
634+
t.Fatalf("Fatal deregistering service. Err: %+v\n", err)
635+
}
584636

585637
resp, err = dbClient.GetService("athena")
586638
if err != nil {

consulClient.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func (cp *ConsulClient) GetObj(key string, retVal interface{}) error {
9797
}
9898

9999
// Parse JSON response
100-
if err := json.Unmarshal([]byte(resp.Value), retVal); err != nil {
101-
log.Errorf("Error parsing object %s, Err %v", resp.Value, err)
100+
if err := json.Unmarshal(resp.Value, retVal); err != nil {
101+
log.Errorf("Error parsing object %v, Err %v", resp.Value, err)
102102
return err
103103
}
104104

@@ -121,7 +121,7 @@ func (cp *ConsulClient) ListDir(key string) ([]string, error) {
121121

122122
var keys []string
123123
for _, kv := range kvs {
124-
keys = append(keys, kv.Key)
124+
keys = append(keys, string(kv.Value))
125125
}
126126

127127
return keys, nil
@@ -152,5 +152,6 @@ func (cp *ConsulClient) DelObj(key string) error {
152152

153153
// GetLocalAddr gets local address of the host
154154
func (cp *ConsulClient) GetLocalAddr() (string, error) {
155+
log.Panic("Calling unsupported API")
155156
return "", nil
156157
}

consulService.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,23 @@ func (cp *ConsulClient) RegisterService(serviceInfo ServiceInfo) error {
4444

4545
log.Infof("Registering service key: %s, value: %+v", keyName, serviceInfo)
4646

47-
// if there is a previously registered service, stop refreshing it
47+
// if there is a previously registered service, no need to register it again..
4848
if cp.serviceDb[keyName] != nil {
49-
close(cp.serviceDb[keyName].stopChan)
49+
srvState := cp.serviceDb[keyName]
50+
if (srvState.ServiceName == serviceInfo.ServiceName) && (srvState.HostAddr == serviceInfo.HostAddr) &&
51+
(srvState.Port == serviceInfo.Port) {
52+
return nil
53+
}
54+
55+
// stop and release the old key
56+
close(srvState.stopChan)
57+
58+
// Delete the service instance
59+
_, err := cp.client.KV().Delete(keyName, nil)
60+
if err != nil {
61+
log.Errorf("Error deleting key %s. Err: %v", keyName, err)
62+
return err
63+
}
5064
}
5165

5266
// JSON format the object

etcdClient.go

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ func (ep *EtcdClient) GetLocalAddr() (string, error) {
221221
Name string `json:"name"`
222222
}
223223

224+
log.Panic("Calling unsupported API")
225+
224226
// Get ep state from etcd
225227
if _, err := httpGetJSON("http://localhost:2379/v2/stats/self", &epData); err != nil {
226228
log.Errorf("Error getting self state. Err: %v", err)

modeldb/modeldb.go

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func ReadObj(objType, objKey string, retVal interface{}) error {
108108
err := cdb.GetObj(key, retVal)
109109
if err != nil {
110110
log.Errorf("Error reading object: %s. Err: %v", key, err)
111+
return err
111112
}
112113

113114
return nil

objdb.go

-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ type Plugin interface {
7878

7979
// API Plugin API
8080
type API interface {
81-
// Return local address used by conf store
82-
GetLocalAddr() (string, error)
83-
8481
// Get a Key from conf store
8582
GetObj(key string, retValue interface{}) error
8683

0 commit comments

Comments
 (0)