Skip to content

Commit

Permalink
refactor(tests): use a const timeout to simplify race avoidance
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Dec 1, 2023
1 parent 8c7b89c commit 8ad83d0
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
3 changes: 0 additions & 3 deletions resolver/parallel_best_resolver_test.go
Expand Up @@ -3,7 +3,6 @@ package resolver
import (
"context"
"strings"
"time"

"github.com/0xERR0R/blocky/config"
. "github.com/0xERR0R/blocky/helpertest"
Expand All @@ -16,8 +15,6 @@ import (

var _ = Describe("ParallelBestResolver", Label("parallelBestResolver"), func() {
const (
timeout = 50 * time.Millisecond

verifyUpstreams = true
noVerifyUpstreams = false
)
Expand Down
6 changes: 5 additions & 1 deletion resolver/resolver_suite_test.go
Expand Up @@ -13,6 +13,10 @@ import (
. "github.com/onsi/gomega"
)

const (
timeout = 50 * time.Millisecond
)

var defaultUpstreamsConfig config.Upstreams

func init() {
Expand All @@ -27,7 +31,7 @@ func init() {
}

// Shorter timeout for tests
defaultUpstreamsConfig.Timeout = config.Duration(50 * time.Millisecond)
defaultUpstreamsConfig.Timeout = config.Duration(timeout)
}

func TestResolver(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions resolver/strict_resolver_test.go
Expand Up @@ -164,7 +164,7 @@ var _ = Describe("StrictResolver", Label("strictResolver"), func() {
))
})
})
When("first upstream exceeds upstreamTimeout", func() {
When("first upstream times-out", func() {
BeforeEach(func() {
testUpstream1 = NewMockUDPUpstreamServer().WithAnswerFn(func(request *dns.Msg) (response *dns.Msg) {
response, err := util.NewMsgWithAnswer("example.com", 123, A, "123.124.122.1")
Expand All @@ -190,7 +190,7 @@ var _ = Describe("StrictResolver", Label("strictResolver"), func() {
))
})
})
When("all upstreams exceed upsteamTimeout", func() {
When("all upstreams timeout", func() {
JustBeforeEach(func() {
testUpstream1 = NewMockUDPUpstreamServer().WithAnswerFn(func(request *dns.Msg) (response *dns.Msg) {
response, err := util.NewMsgWithAnswer("example.com", 123, A, "123.124.122.1")
Expand Down
2 changes: 0 additions & 2 deletions resolver/upstream_resolver_test.go
Expand Up @@ -113,8 +113,6 @@ var _ = Describe("UpstreamResolver", Label("upstreamResolver"), func() {
var counter int32
var attemptsWithTimeout int32
BeforeEach(func() {
timeout := sutConfig.Timeout.ToDuration() // avoid data race

resolveFn := func(request *dns.Msg) *dns.Msg {
// timeout on first x attempts
if atomic.AddInt32(&counter, 1) <= atomic.LoadInt32(&attemptsWithTimeout) {
Expand Down

0 comments on commit 8ad83d0

Please sign in to comment.