2020-09-04 04:01:53 +08:00
|
|
|
package common_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-05 05:29:38 +08:00
|
|
|
"errors"
|
2020-09-04 04:01:53 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-11-06 17:53:56 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2020-09-04 04:01:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSleep(test *testing.T) {
|
|
|
|
const dt = 50 * time.Millisecond
|
2021-12-23 05:54:41 +08:00
|
|
|
t := func(name string, ctx context.Context, expected error) {
|
2020-09-04 04:01:53 +08:00
|
|
|
test.Run(name, func(test *testing.T) {
|
2021-12-23 05:54:41 +08:00
|
|
|
err := common.Sleep(ctx, dt)
|
2021-12-05 05:29:38 +08:00
|
|
|
if !errors.Is(err, expected) {
|
2020-09-04 04:01:53 +08:00
|
|
|
test.Errorf("expected %v, got %v", expected, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:54:41 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
canceled, cancel := context.WithCancel(ctx)
|
2020-09-04 04:01:53 +08:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
t("background context", ctx, nil)
|
|
|
|
t("canceled context", canceled, context.Canceled)
|
|
|
|
}
|