github.com/maypok86/otter@v1.2.1/internal/unixtime/unixtime_test.go (about)

     1  // Copyright (c) 2023 Alexey Mayshev. All rights reserved.
     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 unixtime
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  func TestNow(t *testing.T) {
    23  	Start()
    24  
    25  	got := Now()
    26  	if got != 0 {
    27  		t.Fatalf("unexpected time since program start; got %d; want %d", got, 0)
    28  	}
    29  
    30  	time.Sleep(3 * time.Second)
    31  
    32  	got = Now()
    33  	if got != 2 && got != 3 {
    34  		t.Fatalf("unexpected time since program start; got %d; want %d", got, 3)
    35  	}
    36  
    37  	Stop()
    38  
    39  	time.Sleep(3 * time.Second)
    40  
    41  	if Now()-got > 1 {
    42  		t.Fatal("timer should have stopped")
    43  	}
    44  }