@@ -4,13 +4,15 @@ import (
4
4
"bytes"
5
5
"encoding/json"
6
6
"fmt"
7
+ "os"
7
8
"regexp"
8
9
"sync"
9
10
"testing"
10
11
"time"
11
12
12
13
"github.com/NinesStack/sidecar/service"
13
14
"github.com/relistan/go-director"
15
+ log "github.com/sirupsen/logrus"
14
16
. "github.com/smartystreets/goconvey/convey"
15
17
)
16
18
@@ -130,7 +132,7 @@ func Test_ServicesStateWithData(t *testing.T) {
130
132
So (state .Servers [anotherHostname ].Services [svc .ID ], ShouldNotBeNil )
131
133
})
132
134
133
- Convey ("Doesn't merge a stale service " , func () {
135
+ Convey ("Doesn't merge an update that is older than what we have " , func () {
134
136
state .AddServiceEntry (svc )
135
137
136
138
staleService := service.Service {
@@ -152,6 +154,26 @@ func Test_ServicesStateWithData(t *testing.T) {
152
154
ShouldEqual , "101deadbeef" )
153
155
})
154
156
157
+ Convey ("Doesn't merge an update that is past the tombstone lifespan" , func () {
158
+ staleService := service.Service {
159
+ ID : "deadbeef123" ,
160
+ Name : "stale_service" ,
161
+ Image : "stale" ,
162
+ Created : baseTime ,
163
+ Hostname : anotherHostname ,
164
+ Updated : baseTime .Add (0 - 1 * time .Minute ).Add (0 - TOMBSTONE_LIFESPAN ),
165
+ Status : service .ALIVE ,
166
+ }
167
+
168
+ capture := LogCapture (func () {
169
+ state .AddServiceEntry (staleService )
170
+ })
171
+
172
+ _ , ok := state .Servers [anotherHostname ]
173
+ So (ok , ShouldBeFalse )
174
+ So (capture , ShouldContainSubstring , "Dropping stale service received on gossip" )
175
+ })
176
+
155
177
Convey ("Updates the LastUpdated time for the server" , func () {
156
178
newDate := svc .Updated .AddDate (0 , 0 , 5 )
157
179
svc .Updated = newDate
@@ -723,7 +745,7 @@ func Test_ClusterMembershipManagement(t *testing.T) {
723
745
724
746
func Test_DecodeStream (t * testing.T ) {
725
747
Convey ("Test decoding stream" , t , func () {
726
- serv := service.Service {ID : "007" , Name : "api" , Hostname : "some-aws-host" , Status : 1 }
748
+ serv := service.Service {ID : "007" , Name : "api" , Hostname : "some-aws-host" , Status : 1 , Updated : time . Now (). UTC () }
727
749
state := NewServicesState ()
728
750
state .AddServiceEntry (serv )
729
751
@@ -841,3 +863,13 @@ func ShouldMatch(actual interface{}, expected ...interface{}) string {
841
863
842
864
return ""
843
865
}
866
+
867
+ // LogCapture logs for async testing where we can't get a nice handle on thigns
868
+ func LogCapture (fn func ()) string {
869
+ capture := & bytes.Buffer {}
870
+ log .SetOutput (capture )
871
+ fn ()
872
+ log .SetOutput (os .Stdout )
873
+
874
+ return capture .String ()
875
+ }
0 commit comments