github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/z_last_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  var testDNSFlood = flag.Bool("dnsflood", false, "whether to test dns query flooding")
    15  
    16  func TestDNSThreadLimit(t *testing.T) {
    17  	if !*testDNSFlood {
    18  		t.Skip("test disabled; use -dnsflood to enable")
    19  	}
    20  
    21  	const N = 10000
    22  	c := make(chan int, N)
    23  	for i := 0; i < N; i++ {
    24  		go func(i int) {
    25  			LookupIP(fmt.Sprintf("%d.net-test.golang.org", i))
    26  			c <- 1
    27  		}(i)
    28  	}
    29  	// Don't bother waiting for the stragglers; stop at 0.9 N.
    30  	for i := 0; i < N*9/10; i++ {
    31  		if i%100 == 0 {
    32  			//println("TestDNSThreadLimit:", i)
    33  		}
    34  		<-c
    35  	}
    36  
    37  	// If we're still here, it worked.
    38  }
    39  
    40  func TestLookupIPDeadline(t *testing.T) {
    41  	if !*testDNSFlood {
    42  		t.Skip("test disabled; use -dnsflood to enable")
    43  	}
    44  
    45  	const N = 5000
    46  	const timeout = 3 * time.Second
    47  	c := make(chan error, 2*N)
    48  	for i := 0; i < N; i++ {
    49  		name := fmt.Sprintf("%d.net-test.golang.org", i)
    50  		go func() {
    51  			_, err := lookupIPDeadline(name, time.Now().Add(timeout/2))
    52  			c <- err
    53  		}()
    54  		go func() {
    55  			_, err := lookupIPDeadline(name, time.Now().Add(timeout))
    56  			c <- err
    57  		}()
    58  	}
    59  	qstats := struct {
    60  		succeeded, failed         int
    61  		timeout, temporary, other int
    62  		unknown                   int
    63  	}{}
    64  	deadline := time.After(timeout + time.Second)
    65  	for i := 0; i < 2*N; i++ {
    66  		select {
    67  		case <-deadline:
    68  			t.Fatal("deadline exceeded")
    69  		case err := <-c:
    70  			switch err := err.(type) {
    71  			case nil:
    72  				qstats.succeeded++
    73  			case Error:
    74  				qstats.failed++
    75  				if err.Timeout() {
    76  					qstats.timeout++
    77  				}
    78  				if err.Temporary() {
    79  					qstats.temporary++
    80  				}
    81  				if !err.Timeout() && !err.Temporary() {
    82  					qstats.other++
    83  				}
    84  			default:
    85  				qstats.failed++
    86  				qstats.unknown++
    87  			}
    88  		}
    89  	}
    90  
    91  	// A high volume of DNS queries for sub-domain of golang.org
    92  	// would be coordinated by authoritative or recursive server,
    93  	// or stub resolver which implements query-response rate
    94  	// limitation, so we can expect some query successes and more
    95  	// failures including timeout, temporary and other here.
    96  	// As a rule, unknown must not be shown but it might possibly
    97  	// happen due to issue 4856 for now.
    98  	t.Logf("%v succeeded, %v failed (%v timeout, %v temporary, %v other, %v unknown)", qstats.succeeded, qstats.failed, qstats.timeout, qstats.temporary, qstats.other, qstats.unknown)
    99  }