github.com/mailru/activerecord@v1.12.2/pkg/iproto/netutil/dialer_test.go (about) 1 package netutil 2 3 import ( 4 "fmt" 5 "math" 6 "net" 7 "sync" 8 "testing" 9 "time" 10 11 "golang.org/x/net/context" 12 ) 13 14 // TestDialerDialLimits expects that Dialer will not reach limits and intervals 15 // of given configuration. 16 func TestDialerDialLimits(t *testing.T) { 17 t.Skip("Doesn't work on linux ") 18 srv := &server{} 19 20 d := &Dialer{ 21 Network: "tcp", 22 Addr: "localhost:80", 23 Logf: t.Logf, 24 25 NetDial: srv.dial, 26 27 LoopInterval: time.Millisecond * 25, 28 MaxLoopInterval: time.Millisecond * 100, 29 LoopTimeout: time.Millisecond * 500, 30 } 31 32 precision := float64(time.Millisecond * 10) 33 34 var ( 35 expCalls int 36 expInterval []time.Duration 37 growTime time.Duration 38 step time.Duration 39 ) 40 41 for step < d.MaxLoopInterval { 42 expCalls++ 43 growTime += step 44 expInterval = append(expInterval, step) 45 step += d.LoopInterval 46 } 47 48 expCalls += int((d.LoopTimeout - growTime) / d.MaxLoopInterval) 49 50 _, _ = d.Dial(context.Background()) 51 52 if n := len(srv.calls); n != expCalls { 53 t.Errorf("unexpected dial calls: %v; want %v", n, expCalls) 54 } 55 56 for i, c := range srv.calls { 57 var exp time.Duration 58 if i < len(expInterval) { 59 exp = expInterval[i] 60 } else { 61 exp = d.MaxLoopInterval 62 } 63 if act := c.delay; act != exp && math.Abs(float64(act-exp)) > precision { 64 t.Errorf("unexpected %dth attempt delay: %s; want %s", i, act, exp) 65 } 66 } 67 } 68 69 //nolint:unused 70 type dialCall struct { 71 time time.Time 72 delay time.Duration 73 network, addr string 74 } 75 76 //nolint:unused 77 type server struct { 78 mu sync.Mutex 79 calls []dialCall 80 } 81 82 //nolint:unused 83 func (s *server) dial(ctx context.Context, n, a string) (net.Conn, error) { 84 s.mu.Lock() 85 defer s.mu.Unlock() 86 87 now := time.Now() 88 89 var delay time.Duration 90 if n := len(s.calls); n > 0 { 91 delay = now.Sub(s.calls[n-1].time) 92 } 93 s.calls = append(s.calls, dialCall{now, delay, n, a}) 94 95 return nil, fmt.Errorf("noop") 96 }