Pull request 2099: improve validator test

Squashed commit of the following:

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

    dnsforward: imp code

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

    dnsforward: imp validator test
This commit is contained in:
Eugene Burkov 2023-12-06 18:56:14 +03:00
parent a3be6a9c19
commit 083abaac09
1 changed files with 25 additions and 11 deletions

View File

@ -4,7 +4,6 @@ import (
"net" "net"
"net/url" "net/url"
"strings" "strings"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -164,13 +163,16 @@ func TestUpstreamConfigValidator(t *testing.T) {
} }
func TestUpstreamConfigValidator_Check_once(t *testing.T) { func TestUpstreamConfigValidator_Check_once(t *testing.T) {
reqs := atomic.Int32{} type signal = struct{}
reset := func() { reqs.Store(0) }
reqCh := make(chan signal)
hdlr := dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) { hdlr := dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
pt := testutil.PanicT{}
err := w.WriteMsg(new(dns.Msg).SetReply(m)) err := w.WriteMsg(new(dns.Msg).SetReply(m))
require.NoError(testutil.PanicT{}, err) require.NoError(pt, err)
reqs.Add(1)
testutil.RequireSend(pt, reqCh, signal{}, testTimeout)
}) })
addr := (&url.URL{ addr := (&url.URL{
@ -179,6 +181,10 @@ func TestUpstreamConfigValidator_Check_once(t *testing.T) {
}).String() }).String()
twoAddrs := strings.Join([]string{addr, addr}, " ") twoAddrs := strings.Join([]string{addr, addr}, " ")
wantStatus := map[string]string{
addr: "OK",
}
testCases := []struct { testCases := []struct {
name string name string
ups []string ups []string
@ -195,15 +201,23 @@ func TestUpstreamConfigValidator_Check_once(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
t.Cleanup(reset)
cv := newUpstreamConfigValidator(tc.ups, nil, nil, &upstream.Options{ 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())
}) })
} }
} }