Skip to content

Commit

Permalink
dnsforward: imp validator test
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Dec 6, 2023
1 parent a3be6a9 commit 9751bf5
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions internal/dnsforward/upstreams_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"
"net/url"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -164,13 +163,14 @@ 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) {
err := w.WriteMsg(new(dns.Msg).SetReply(m))
require.NoError(testutil.PanicT{}, err)
reqs.Add(1)

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

addr := (&url.URL{
Expand All @@ -179,6 +179,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 +199,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 9751bf5

Please sign in to comment.