coredns/plugin/errors/errors_test.go
Miek Gieben c840caf1ef
Speed up testing (#4239)
* Speed up testing

* make notification run in the background, this recudes the test_readme
time from 18s to 0.10s
* reduce time for zone reload

* TestServeDNSConcurrent remove entirely. This took a whopping 58s for
  ... ? A few minutes staring didn't reveal wth it is actually testing.
  Making values smaller revealed race conditions in the tests. Remove
  entirely.

* Move many interval values to variables so we can reset them to short
  values for the tests.

* test_large_axfr: make the zone smaller. The number used 64K has no
  rational, make it 64/10 to speed up.
* TestProxyThreeWay: use client with shorter timeout

A few random tidbits in other tests.

Total time saved: 177s (almost 3m) - which makes it worthwhile again to
run the test locally:

this branch:

~~~
ok  	github.com/coredns/coredns/test	10.437s
cd plugin; time go t ./...
5,51s user 7,51s system 11,15s elapsed 744%CPU (
~~~

master:

~~~
ok  	github.com/coredns/coredns/test	35.252s
cd plugin; time go t ./...
157,64s user 15,39s system 50,05s elapsed 345%CPU ()
~~~
tests/ -25s
plugins/ -40s

This brings the total on 20s, and another 10s can be saved by fixing
dnstapio. Moving this to 5s would be even better, but 10s is also nice.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Also 0.01

Signed-off-by: Miek Gieben <miek@miek.nl>
2020-10-30 10:27:04 +01:00

200 lines
4.4 KiB
Go

package errors
import (
"bytes"
"context"
"errors"
"fmt"
golog "log"
"regexp"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
)
func TestErrors(t *testing.T) {
buf := bytes.Buffer{}
golog.SetOutput(&buf)
em := errorHandler{}
testErr := errors.New("test error")
tests := []struct {
next plugin.Handler
expectedCode int
expectedLog string
expectedErr error
}{
{
next: genErrorHandler(dns.RcodeSuccess, nil),
expectedCode: dns.RcodeSuccess,
expectedLog: "",
expectedErr: nil,
},
{
next: genErrorHandler(dns.RcodeNotAuth, testErr),
expectedCode: dns.RcodeNotAuth,
expectedLog: fmt.Sprintf("%d %s: %v\n", dns.RcodeNotAuth, "example.org. A", testErr),
expectedErr: testErr,
},
}
ctx := context.TODO()
req := new(dns.Msg)
req.SetQuestion("example.org.", dns.TypeA)
for i, tc := range tests {
em.Next = tc.next
buf.Reset()
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := em.ServeDNS(ctx, rec, req)
if err != tc.expectedErr {
t.Errorf("Test %d: Expected error %v, but got %v",
i, tc.expectedErr, err)
}
if code != tc.expectedCode {
t.Errorf("Test %d: Expected status code %d, but got %d",
i, tc.expectedCode, code)
}
if log := buf.String(); !strings.Contains(log, tc.expectedLog) {
t.Errorf("Test %d: Expected log %q, but got %q",
i, tc.expectedLog, log)
}
}
}
func TestLogPattern(t *testing.T) {
buf := bytes.Buffer{}
golog.SetOutput(&buf)
h := &errorHandler{
patterns: []*pattern{{
count: 4,
period: 2 * time.Second,
pattern: regexp.MustCompile("^error.*!$"),
}},
}
h.logPattern(0)
expLog := "4 errors like '^error.*!$' occurred in last 2s"
if log := buf.String(); !strings.Contains(log, expLog) {
t.Errorf("Expected log %q, but got %q", expLog, log)
}
}
func TestInc(t *testing.T) {
h := &errorHandler{
stopFlag: 1,
patterns: []*pattern{{
period: 2 * time.Second,
pattern: regexp.MustCompile("^error.*!$"),
}},
}
ret := h.inc(0)
if ret {
t.Error("Unexpected return value, expected false, actual true")
}
h.stopFlag = 0
ret = h.inc(0)
if !ret {
t.Error("Unexpected return value, expected true, actual false")
}
expCnt := uint32(1)
actCnt := atomic.LoadUint32(&h.patterns[0].count)
if actCnt != expCnt {
t.Errorf("Unexpected 'count', expected %d, actual %d", expCnt, actCnt)
}
t1 := h.patterns[0].timer()
if t1 == nil {
t.Error("Unexpected 'timer', expected not nil")
}
ret = h.inc(0)
if !ret {
t.Error("Unexpected return value, expected true, actual false")
}
expCnt = uint32(2)
actCnt = atomic.LoadUint32(&h.patterns[0].count)
if actCnt != expCnt {
t.Errorf("Unexpected 'count', expected %d, actual %d", expCnt, actCnt)
}
t2 := h.patterns[0].timer()
if t2 != t1 {
t.Error("Unexpected 'timer', expected the same")
}
ret = t1.Stop()
if !ret {
t.Error("Timer was unexpectedly stopped before")
}
ret = t2.Stop()
if ret {
t.Error("Timer was unexpectedly not stopped before")
}
}
func TestStop(t *testing.T) {
buf := bytes.Buffer{}
golog.SetOutput(&buf)
h := &errorHandler{
patterns: []*pattern{{
period: 2 * time.Second,
pattern: regexp.MustCompile("^error.*!$"),
}},
}
h.inc(0)
h.inc(0)
h.inc(0)
expCnt := uint32(3)
actCnt := atomic.LoadUint32(&h.patterns[0].count)
if actCnt != expCnt {
t.Fatalf("Unexpected initial 'count', expected %d, actual %d", expCnt, actCnt)
}
h.stop()
expCnt = uint32(0)
actCnt = atomic.LoadUint32(&h.patterns[0].count)
if actCnt != expCnt {
t.Errorf("Unexpected 'count', expected %d, actual %d", expCnt, actCnt)
}
expStop := uint32(1)
actStop := h.stopFlag
if actStop != expStop {
t.Errorf("Unexpected 'stop', expected %d, actual %d", expStop, actStop)
}
t1 := h.patterns[0].timer()
if t1 == nil {
t.Error("Unexpected 'timer', expected not nil")
} else if t1.Stop() {
t.Error("Timer was unexpectedly not stopped before")
}
expLog := "3 errors like '^error.*!$' occurred in last 2s"
if log := buf.String(); !strings.Contains(log, expLog) {
t.Errorf("Expected log %q, but got %q", expLog, log)
}
}
func genErrorHandler(rcode int, err error) plugin.Handler {
return plugin.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
return rcode, err
})
}