github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/network/p2p/nat/nat_test.go (about)

     1  package nat
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestAutoDiscRace(t *testing.T) {
    10  	ad := startautodisc("thing", func() Interface {
    11  		time.Sleep(500 * time.Millisecond)
    12  		return extIP{33, 44, 55, 66}
    13  	})
    14  
    15  	type rval struct {
    16  		ip  net.IP
    17  		err error
    18  	}
    19  	results := make(chan rval, 50)
    20  	for i := 0; i < cap(results); i++ {
    21  		go func() {
    22  			ip, err := ad.ExternalIP()
    23  			results <- rval{ip, err}
    24  		}()
    25  	}
    26  
    27  	deadline := time.After(2 * time.Second)
    28  	for i := 0; i < cap(results); i++ {
    29  		select {
    30  		case <-deadline:
    31  			t.Fatal("deadline exceeded")
    32  		case rval := <-results:
    33  			if rval.err != nil {
    34  				t.Errorf("result %d: unexpected error: %v", i, rval.err)
    35  			}
    36  			wantIP := net.IP{33, 44, 55, 66}
    37  			if !rval.ip.Equal(wantIP) {
    38  				t.Errorf("result %d: got IP %v, want %v", i, rval.ip, wantIP)
    39  			}
    40  		}
    41  	}
    42  }