github.com/dubbogo/gost@v1.14.0/time/timer_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  // Package gxtime encapsulates some golang.time functions
    19  package gxtime
    20  
    21  import (
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func TestGetTimerWheel(t *testing.T) {
    27  	InitDefaultTimerWheel()
    28  	tw := GetDefaultTimerWheel()
    29  	if tw == nil {
    30  		t.Fatal("default time wheel is nil")
    31  	}
    32  }
    33  
    34  func TestUnix2Time(t *testing.T) {
    35  	now := time.Now()
    36  	nowUnix := Time2Unix(now)
    37  	tm := Unix2Time(nowUnix)
    38  	// time->unix有精度损失,所以只能在秒级进行比较
    39  	if tm.Unix() != now.Unix() {
    40  		t.Fatalf("@now:%#v, tm:%#v", now, tm)
    41  	}
    42  }
    43  
    44  func TestUnixNano2Time(t *testing.T) {
    45  	now := time.Now()
    46  	nowUnix := Time2UnixNano(now)
    47  	tm := UnixNano2Time(nowUnix)
    48  	if tm.UnixNano() != now.UnixNano() {
    49  		t.Fatalf("@now:%#v, tm:%#v", now, tm)
    50  	}
    51  }
    52  
    53  func TestGetEndTime(t *testing.T) {
    54  	dayEndTime := GetEndTime("day")
    55  	t.Logf("today end time %q", dayEndTime)
    56  
    57  	weekEndTime := GetEndTime("week")
    58  	t.Logf("this week end time %q", weekEndTime)
    59  
    60  	monthEndTime := GetEndTime("month")
    61  	t.Logf("this month end time %q", monthEndTime)
    62  
    63  	yearEndTime := GetEndTime("year")
    64  	t.Logf("this year end time %q", yearEndTime)
    65  }
    66  
    67  func TestTimerConsumerGoroutine(t *testing.T) {
    68  	InitDefaultTimerWheel()
    69  	time.Sleep(time.Second * 2)
    70  	println("sleep over")
    71  	<-defaultTimerWheel.After(1)
    72  }