Skip to content

Commit

Permalink
util/systimemon: migrate test-infra to testify (pingcap#26633)
Browse files Browse the repository at this point in the history
  • Loading branch information
yedamao authored Jul 28, 2021
1 parent 583cdc8 commit c585a76
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
31 changes: 31 additions & 0 deletions util/systimemon/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package systimemon

import (
"testing"

"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()

opts := []goleak.Option{
goleak.IgnoreTopFunction("github.com/pingcap/tidb/util/systimemon.StartMonitor"),
}

goleak.VerifyTestMain(m, opts...)
}
2 changes: 1 addition & 1 deletion util/systimemon/systime_mon.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func StartMonitor(now func() time.Time, systimeErrHandler func(), successCallbac
logutil.BgLogger().Error("system time jump backward", zap.Int64("last", last))
systimeErrHandler()
}
// call sucessCallback per second.
// call successCallback per second.
tickCount++
if tickCount >= 10 {
tickCount = 0
Expand Down
25 changes: 9 additions & 16 deletions util/systimemon/systime_mon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,29 @@
package systimemon

import (
"sync/atomic"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)

func TestT(t *testing.T) {
TestingT(t)
}

func TestSystimeMonitor(t *testing.T) {
var jumpForward int32
t.Parallel()

trigged := false
errTriggered := atomic.NewBool(false)
nowTriggered := atomic.NewBool(false)
go StartMonitor(
func() time.Time {
if !trigged {
trigged = true
if !nowTriggered.Load() {
nowTriggered.Store(true)
return time.Now()
}

return time.Now().Add(-2 * time.Second)
}, func() {
atomic.StoreInt32(&jumpForward, 1)
errTriggered.Store(true)
}, func() {})

time.Sleep(1 * time.Second)

if atomic.LoadInt32(&jumpForward) != 1 {
t.Error("should detect time error")
}
require.Eventually(t, errTriggered.Load, time.Second, 10*time.Millisecond)
}

0 comments on commit c585a76

Please sign in to comment.