Skip to content

Commit

Permalink
Do not use internal endpointregistry.lastSeen field in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Zavodskikh <roman.zavodskikh@zalando.de>
  • Loading branch information
Roman Zavodskikh committed Sep 14, 2023
1 parent 4b2d6eb commit ecd00c7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
4 changes: 3 additions & 1 deletion routing/endpointregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (e *entry) InflightRequests() int64 {

type EndpointRegistry struct {
lastSeen map[string]time.Time
now func() time.Time

mu sync.Mutex

Expand All @@ -45,7 +46,7 @@ type RegistryOptions struct {
}

func (r *EndpointRegistry) Do(routes []*Route) []*Route {
now := time.Now()
now := r.now()

for _, route := range routes {
if route.BackendType == eskip.LBBackend {
Expand Down Expand Up @@ -76,6 +77,7 @@ func NewEndpointRegistry(o RegistryOptions) *EndpointRegistry {
return &EndpointRegistry{
data: map[string]*entry{},
lastSeen: map[string]time.Time{},
now: time.Now,
}
}

Expand Down
61 changes: 44 additions & 17 deletions routing/endpointregistry_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routing
package routing_test

import (
"fmt"
Expand All @@ -9,18 +9,19 @@ import (

"github.com/stretchr/testify/assert"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/routing"
)

func TestEmptyRegistry(t *testing.T) {
r := NewEndpointRegistry(RegistryOptions{})
r := routing.NewEndpointRegistry(routing.RegistryOptions{})
m := r.GetMetrics("some key")

assert.Equal(t, time.Time{}, m.DetectedTime())
assert.Equal(t, int64(0), m.InflightRequests())
}

func TestSetAndGet(t *testing.T) {
r := NewEndpointRegistry(RegistryOptions{})
r := routing.NewEndpointRegistry(routing.RegistryOptions{})

mBefore := r.GetMetrics("some key")
r.IncInflightRequest("some key")
Expand All @@ -39,7 +40,7 @@ func TestSetAndGet(t *testing.T) {
}

func TestSetAndGetAnotherKey(t *testing.T) {
r := NewEndpointRegistry(RegistryOptions{})
r := routing.NewEndpointRegistry(routing.RegistryOptions{})

r.IncInflightRequest("some key")
mToChange := r.GetMetrics("some key")
Expand All @@ -50,20 +51,46 @@ func TestSetAndGetAnotherKey(t *testing.T) {
}

func TestDoRemovesOldEntries(t *testing.T) {
route := &Route{LBEndpoints: []LBEndpoint{
{Host: "existing endpoint"},
}}
route.BackendType = eskip.LBBackend
r := routing.NewEndpointRegistry(routing.RegistryOptions{})

r := NewEndpointRegistry(RegistryOptions{})
routing.SetNow(r, func() time.Time {
return time.Date(2023, 9, 14, 0, 0, 0, 0, time.UTC)
})
route := &routing.Route{
LBEndpoints: []routing.LBEndpoint{
{Host: "endpoint1.test:80"},
{Host: "endpoint2.test:80"},
},
Route: eskip.Route{
BackendType: eskip.LBBackend,
},
}
r.Do([]*routing.Route{route})

mExist := r.GetMetrics("endpoint1.test:80")
mExistYet := r.GetMetrics("endpoint2.test:80")
assert.NotEqual(t, time.Time{}, mExist.DetectedTime())
assert.NotEqual(t, time.Time{}, mExistYet.DetectedTime())

r.IncInflightRequest("existing endpoint")
r.IncInflightRequest("removed endpoint")
r.lastSeen["removed endpoint"] = time.Now().Add(-lastSeenTimeout)
r.Do([]*Route{route})
r.IncInflightRequest("endpoint1.test:80")
r.IncInflightRequest("endpoint2.test:80")

routing.SetNow(r, func() time.Time {
return time.Date(2023, 9, 14, 0, 0, 0, 0, time.UTC).Add(routing.ExportLastSeenTimeout + time.Second)
})
route = &routing.Route{
LBEndpoints: []routing.LBEndpoint{
{Host: "endpoint1.test:80"},
},
Route: eskip.Route{
BackendType: eskip.LBBackend,
},
}
route.BackendType = eskip.LBBackend
r.Do([]*routing.Route{route})

mExist := r.GetMetrics("existing endpoint")
mRemoved := r.GetMetrics("removed endpoint")
mExist = r.GetMetrics("endpoint1.test:80")
mRemoved := r.GetMetrics("endpoint2.test:80")

assert.NotEqual(t, time.Time{}, mExist.DetectedTime())
assert.Equal(t, int64(1), mExist.InflightRequests())
Expand Down Expand Up @@ -103,7 +130,7 @@ func benchmarkIncInflightRequests(b *testing.B, name string, goroutines int) {
const mapSize int = 10000

b.Run(name, func(b *testing.B) {
r := NewEndpointRegistry(RegistryOptions{})
r := routing.NewEndpointRegistry(routing.RegistryOptions{})
for i := 1; i < mapSize; i++ {
r.IncInflightRequest(fmt.Sprintf("foo-%d", i))
}
Expand Down Expand Up @@ -139,7 +166,7 @@ func benchmarkGetInflightRequests(b *testing.B, name string, goroutines int) {
const mapSize int = 10000

b.Run(name, func(b *testing.B) {
r := NewEndpointRegistry(RegistryOptions{})
r := routing.NewEndpointRegistry(routing.RegistryOptions{})
for i := 1; i < mapSize; i++ {
r.IncInflightRequest(fmt.Sprintf("foo-%d", i))
}
Expand Down
7 changes: 7 additions & 0 deletions routing/export_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package routing

import "time"

var (
ExportProcessRouteDef = processRouteDef
ExportNewMatcher = newMatcher
ExportMatch = (*matcher).match
ExportProcessPredicates = processPredicates
ExportLastSeenTimeout = lastSeenTimeout
)

func SetNow(r *EndpointRegistry, now func() time.Time) {
r.now = now
}

0 comments on commit ecd00c7

Please sign in to comment.