Skip to content

Commit

Permalink
fix median time unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
whitefen committed Nov 28, 2018
1 parent 189e380 commit 1595186
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions util/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package util
import (
"github.com/copernet/copernicus/conf"
"strconv"
"sync"
"testing"
"time"
)

var testMtx sync.Mutex

func TestGetTime(t *testing.T) {
testMtx.Lock()
defer testMtx.Unlock()
mockTime = 0
nowTime := time.Now().Unix()
actualTime := GetTimeSec()
Expand All @@ -22,13 +27,17 @@ func TestGetTime(t *testing.T) {
if nowTime == actualTime {
t.Errorf("the nowTime:%d should not equal actualTime:%d", nowTime, actualTime)
}

mockTime = 0
}

func TestGetAdjustedTimeSec(t *testing.T) {
GetAdjustedTimeSec()
}

func TestGetTimeMicroSec(t *testing.T) {
testMtx.Lock()
defer testMtx.Unlock()
mockTime = 100
actualTime := GetTimeMicroSec()
if actualTime != mockTime*1000*1000 {
Expand Down Expand Up @@ -60,6 +69,9 @@ func TestSetMockTime(t *testing.T) {

// TestMedianTime tests the medianTime implementation.
func TestMedianTime(t *testing.T) {
testMtx.Lock()
defer testMtx.Unlock()
SetMockTime(0)
conf.Cfg = conf.InitConfig([]string{})
tests := []struct {
in []int64
Expand Down Expand Up @@ -140,8 +152,8 @@ func TestMedianTime(t *testing.T) {
// second, allow a fudge factor to compensate.
adjustedTime := GetAdjustedTimeSec()
now := time.Now().Unix()
wantTime := now + filter.getOffsetSec()
wantTime2 := now + filter.getOffsetSec() - 1
wantTime := now + GetGlobalMedianTime().getOffsetSec()
wantTime2 := now + GetGlobalMedianTime().getOffsetSec() - 1
if adjustedTime != wantTime && adjustedTime != wantTime2 {
t.Errorf("AdjustedTime #%d: unexpected result -- got %v, want %v or %v",
i, adjustedTime, wantTime, wantTime2)
Expand Down

0 comments on commit 1595186

Please sign in to comment.