aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2020-10-30 10:27:04 +0100
committerGravatar GitHub <noreply@github.com> 2020-10-30 10:27:04 +0100
commitc840caf1ef77d8f86ee7d11f644e0d6ea42c469a (patch)
tree8fd3b097e41d1b04f737acfe92a7e81afe5db201 /plugin
parentbc0115d71f15aacc04d9ce9e8ce148b31d151800 (diff)
downloadcoredns-c840caf1ef77d8f86ee7d11f644e0d6ea42c469a.tar.gz
coredns-c840caf1ef77d8f86ee7d11f644e0d6ea42c469a.tar.zst
coredns-c840caf1ef77d8f86ee7d11f644e0d6ea42c469a.zip
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>
Diffstat (limited to 'plugin')
-rw-r--r--plugin/errors/errors_test.go187
-rw-r--r--plugin/file/reload_test.go6
-rw-r--r--plugin/file/setup.go16
-rw-r--r--plugin/forward/forward.go2
-rw-r--r--plugin/forward/health.go9
-rw-r--r--plugin/forward/health_test.go101
-rw-r--r--plugin/forward/persistent.go3
-rw-r--r--plugin/forward/proxy.go3
-rw-r--r--plugin/metrics/metrics_test.go10
-rw-r--r--plugin/sign/signer_test.go5
10 files changed, 71 insertions, 271 deletions
diff --git a/plugin/errors/errors_test.go b/plugin/errors/errors_test.go
index 8299fc333..9563b7323 100644
--- a/plugin/errors/errors_test.go
+++ b/plugin/errors/errors_test.go
@@ -6,11 +6,8 @@ import (
"errors"
"fmt"
golog "log"
- "math/rand"
"regexp"
- "strconv"
"strings"
- "sync"
"sync/atomic"
"testing"
"time"
@@ -196,190 +193,6 @@ func TestStop(t *testing.T) {
}
}
-const (
- pattern0 = "failed"
- pattern1 = "down$"
-)
-
-func TestServeDNSConcurrent(t *testing.T) {
- buf := bytes.NewBuffer(nil)
- golog.SetOutput(buf)
-
- eg := newErrorGenerator()
-
- h := &errorHandler{
- patterns: []*pattern{
- {
- period: 3 * time.Nanosecond,
- pattern: regexp.MustCompile(pattern0),
- },
- {
- period: 2 * time.Nanosecond,
- pattern: regexp.MustCompile(pattern1),
- },
- },
- Next: eg,
- }
-
- ctx := context.TODO()
- r := new(dns.Msg)
- w := &test.ResponseWriter{}
-
- var wg sync.WaitGroup
- runnersCnt := 9
- runner := func() {
- defer wg.Done()
- for eg.progress() < 100 {
- h.ServeDNS(ctx, w, r)
- }
- }
-
- wg.Add(runnersCnt)
- for ; runnersCnt > 0; runnersCnt-- {
- go runner()
- }
-
- stopCalled := false
- for eg.progress() < 100 {
- if !stopCalled && eg.progress() > 50 {
- h.stop()
- stopCalled = true
- }
- h.ServeDNS(ctx, w, r)
- }
- if !stopCalled {
- h.stop()
- }
-
- wg.Wait()
- time.Sleep(time.Millisecond)
-
- rep := makeReport(t, buf)
-
- if rep.c[Err0] == 0 {
- t.Errorf("Err0 was never consolidated")
- }
- if rep.c[Err1] == 0 {
- t.Errorf("Err1 was never consolidated")
- }
- if rep.c[Err0]+rep.r[Err0] != ErrCnt0 {
- t.Errorf("Unexpected Err0 count, expected %d, consolidated %d and reported %d",
- ErrCnt0, rep.c[Err0], rep.r[Err0])
- }
- if rep.c[Err1]+rep.r[Err1] != ErrCnt1 {
- t.Errorf("Unexpected Err1 count, expected %d, consolidated %d and reported %d",
- ErrCnt1, rep.c[Err1], rep.r[Err1])
- }
- if rep.r[Err2] != ErrCnt2 {
- t.Errorf("Unexpected Err2 count, expected %d, reported %d",
- ErrCnt2, rep.r[Err2])
- }
- if rep.r[Err3] != ErrCnt3 {
- t.Errorf("Unexpected Err3 count, expected %d, reported %d",
- ErrCnt3, rep.r[Err3])
- }
-}
-
-// error generator
-const (
- Err0 = iota
- Err1
- Err2
- Err3
-
- ErrStr0 = "failed to connect"
- ErrStr1 = "upstream is down"
- ErrStr2 = "access denied"
- ErrStr3 = "yet another error"
-
- ErrCnt0 = 120000
- ErrCnt1 = 130000
- ErrCnt2 = 150000
- ErrCnt3 = 100000
-)
-
-type errorBunch struct {
- cnt int32
- err error
-}
-
-type errorGenerator struct {
- errors [4]errorBunch
- totalCnt int32
- totalLim int32
-}
-
-func newErrorGenerator() *errorGenerator {
- rand.Seed(time.Now().UnixNano())
-
- return &errorGenerator{
- errors: [4]errorBunch{
- {ErrCnt0, fmt.Errorf(ErrStr0)},
- {ErrCnt1, fmt.Errorf(ErrStr1)},
- {ErrCnt2, fmt.Errorf(ErrStr2)},
- {ErrCnt3, fmt.Errorf(ErrStr3)},
- },
- totalLim: ErrCnt0 + ErrCnt1 + ErrCnt2 + ErrCnt3,
- }
-}
-
-// returns progress as a value from [0..100] range
-func (sh *errorGenerator) progress() int32 {
- return atomic.LoadInt32(&sh.totalCnt) * 100 / sh.totalLim
-}
-
-func (sh *errorGenerator) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
- i := rand.Int()
- for c := 0; c < 4; c++ {
- errInd := (i + c) & 3
- if atomic.AddInt32(&sh.errors[errInd].cnt, -1) >= 0 {
- atomic.AddInt32(&sh.totalCnt, 1)
- return 0, sh.errors[errInd].err
- }
- atomic.AddInt32(&sh.errors[errInd].cnt, 1)
- }
- return 0, nil
-}
-
-func (sh *errorGenerator) Name() string { return "errorGenerator" }
-
-// error report
-type errorReport struct {
- c [4]uint32
- r [4]uint32
-}
-
-func makeReport(t *testing.T, b *bytes.Buffer) errorReport {
- logDump := b.String()
- r := errorReport{}
-
- re := regexp.MustCompile(`(\d+) errors like '(.*)' occurred`)
- m := re.FindAllStringSubmatch(logDump, -1)
- for _, sub := range m {
- ind := 0
- switch sub[2] {
- case pattern0:
- ind = Err0
- case pattern1:
- ind = Err1
- default:
- t.Fatalf("Unknown pattern found in log: %s", sub[0])
- }
- n, err := strconv.ParseUint(sub[1], 10, 32)
- if err != nil {
- t.Fatalf("Invalid number found in log: %s", sub[0])
- }
- r.c[ind] += uint32(n)
- }
- t.Logf("%d consolidated messages found", len(m))
-
- r.r[Err0] = uint32(strings.Count(logDump, ErrStr0))
- r.r[Err1] = uint32(strings.Count(logDump, ErrStr1))
- r.r[Err2] = uint32(strings.Count(logDump, ErrStr2))
- r.r[Err3] = uint32(strings.Count(logDump, ErrStr3))
- return r
-}
-
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
diff --git a/plugin/file/reload_test.go b/plugin/file/reload_test.go
index 0c644d484..5d712f342 100644
--- a/plugin/file/reload_test.go
+++ b/plugin/file/reload_test.go
@@ -30,9 +30,9 @@ func TestZoneReload(t *testing.T) {
t.Fatalf("Failed to parse zone: %s", err)
}
- z.ReloadInterval = 500 * time.Millisecond
+ z.ReloadInterval = 10 * time.Millisecond
z.Reload(&transfer.Transfer{})
- time.Sleep(time.Second)
+ time.Sleep(20 * time.Millisecond)
ctx := context.TODO()
r := new(dns.Msg)
@@ -60,7 +60,7 @@ func TestZoneReload(t *testing.T) {
t.Fatalf("Failed to write new zone data: %s", err)
}
// Could still be racy, but we need to wait a bit for the event to be seen
- time.Sleep(1 * time.Second)
+ time.Sleep(30 * time.Millisecond)
rrs, err = z.ApexIfDefined()
if err != nil {
diff --git a/plugin/file/setup.go b/plugin/file/setup.go
index ed19aedfe..c5ff4c203 100644
--- a/plugin/file/setup.go
+++ b/plugin/file/setup.go
@@ -28,9 +28,11 @@ func setup(c *caddy.Controller) error {
return nil
}
f.transfer = t.(*transfer.Transfer) // if found this must be OK.
- for _, n := range zones.Names {
- f.transfer.Notify(n)
- }
+ go func() {
+ for _, n := range zones.Names {
+ f.transfer.Notify(n)
+ }
+ }()
return nil
})
@@ -39,9 +41,11 @@ func setup(c *caddy.Controller) error {
if t == nil {
return nil
}
- for _, n := range zones.Names {
- f.transfer.Notify(n)
- }
+ go func() {
+ for _, n := range zones.Names {
+ f.transfer.Notify(n)
+ }
+ }()
return nil
})
diff --git a/plugin/forward/forward.go b/plugin/forward/forward.go
index e9a180cb6..eff8f7030 100644
--- a/plugin/forward/forward.go
+++ b/plugin/forward/forward.go
@@ -229,4 +229,4 @@ type options struct {
hcRecursionDesired bool
}
-const defaultTimeout = 5 * time.Second
+var defaultTimeout = 5 * time.Second
diff --git a/plugin/forward/health.go b/plugin/forward/health.go
index 2b59039a6..fcd3df200 100644
--- a/plugin/forward/health.go
+++ b/plugin/forward/health.go
@@ -24,14 +24,19 @@ type dnsHc struct {
recursionDesired bool
}
+var (
+ hcReadTimeout = 1 * time.Second
+ hcWriteTimeout = 1 * time.Second
+)
+
// NewHealthChecker returns a new HealthChecker based on transport.
func NewHealthChecker(trans string, recursionDesired bool) HealthChecker {
switch trans {
case transport.DNS, transport.TLS:
c := new(dns.Client)
c.Net = "udp"
- c.ReadTimeout = 1 * time.Second
- c.WriteTimeout = 1 * time.Second
+ c.ReadTimeout = hcReadTimeout
+ c.WriteTimeout = hcWriteTimeout
return &dnsHc{c: c, recursionDesired: recursionDesired}
}
diff --git a/plugin/forward/health_test.go b/plugin/forward/health_test.go
index 7dd0c337a..88a96e803 100644
--- a/plugin/forward/health_test.go
+++ b/plugin/forward/health_test.go
@@ -14,7 +14,11 @@ import (
)
func TestHealth(t *testing.T) {
- const expected = 1
+ hcReadTimeout = 10 * time.Millisecond
+ hcWriteTimeout = 10 * time.Millisecond
+ readTimeout = 10 * time.Millisecond
+ defaultTimeout = 10 * time.Millisecond
+
i := uint32(0)
q := uint32(0)
s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
@@ -41,15 +45,19 @@ func TestHealth(t *testing.T) {
f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
- time.Sleep(1 * time.Second)
+ time.Sleep(20 * time.Millisecond)
i1 := atomic.LoadUint32(&i)
- if i1 != expected {
- t.Errorf("Expected number of health checks with RecursionDesired==true to be %d, got %d", expected, i1)
+ if i1 != 1 {
+ t.Errorf("Expected number of health checks with RecursionDesired==true to be %d, got %d", 1, i1)
}
}
func TestHealthNoRecursion(t *testing.T) {
- const expected = 1
+ hcReadTimeout = 10 * time.Millisecond
+ readTimeout = 10 * time.Millisecond
+ defaultTimeout = 10 * time.Millisecond
+ hcWriteTimeout = 10 * time.Millisecond
+
i := uint32(0)
q := uint32(0)
s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
@@ -77,65 +85,25 @@ func TestHealthNoRecursion(t *testing.T) {
f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
- time.Sleep(1 * time.Second)
+ time.Sleep(20 * time.Millisecond)
i1 := atomic.LoadUint32(&i)
- if i1 != expected {
- t.Errorf("Expected number of health checks with RecursionDesired==false to be %d, got %d", expected, i1)
+ if i1 != 1 {
+ t.Errorf("Expected number of health checks with RecursionDesired==false to be %d, got %d", 1, i1)
}
}
func TestHealthTimeout(t *testing.T) {
- const expected = 1
- i := uint32(0)
- q := uint32(0)
- s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
- if r.Question[0].Name == "." {
- // health check, answer
- atomic.AddUint32(&i, 1)
- ret := new(dns.Msg)
- ret.SetReply(r)
- w.WriteMsg(ret)
- return
- }
- if atomic.LoadUint32(&q) == 0 { //drop only first query
- atomic.AddUint32(&q, 1)
- return
- }
- ret := new(dns.Msg)
- ret.SetReply(r)
- w.WriteMsg(ret)
- })
- defer s.Close()
-
- p := NewProxy(s.Addr, transport.DNS)
- f := New()
- f.SetProxy(p)
- defer f.OnShutdown()
-
- req := new(dns.Msg)
- req.SetQuestion("example.org.", dns.TypeA)
+ hcReadTimeout = 10 * time.Millisecond
+ hcWriteTimeout = 10 * time.Millisecond
+ readTimeout = 10 * time.Millisecond
+ defaultTimeout = 10 * time.Millisecond
- f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
-
- time.Sleep(1 * time.Second)
- i1 := atomic.LoadUint32(&i)
- if i1 != expected {
- t.Errorf("Expected number of health checks to be %d, got %d", expected, i1)
- }
-}
-
-func TestHealthFailTwice(t *testing.T) {
- const expected = 2
i := uint32(0)
q := uint32(0)
s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
if r.Question[0].Name == "." {
+ // health check, answer
atomic.AddUint32(&i, 1)
- i1 := atomic.LoadUint32(&i)
- // Timeout health until we get the second one
- if i1 < 2 {
- return
- }
ret := new(dns.Msg)
ret.SetReply(r)
w.WriteMsg(ret)
@@ -161,14 +129,20 @@ func TestHealthFailTwice(t *testing.T) {
f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
- time.Sleep(3 * time.Second)
+ time.Sleep(20 * time.Millisecond)
i1 := atomic.LoadUint32(&i)
- if i1 != expected {
- t.Errorf("Expected number of health checks to be %d, got %d", expected, i1)
+ if i1 != 1 {
+ t.Errorf("Expected number of health checks to be %d, got %d", 1, i1)
}
}
func TestHealthMaxFails(t *testing.T) {
+ hcReadTimeout = 10 * time.Millisecond
+ hcWriteTimeout = 10 * time.Millisecond
+ readTimeout = 10 * time.Millisecond
+ defaultTimeout = 10 * time.Millisecond
+ hcInterval = 10 * time.Millisecond
+
s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
// timeout
})
@@ -185,7 +159,7 @@ func TestHealthMaxFails(t *testing.T) {
f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
- time.Sleep(readTimeout + 1*time.Second)
+ time.Sleep(100 * time.Millisecond)
fails := atomic.LoadUint32(&p.fails)
if !p.Down(f.maxfails) {
t.Errorf("Expected Proxy fails to be greater than %d, got %d", f.maxfails, fails)
@@ -193,7 +167,12 @@ func TestHealthMaxFails(t *testing.T) {
}
func TestHealthNoMaxFails(t *testing.T) {
- const expected = 0
+ hcReadTimeout = 10 * time.Millisecond
+ hcWriteTimeout = 10 * time.Millisecond
+ readTimeout = 10 * time.Millisecond
+ defaultTimeout = 10 * time.Millisecond
+ hcInterval = 10 * time.Millisecond
+
i := uint32(0)
s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
if r.Question[0].Name == "." {
@@ -217,9 +196,9 @@ func TestHealthNoMaxFails(t *testing.T) {
f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
- time.Sleep(1 * time.Second)
+ time.Sleep(20 * time.Millisecond)
i1 := atomic.LoadUint32(&i)
- if i1 != expected {
- t.Errorf("Expected number of health checks to be %d, got %d", expected, i1)
+ if i1 != 0 {
+ t.Errorf("Expected number of health checks to be %d, got %d", 0, i1)
}
}
diff --git a/plugin/forward/persistent.go b/plugin/forward/persistent.go
index b4a1ce16e..95d08e1e1 100644
--- a/plugin/forward/persistent.go
+++ b/plugin/forward/persistent.go
@@ -152,7 +152,10 @@ const (
defaultExpire = 10 * time.Second
minDialTimeout = 1 * time.Second
maxDialTimeout = 30 * time.Second
+)
+// Make a var for minimizing this value in tests.
+var (
// Some resolves might take quite a while, usually (cached) responses are fast. Set to 2s to give us some time to retry a different upstream.
readTimeout = 2 * time.Second
)
diff --git a/plugin/forward/proxy.go b/plugin/forward/proxy.go
index 2fe0f3839..80cb581f9 100644
--- a/plugin/forward/proxy.go
+++ b/plugin/forward/proxy.go
@@ -77,5 +77,6 @@ func (p *Proxy) start(duration time.Duration) {
const (
maxTimeout = 2 * time.Second
- hcInterval = 500 * time.Millisecond
)
+
+var hcInterval = 500 * time.Millisecond
diff --git a/plugin/metrics/metrics_test.go b/plugin/metrics/metrics_test.go
index 1f63a20b7..bd72bf1f4 100644
--- a/plugin/metrics/metrics_test.go
+++ b/plugin/metrics/metrics_test.go
@@ -30,25 +30,25 @@ func TestMetrics(t *testing.T) {
// This all works because 1 bucket (1 zone, 1 type)
{
next: test.NextHandler(dns.RcodeSuccess, nil),
- qname: "example.org",
+ qname: "example.org.",
metric: "coredns_dns_requests_total",
expectedValue: "1",
},
{
next: test.NextHandler(dns.RcodeSuccess, nil),
- qname: "example.org",
+ qname: "example.org.",
metric: "coredns_dns_requests_total",
expectedValue: "2",
},
{
next: test.NextHandler(dns.RcodeSuccess, nil),
- qname: "example.org",
+ qname: "example.org.",
metric: "coredns_dns_requests_total",
expectedValue: "3",
},
{
next: test.NextHandler(dns.RcodeSuccess, nil),
- qname: "example.org",
+ qname: "example.org.",
metric: "coredns_dns_responses_total",
expectedValue: "4",
},
@@ -61,7 +61,7 @@ func TestMetrics(t *testing.T) {
if tc.qtype == 0 {
tc.qtype = dns.TypeA
}
- req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
+ req.SetQuestion(tc.qname, tc.qtype)
met.Next = tc.next
rec := dnstest.NewRecorder(&test.ResponseWriter{})
diff --git a/plugin/sign/signer_test.go b/plugin/sign/signer_test.go
index 869e99d96..c150bbf16 100644
--- a/plugin/sign/signer_test.go
+++ b/plugin/sign/signer_test.go
@@ -1,7 +1,6 @@
package sign
import (
- "bytes"
"io/ioutil"
"os"
"testing"
@@ -176,8 +175,4 @@ func TestSignDS(t *testing.T) {
if x := nsec[0].Header().Ttl; x != minttl {
t.Errorf("Expected no NSEC TTL to be %d for %s, got %d", minttl, "www.miek.nl.", x)
}
- // print zone on error
- buf := &bytes.Buffer{}
- write(buf, z)
- t.Logf("%s\n", buf)
}