github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/rootd/dns/connpool_test.go (about)

     1  //go:build linux
     2  
     3  package dns
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/miekg/dns"
    13  )
    14  
    15  func TestConnPoolConcurrency(t *testing.T) {
    16  	const (
    17  		TOTAL_THREADS       = 15
    18  		REQUESTS_PER_THREAD = 5
    19  		TIMEOUT_S           = 8
    20  	)
    21  	ctx := context.Background()
    22  	dc := &dns.Client{
    23  		Net:     "udp",
    24  		Timeout: TIMEOUT_S * time.Second,
    25  	}
    26  	pool, err := NewConnPool("8.8.8.8", 5)
    27  	if err != nil {
    28  		t.Log(err)
    29  		t.FailNow()
    30  	}
    31  	defer pool.Close()
    32  	errors := make(chan error)
    33  	wg := &sync.WaitGroup{}
    34  	wg.Add(TOTAL_THREADS)
    35  	for i := 0; i < TOTAL_THREADS; i++ {
    36  		go func(idx int) {
    37  			defer wg.Done()
    38  			for j := 0; j < REQUESTS_PER_THREAD; j++ {
    39  				msg := new(dns.Msg)
    40  				domain := fmt.Sprintf("dns-test-%d.preview.edgestack.me.", idx)
    41  				msg.SetQuestion(domain, dns.TypeMX)
    42  				ctx, cancel := context.WithTimeout(ctx, TIMEOUT_S*time.Second)
    43  				_, _, err := pool.Exchange(ctx, dc, msg)
    44  				cancel()
    45  				errors <- err
    46  			}
    47  		}(i)
    48  	}
    49  	go func() {
    50  		wg.Wait()
    51  		close(errors)
    52  	}()
    53  	for err := range errors {
    54  		if err != nil {
    55  			t.Error(err)
    56  		}
    57  	}
    58  }