github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/faketime/faketime_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 faketime_test
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/tcpip/faketime"
    22  )
    23  
    24  func TestManualClockAdvance(t *testing.T) {
    25  	const timeout = time.Millisecond
    26  	clock := faketime.NewManualClock()
    27  	start := clock.NowMonotonic()
    28  	clock.Advance(timeout)
    29  	if got, want := clock.NowMonotonic().Sub(start), timeout; got != want {
    30  		t.Errorf("got = %d, want = %d", got, want)
    31  	}
    32  }
    33  
    34  func TestManualClockAfterFunc(t *testing.T) {
    35  	const (
    36  		timeout1 = time.Millisecond     // timeout for counter1
    37  		timeout2 = 2 * time.Millisecond // timeout for counter2
    38  	)
    39  	tests := []struct {
    40  		name         string
    41  		advance      time.Duration
    42  		wantCounter1 int
    43  		wantCounter2 int
    44  	}{
    45  		{
    46  			name:         "before timeout1",
    47  			advance:      timeout1 - 1,
    48  			wantCounter1: 0,
    49  			wantCounter2: 0,
    50  		},
    51  		{
    52  			name:         "timeout1",
    53  			advance:      timeout1,
    54  			wantCounter1: 1,
    55  			wantCounter2: 0,
    56  		},
    57  		{
    58  			name:         "timeout2",
    59  			advance:      timeout2,
    60  			wantCounter1: 1,
    61  			wantCounter2: 1,
    62  		},
    63  		{
    64  			name:         "after timeout2",
    65  			advance:      timeout2 + 1,
    66  			wantCounter1: 1,
    67  			wantCounter2: 1,
    68  		},
    69  	}
    70  
    71  	for _, test := range tests {
    72  		t.Run(test.name, func(t *testing.T) {
    73  			clock := faketime.NewManualClock()
    74  			counter1 := 0
    75  			counter2 := 0
    76  			clock.AfterFunc(timeout1, func() {
    77  				counter1++
    78  			})
    79  			clock.AfterFunc(timeout2, func() {
    80  				counter2++
    81  			})
    82  			start := clock.NowMonotonic()
    83  			clock.Advance(test.advance)
    84  			if got, want := counter1, test.wantCounter1; got != want {
    85  				t.Errorf("got counter1 = %d, want = %d", got, want)
    86  			}
    87  			if got, want := counter2, test.wantCounter2; got != want {
    88  				t.Errorf("got counter2 = %d, want = %d", got, want)
    89  			}
    90  			if got, want := clock.NowMonotonic().Sub(start), test.advance; got != want {
    91  				t.Errorf("got elapsed = %d, want = %d", got, want)
    92  			}
    93  		})
    94  	}
    95  }