Skip to content

Commit

Permalink
tests: check InContainer() for TestCPUValue and `TestGetCgroupCPU…
Browse files Browse the repository at this point in the history
…()` test cases (#46736)
  • Loading branch information
Defined2014 authored Sep 7, 2023
1 parent 5981cc5 commit 670778e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions util/cgroup/cgroup_cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool {
}

func TestGetCgroupCPU(t *testing.T) {
// If it's not in the container,
// it will have less files and the test case will fail forever.
if !InContainer() {
t.Skip("Not in container, skip this test case.")
}
exit := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
Expand Down
1 change: 1 addition & 0 deletions util/cpu/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ go_test(
"//resourcemanager/scheduler",
"//resourcemanager/util",
"//testkit/testsetup",
"//util/cgroup",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//require",
"@org_uber_go_goleak//:goleak",
Expand Down
10 changes: 8 additions & 2 deletions util/cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/resourcemanager/scheduler"
"github.com/pingcap/tidb/resourcemanager/util"
"github.com/pingcap/tidb/util/cgroup"
"github.com/pingcap/tidb/util/cpu"
"github.com/stretchr/testify/require"
)

func TestCPUValue(t *testing.T) {
// If it's not in the container,
// it will have less files and the test case will fail forever.
if !cgroup.InContainer() {
t.Skip("Not in container, skip this test case.")
}
observer := cpu.NewCPUObserver()
exit := make(chan struct{})
var wg sync.WaitGroup
Expand All @@ -47,7 +53,7 @@ func TestCPUValue(t *testing.T) {
}
observer.Start()
for n := 0; n < 10; n++ {
time.Sleep(1 * time.Second)
time.Sleep(200 * time.Millisecond)
value, unsupported := cpu.GetCPUUsage()
require.False(t, unsupported)
require.Greater(t, value, 0.0)
Expand Down Expand Up @@ -82,7 +88,7 @@ func TestFailpointCPUValue(t *testing.T) {
}
observer.Start()
for n := 0; n < 10; n++ {
time.Sleep(1 * time.Second)
time.Sleep(200 * time.Millisecond)
value, unsupported := cpu.GetCPUUsage()
require.True(t, unsupported)
require.Equal(t, value, 0.0)
Expand Down

0 comments on commit 670778e

Please sign in to comment.