Files
Lucas ManningandgVisor bot 1437270d71 Add a few small cleanups to tcp timers.
First is to have the timer save its callback function rather than calling
AfterFunc and Stop immediately. AfterFunc spawns two goroutines under the
hood and Stop cancels them, so this avoids a couple scheduler interactions.

Second is to unify maybeFailTimerHandler and timerHandler. These two functions
do the same thing, the former just handles errors. We can easily modify the
functions passed to timerHandler to just return nil errors and only use one
function.

Last is renaming isZero to isUninitialized, which IMO more accurately describes
what the function is checking.

PiperOrigin-RevId: 605708542
2024-02-09 12:57:45 -08:00

51 lines
1.3 KiB
Go

// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tcp
import (
"testing"
"time"
"gvisor.dev/gvisor/pkg/sleep"
"gvisor.dev/gvisor/pkg/tcpip/faketime"
)
func TestCleanup(t *testing.T) {
const (
timerDurationSeconds = 2
isAssertedTimeoutSeconds = timerDurationSeconds + 1
)
clock := faketime.NewManualClock()
tmr := timer{}
w := sleep.Waker{}
tmr.init(clock, w.Assert)
tmr.enable(timerDurationSeconds * time.Second)
tmr.cleanup()
if !tmr.isUninitialized() {
t.Errorf("got tmr.isUninitialized = false, want = true")
}
// The waker should not be asserted.
for i := 0; i < isAssertedTimeoutSeconds; i++ {
clock.Advance(time.Second)
if w.IsAsserted() {
t.Fatalf("waker asserted unexpectedly")
}
}
}