Skip to content

Commit ca30e58

Browse files
committed
Add unit tests for timeout errors
1 parent 92ef78d commit ca30e58

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

queuelistener/listener_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/zalando/skipper/logging/loggingtest"
1314
"github.com/zalando/skipper/metrics/metricstest"
1415
)
1516

@@ -21,12 +22,20 @@ type testConnection struct {
2122
type testListener struct {
2223
sync.Mutex
2324
closed bool
25+
failNextTimeout bool
2426
fail bool
2527
connsBeforeFail int
2628
addr net.Addr
2729
conns chan *testConnection
2830
}
2931

32+
type testError struct{}
33+
34+
var errTimeout testError
35+
36+
func (err testError) Error() string { return "test error" }
37+
func (err testError) Timeout() bool { return false }
38+
3039
func (c *testConnection) Read([]byte) (int, error) { return 0, nil }
3140
func (c *testConnection) Write([]byte) (int, error) { return 0, nil }
3241
func (c *testConnection) LocalAddr() net.Addr { return nil }
@@ -50,6 +59,11 @@ func (c *testConnection) isClosed() bool {
5059

5160
func (l *testListener) Accept() (net.Conn, error) {
5261

62+
if l.failNextTimeout {
63+
l.failNextTimeout = false
64+
return nil, errTimeout
65+
}
66+
5367
if l.fail {
5468
return nil, errors.New("listener error")
5569
}
@@ -339,6 +353,25 @@ func TestInterface(t *testing.T) {
339353
}
340354
})
341355

356+
t.Run("wrapped listener returns timeout error, logs and retries", func(t *testing.T) {
357+
log := loggingtest.New()
358+
l, err := listenWith(&testListener{failNextTimeout: true}, Options{
359+
Log: log,
360+
})
361+
if err != nil {
362+
t.Fatal(err)
363+
}
364+
defer l.Close()
365+
conn, err := l.Accept()
366+
if err != nil {
367+
t.Fatal(err)
368+
}
369+
defer conn.Close()
370+
if err := log.WaitFor(errTimeout.Error(), 120*time.Millisecond); err != nil {
371+
t.Error("failed to log timeout error")
372+
}
373+
})
374+
342375
t.Run("wrapped permanently fails, returns queued connections and the error", func(t *testing.T) {
343376
m := &metricstest.MockMetrics{}
344377
l, err := listenWith(&testListener{connsBeforeFail: 3}, Options{
@@ -947,6 +980,26 @@ func TestTeardown(t *testing.T) {
947980

948981
func TestMonitoring(t *testing.T) {
949982

983+
t.Run("logs the timeout errors", func(t *testing.T) {
984+
log := loggingtest.New()
985+
l, err := listenWith(&testListener{failNextTimeout: true}, Options{
986+
Log: log,
987+
})
988+
989+
if err != nil {
990+
t.Fatal(err)
991+
}
992+
defer l.Close()
993+
conn, err := l.Accept()
994+
if err != nil {
995+
t.Fatal(err)
996+
}
997+
defer conn.Close()
998+
if err := log.WaitFor(errTimeout.Error(), 120*time.Millisecond); err != nil {
999+
t.Error("failed to log timeout error")
1000+
}
1001+
})
1002+
9501003
t.Run("updates the gauges for the concurrency and the queue size, measures accept latency", func(t *testing.T) {
9511004
m := &metricstest.MockMetrics{}
9521005
l, err := listenWith(&testListener{}, Options{

0 commit comments

Comments
 (0)