Skip to content

Commit

Permalink
routesrv: fix flaky TestESkipBytesHandlerWithOldLastModified (#2531)
Browse files Browse the repository at this point in the history
* fix flaky test by mocking clock instead of using sleep.
* add tracing tag on route update

Follows up on #2526

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Aug 21, 2023
1 parent 8ad3fcd commit dbb456d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion routesrv/eskipbytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type eskipBytes struct {
mu sync.RWMutex

tracer ot.Tracer
now func() time.Time
}

// formatAndSet takes a slice of routes and stores them eskip-formatted
Expand All @@ -42,7 +43,7 @@ func (e *eskipBytes) formatAndSet(routes []*eskip.Route) (_ int, initialized boo

updated = !bytes.Equal(e.data, data)
if updated {
e.lastModified = time.Now()
e.lastModified = e.now()
e.data = data
e.etag = fmt.Sprintf(`"%x"`, sha256.Sum256(e.data))
e.count = len(routes)
Expand Down
7 changes: 7 additions & 0 deletions routesrv/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package routesrv

import "time"

func SetNow(rs *RouteServer, now func() time.Time) {
rs.poller.b.now = now
}
1 change: 1 addition & 0 deletions routesrv/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (p *poller) poll(wg *sync.WaitGroup) {
}
if updated {
logger.Info(LogRoutesUpdated)
span.SetTag("routes.updated", true)
routesUpdated.SetToCurrentTime()
}
span.SetTag("routes.count", routesCount)
Expand Down
2 changes: 1 addition & 1 deletion routesrv/routesrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func New(opts skipper.Options) (*RouteServer, error) {
return nil, err
}

b := &eskipBytes{tracer: tracer}
b := &eskipBytes{tracer: tracer, now: time.Now}
bs := &eskipBytesStatus{b: b}
handler := http.NewServeMux()
handler.Handle("/health", bs)
Expand Down
7 changes: 5 additions & 2 deletions routesrv/routesrv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func TestMain(m *testing.M) {
func TestNotInitializedRoutesAreNotServed(t *testing.T) {
defer tl.Reset()
ks, _ := newKubeServer(t)
defer ks.Close()

rs := newRouteServer(t, ks)

w := getRoutes(rs)
Expand Down Expand Up @@ -513,8 +515,9 @@ func TestESkipBytesHandlerWithOldLastModified(t *testing.T) {
lastModified := w1.Header().Get("Last-Modified")
header := map[string]string{"If-Modified-Since": lastModified}

// Last-Modified has a second precision so we need to wait a bit for it to change
time.Sleep(2 * time.Second)
// Last-Modified has a second precision so we need to wait a bit for it to change.
// Move clock forward instead of using time.Sleep that makes test flaky.
routesrv.SetNow(rs, func() time.Time { return time.Now().Add(2 * time.Second) })

// update the routes, which also updated the e.lastModified
handler.set(newKubeAPI(t, loadKubeYAML(t, "testdata/lb-target-single.yaml")))
Expand Down

0 comments on commit dbb456d

Please sign in to comment.