Skip to content

Commit

Permalink
Pull request 2099: improve validator test
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 3a7b8fd
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Dec 6 18:36:08 2023 +0300

    dnsforward: imp code

commit 9751bf5
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Dec 6 18:27:59 2023 +0300

    dnsforward: imp validator test
  • Loading branch information
EugeneOne1 committed Dec 6, 2023
1 parent a3be6a9 commit 083abaa
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions internal/dnsforward/upstreams_internal_test.go
Expand Up @@ -4,7 +4,6 @@ import (
"net"
"net/url"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -164,13 +163,16 @@ func TestUpstreamConfigValidator(t *testing.T) {
}

func TestUpstreamConfigValidator_Check_once(t *testing.T) {
reqs := atomic.Int32{}
reset := func() { reqs.Store(0) }
type signal = struct{}

reqCh := make(chan signal)
hdlr := dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
pt := testutil.PanicT{}

err := w.WriteMsg(new(dns.Msg).SetReply(m))
require.NoError(testutil.PanicT{}, err)
reqs.Add(1)
require.NoError(pt, err)

testutil.RequireSend(pt, reqCh, signal{}, testTimeout)
})

addr := (&url.URL{
Expand All @@ -179,6 +181,10 @@ func TestUpstreamConfigValidator_Check_once(t *testing.T) {
}).String()
twoAddrs := strings.Join([]string{addr, addr}, " ")

wantStatus := map[string]string{
addr: "OK",
}

testCases := []struct {
name string
ups []string
Expand All @@ -195,15 +201,23 @@ func TestUpstreamConfigValidator_Check_once(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Cleanup(reset)

cv := newUpstreamConfigValidator(tc.ups, nil, nil, &upstream.Options{
Timeout: 100 * time.Millisecond,
Timeout: testTimeout,
})
cv.check()
cv.close()

assert.Equal(t, int32(1), reqs.Load())
go func() {
cv.check()
testutil.RequireSend(testutil.PanicT{}, reqCh, signal{}, testTimeout)
}()

// Wait for the only request to be sent.
testutil.RequireReceive(t, reqCh, testTimeout)

// Wait for the check to finish.
testutil.RequireReceive(t, reqCh, testTimeout)

cv.close()
require.Equal(t, wantStatus, cv.status())
})
}
}

0 comments on commit 083abaa

Please sign in to comment.