golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/ack_delay_test.go (about)

     1  // Copyright 2023 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  //go:build go1.21
     6  
     7  package quic
     8  
     9  import (
    10  	"math"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestAckDelayFromDuration(t *testing.T) {
    16  	for _, test := range []struct {
    17  		d                time.Duration
    18  		ackDelayExponent uint8
    19  		want             unscaledAckDelay
    20  	}{{
    21  		d:                8 * time.Microsecond,
    22  		ackDelayExponent: 3,
    23  		want:             1,
    24  	}, {
    25  		d:                1 * time.Nanosecond,
    26  		ackDelayExponent: 3,
    27  		want:             0, // rounds to zero
    28  	}, {
    29  		d:                3 * (1 << 20) * time.Microsecond,
    30  		ackDelayExponent: 20,
    31  		want:             3,
    32  	}} {
    33  		got := unscaledAckDelayFromDuration(test.d, test.ackDelayExponent)
    34  		if got != test.want {
    35  			t.Errorf("unscaledAckDelayFromDuration(%v, %v) = %v, want %v",
    36  				test.d, test.ackDelayExponent, got, test.want)
    37  		}
    38  	}
    39  }
    40  
    41  func TestAckDelayToDuration(t *testing.T) {
    42  	for _, test := range []struct {
    43  		d                unscaledAckDelay
    44  		ackDelayExponent uint8
    45  		want             time.Duration
    46  	}{{
    47  		d:                1,
    48  		ackDelayExponent: 3,
    49  		want:             8 * time.Microsecond,
    50  	}, {
    51  		d:                0,
    52  		ackDelayExponent: 3,
    53  		want:             0,
    54  	}, {
    55  		d:                3,
    56  		ackDelayExponent: 20,
    57  		want:             3 * (1 << 20) * time.Microsecond,
    58  	}, {
    59  		d:                math.MaxInt64 / 1000,
    60  		ackDelayExponent: 0,
    61  		want:             (math.MaxInt64 / 1000) * time.Microsecond,
    62  	}, {
    63  		d:                (math.MaxInt64 / 1000) + 1,
    64  		ackDelayExponent: 0,
    65  		want:             0, // return 0 on overflow
    66  	}, {
    67  		d:                math.MaxInt64 / 1000 / 8,
    68  		ackDelayExponent: 3,
    69  		want:             (math.MaxInt64 / 1000 / 8) * 8 * time.Microsecond,
    70  	}, {
    71  		d:                (math.MaxInt64 / 1000 / 8) + 1,
    72  		ackDelayExponent: 3,
    73  		want:             0, // return 0 on overflow
    74  	}} {
    75  		got := test.d.Duration(test.ackDelayExponent)
    76  		if got != test.want {
    77  			t.Errorf("unscaledAckDelay(%v).Duration(%v) = %v, want %v",
    78  				test.d, test.ackDelayExponent, int64(got), int64(test.want))
    79  		}
    80  	}
    81  }