github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/p2p/nat/nat_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package nat 18 19 import ( 20 "bytes" 21 "net" 22 "testing" 23 "time" 24 ) 25 26 // This test checks that autodisc doesn't hang and returns 27 // consistent results when multiple goroutines call its methods 28 // concurrently. 29 func TestAutoDiscRace(t *testing.T) { 30 ad := startautodisc("thing", func() Interface { 31 time.Sleep(500 * time.Millisecond) 32 return extIP{33, 44, 55, 66} 33 }) 34 35 // Spawn a few concurrent calls to ad.ExternalIP. 36 type rval struct { 37 ip net.IP 38 err error 39 } 40 results := make(chan rval, 50) 41 for i := 0; i < cap(results); i++ { 42 go func() { 43 ip, err := ad.ExternalIP() 44 results <- rval{ip, err} 45 }() 46 } 47 48 // Check that they all return the correct result within the deadline. 49 deadline := time.After(2 * time.Second) 50 for i := 0; i < cap(results); i++ { 51 select { 52 case <-deadline: 53 t.Fatal("deadline exceeded") 54 case rval := <-results: 55 if rval.err != nil { 56 t.Errorf("result %d: unexpected error: %v", i, rval.err) 57 } 58 wantIP := net.IP{33, 44, 55, 66} 59 if !bytes.Equal(rval.ip, wantIP) { 60 t.Errorf("result %d: got IP %v, want %v", i, rval.ip, wantIP) 61 } 62 } 63 } 64 }