gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/transport/tcp/timer_test.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tcp
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"gvisor.dev/gvisor/pkg/sleep"
    22  	"gvisor.dev/gvisor/pkg/tcpip/faketime"
    23  )
    24  
    25  func TestCleanup(t *testing.T) {
    26  	const (
    27  		timerDurationSeconds     = 2
    28  		isAssertedTimeoutSeconds = timerDurationSeconds + 1
    29  	)
    30  
    31  	clock := faketime.NewManualClock()
    32  
    33  	tmr := timer{}
    34  	w := sleep.Waker{}
    35  	tmr.init(clock, w.Assert)
    36  	tmr.enable(timerDurationSeconds * time.Second)
    37  	tmr.cleanup()
    38  
    39  	if !tmr.isUninitialized() {
    40  		t.Errorf("got tmr.isUninitialized = false, want = true")
    41  	}
    42  
    43  	// The waker should not be asserted.
    44  	for i := 0; i < isAssertedTimeoutSeconds; i++ {
    45  		clock.Advance(time.Second)
    46  		if w.IsAsserted() {
    47  			t.Fatalf("waker asserted unexpectedly")
    48  		}
    49  	}
    50  }