Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filters/scheduler: wait for spans finishing in lifo errors test #1964

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion filters/scheduler/lifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"net/http/httptest"
"net/url"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aryszka/jobqueue"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
Expand Down Expand Up @@ -301,6 +303,38 @@ func TestNewLIFO(t *testing.T) {
}
}

type testTracer struct {
*mocktracer.MockTracer
spans int32
}

func (t *testTracer) Reset() {
atomic.StoreInt32(&t.spans, 0)
t.MockTracer.Reset()
}

func (t *testTracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
atomic.AddInt32(&t.spans, 1)
return t.MockTracer.StartSpan(operationName, opts...)
}

func (t *testTracer) FinishedSpans() []*mocktracer.MockSpan {
timeout := time.After(1 * time.Second)
retry := time.NewTicker(100 * time.Millisecond)
defer retry.Stop()
for {
finished := t.MockTracer.FinishedSpans()
Copy link
Member Author

@AlexanderYastrebov AlexanderYastrebov Feb 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opentracing/opentracing-go#239 adds UnfinishedSpans() but the library is deprecated opentracing/specification#163 so it seems there will be no new release

if len(finished) == int(atomic.LoadInt32(&t.spans)) {
return finished
}
select {
case <-retry.C:
case <-timeout:
return nil
}
}
}

func TestLifoErrors(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
time.Sleep(time.Second)
Expand Down Expand Up @@ -334,7 +368,7 @@ func TestLifoErrors(t *testing.T) {

<-rt.FirstLoad()

tracer := mocktracer.New()
tracer := &testTracer{MockTracer: mocktracer.New()}
pr := proxy.WithParams(proxy.Params{
Routing: rt,
OpenTracing: &proxy.OpenTracingParams{Tracer: tracer},
Expand Down Expand Up @@ -366,6 +400,8 @@ func TestLifoErrors(t *testing.T) {
503: 8, // were refused due to full queue
}, codes)

reg.UpdateMetrics()

metrics.WithCounters(func(counters map[string]int64) {
assert.Equal(t, int64(7), counters["lifo.aroute.error.timeout"])
assert.Equal(t, int64(8), counters["lifo.aroute.error.full"])
Expand Down
6 changes: 6 additions & 0 deletions scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ func (r *Registry) updateMetrics() {
}
}

func (r *Registry) UpdateMetrics() {
if r.options.Metrics != nil {
r.updateMetrics()
}
}

// Close closes the registry, including graceful tearing down the stored queues.
func (r *Registry) Close() {
r.mu.Lock()
Expand Down