golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/sent_val_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 "testing"
    10  
    11  func TestSentVal(t *testing.T) {
    12  	for _, test := range []struct {
    13  		name              string
    14  		f                 func(*sentVal)
    15  		wantIsSet         bool
    16  		wantShouldSend    bool
    17  		wantIsReceived    bool
    18  		wantShouldSendPTO bool
    19  	}{{
    20  		name:              "zero value",
    21  		f:                 func(*sentVal) {},
    22  		wantIsSet:         false,
    23  		wantShouldSend:    false,
    24  		wantShouldSendPTO: false,
    25  		wantIsReceived:    false,
    26  	}, {
    27  		name:              "v.set()",
    28  		f:                 (*sentVal).set,
    29  		wantIsSet:         true,
    30  		wantShouldSend:    true,
    31  		wantShouldSendPTO: true,
    32  		wantIsReceived:    false,
    33  	}, {
    34  		name: "v.setSent(0)",
    35  		f: func(v *sentVal) {
    36  			v.setSent(0)
    37  		},
    38  		wantIsSet:         true,
    39  		wantShouldSend:    false,
    40  		wantShouldSendPTO: true,
    41  		wantIsReceived:    false,
    42  	}, {
    43  		name: "sent.set()",
    44  		f: func(v *sentVal) {
    45  			v.setSent(0)
    46  			v.set()
    47  		},
    48  		wantIsSet:         true,
    49  		wantShouldSend:    false,
    50  		wantShouldSendPTO: true,
    51  		wantIsReceived:    false,
    52  	}, {
    53  		name: "sent.setUnsent()",
    54  		f: func(v *sentVal) {
    55  			v.setSent(0)
    56  			v.setUnsent()
    57  		},
    58  		wantIsSet:         true,
    59  		wantShouldSend:    true,
    60  		wantShouldSendPTO: true,
    61  		wantIsReceived:    false,
    62  	}, {
    63  		name: "set.clear()",
    64  		f: func(v *sentVal) {
    65  			v.set()
    66  			v.clear()
    67  		},
    68  		wantIsSet:         false,
    69  		wantShouldSend:    false,
    70  		wantShouldSendPTO: false,
    71  		wantIsReceived:    false,
    72  	}, {
    73  		name:              "v.setReceived()",
    74  		f:                 (*sentVal).setReceived,
    75  		wantIsSet:         true,
    76  		wantShouldSend:    false,
    77  		wantShouldSendPTO: false,
    78  		wantIsReceived:    true,
    79  	}, {
    80  		name: "v.ackOrLoss(!pnum, true)",
    81  		f: func(v *sentVal) {
    82  			v.setSent(1)
    83  			v.ackOrLoss(0, packetAcked) // ack different packet containing the val
    84  		},
    85  		wantIsSet:         true,
    86  		wantShouldSend:    false,
    87  		wantShouldSendPTO: false,
    88  		wantIsReceived:    true,
    89  	}, {
    90  		name: "v.ackOrLoss(!pnum, packetLost)",
    91  		f: func(v *sentVal) {
    92  			v.setSent(1)
    93  			v.ackOrLoss(0, packetLost) // lose different packet containing the val
    94  		},
    95  		wantIsSet:         true,
    96  		wantShouldSend:    false,
    97  		wantShouldSendPTO: true,
    98  		wantIsReceived:    false,
    99  	}, {
   100  		name: "v.ackOrLoss(pnum, packetLost)",
   101  		f: func(v *sentVal) {
   102  			v.setSent(1)
   103  			v.ackOrLoss(1, packetLost) // lose same packet containing the val
   104  		},
   105  		wantIsSet:         true,
   106  		wantShouldSend:    true,
   107  		wantShouldSendPTO: true,
   108  		wantIsReceived:    false,
   109  	}, {
   110  		name: "v.ackLatestOrLoss(!pnum, packetAcked)",
   111  		f: func(v *sentVal) {
   112  			v.setSent(1)
   113  			v.ackLatestOrLoss(0, packetAcked) // ack different packet containing the val
   114  		},
   115  		wantIsSet:         true,
   116  		wantShouldSend:    false,
   117  		wantShouldSendPTO: true,
   118  		wantIsReceived:    false,
   119  	}, {
   120  		name: "v.ackLatestOrLoss(pnum, packetAcked)",
   121  		f: func(v *sentVal) {
   122  			v.setSent(1)
   123  			v.ackLatestOrLoss(1, packetAcked) // ack same packet containing the val
   124  		},
   125  		wantIsSet:         true,
   126  		wantShouldSend:    false,
   127  		wantShouldSendPTO: false,
   128  		wantIsReceived:    true,
   129  	}, {
   130  		name: "v.ackLatestOrLoss(!pnum, packetLost)",
   131  		f: func(v *sentVal) {
   132  			v.setSent(1)
   133  			v.ackLatestOrLoss(0, packetLost) // lose different packet containing the val
   134  		},
   135  		wantIsSet:         true,
   136  		wantShouldSend:    false,
   137  		wantShouldSendPTO: true,
   138  		wantIsReceived:    false,
   139  	}, {
   140  		name: "v.ackLatestOrLoss(pnum, packetLost)",
   141  		f: func(v *sentVal) {
   142  			v.setSent(1)
   143  			v.ackLatestOrLoss(1, packetLost) // lose same packet containing the val
   144  		},
   145  		wantIsSet:         true,
   146  		wantShouldSend:    true,
   147  		wantShouldSendPTO: true,
   148  		wantIsReceived:    false,
   149  	}} {
   150  		var v sentVal
   151  		test.f(&v)
   152  		if got, want := v.isSet(), test.wantIsSet; got != want {
   153  			t.Errorf("%v: v.isSet() = %v, want %v", test.name, got, want)
   154  		}
   155  		if got, want := v.shouldSend(), test.wantShouldSend; got != want {
   156  			t.Errorf("%v: v.shouldSend() = %v, want %v", test.name, got, want)
   157  		}
   158  		if got, want := v.shouldSendPTO(false), test.wantShouldSend; got != want {
   159  			t.Errorf("%v: v.shouldSendPTO(false) = %v, want %v", test.name, got, want)
   160  		}
   161  		if got, want := v.shouldSendPTO(true), test.wantShouldSendPTO; got != want {
   162  			t.Errorf("%v: v.shouldSendPTO(true) = %v, want %v", test.name, got, want)
   163  		}
   164  		if got, want := v.isReceived(), test.wantIsReceived; got != want {
   165  			t.Errorf("%v: v.isReceived() = %v, want %v", test.name, got, want)
   166  		}
   167  	}
   168  }