fix UT issue for package gcron (#1992)

This commit is contained in:
John Guo 2022-07-12 09:55:46 +08:00 committed by GitHub
parent 8ed57c6468
commit dd7caea910
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 39 deletions

View File

@ -7,12 +7,12 @@
package g
import (
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/internal/utils"
)
// SetDebug enables/disables the GoFrame internal logging manually.
// Note that this function is not concurrent safe, be aware of the DATA RACE,
// which means you should call this function in your boot but not the runtime.
func SetDebug(enabled bool) {
intlog.SetEnabled(enabled)
utils.SetDebugEnabled(enabled)
}

View File

@ -24,28 +24,10 @@ const (
stackFilterKey = "/internal/intlog"
)
var (
// isGFDebug marks whether printing GoFrame debug information.
isGFDebug = false
)
func init() {
isGFDebug = utils.IsDebugEnabled()
}
// SetEnabled enables/disables the internal logging manually.
// Note that this function is not concurrent safe, be aware of the DATA RACE.
func SetEnabled(enabled bool) {
// If they're the same, it does not write the `isGFDebug` but only reading operation.
if isGFDebug != enabled {
isGFDebug = enabled
}
}
// Print prints `v` with newline using fmt.Println.
// The parameter `v` can be multiple variables.
func Print(ctx context.Context, v ...interface{}) {
if !isGFDebug {
if !utils.IsDebugEnabled() {
return
}
doPrint(ctx, fmt.Sprint(v...), false)
@ -54,7 +36,7 @@ func Print(ctx context.Context, v ...interface{}) {
// Printf prints `v` with format `format` using fmt.Printf.
// The parameter `v` can be multiple variables.
func Printf(ctx context.Context, format string, v ...interface{}) {
if !isGFDebug {
if !utils.IsDebugEnabled() {
return
}
doPrint(ctx, fmt.Sprintf(format, v...), false)
@ -63,7 +45,7 @@ func Printf(ctx context.Context, format string, v ...interface{}) {
// Error prints `v` with newline using fmt.Println.
// The parameter `v` can be multiple variables.
func Error(ctx context.Context, v ...interface{}) {
if !isGFDebug {
if !utils.IsDebugEnabled() {
return
}
doPrint(ctx, fmt.Sprint(v...), true)
@ -71,7 +53,7 @@ func Error(ctx context.Context, v ...interface{}) {
// Errorf prints `v` with format `format` using fmt.Printf.
func Errorf(ctx context.Context, format string, v ...interface{}) {
if !isGFDebug {
if !utils.IsDebugEnabled() {
return
}
doPrint(ctx, fmt.Sprintf(format, v...), true)
@ -80,7 +62,7 @@ func Errorf(ctx context.Context, format string, v ...interface{}) {
// PrintFunc prints the output from function `f`.
// It only calls function `f` if debug mode is enabled.
func PrintFunc(ctx context.Context, f func() string) {
if !isGFDebug {
if !utils.IsDebugEnabled() {
return
}
s := f()
@ -93,7 +75,7 @@ func PrintFunc(ctx context.Context, f func() string) {
// ErrorFunc prints the output from function `f`.
// It only calls function `f` if debug mode is enabled.
func ErrorFunc(ctx context.Context, f func() string) {
if !isGFDebug {
if !utils.IsDebugEnabled() {
return
}
s := f()
@ -104,7 +86,7 @@ func ErrorFunc(ctx context.Context, f func() string) {
}
func doPrint(ctx context.Context, content string, stack bool) {
if !isGFDebug {
if !utils.IsDebugEnabled() {
return
}
buffer := bytes.NewBuffer(nil)

View File

@ -14,7 +14,6 @@ import (
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/utils"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/test/gtest"
)
@ -89,22 +88,21 @@ func TestCron_Remove(t *testing.T) {
}
func TestCron_Add_FixedPattern(t *testing.T) {
debug := utils.IsDebugEnabled()
utils.SetDebugEnabled(true)
defer func() {
utils.SetDebugEnabled(debug)
}()
gtest.C(t, func(t *gtest.T) {
var (
now = time.Now()
cron = gcron.New()
array = garray.New(true)
seconds = (now.Second() + 2) % 60
pattern = fmt.Sprintf(
`%d %d %d %d %d %s`,
seconds, now.Minute(), now.Hour(), now.Day(), now.Month(), now.Weekday().String(),
)
minutes = now.Minute()
seconds = now.Second() + 2
)
if seconds >= 60 {
seconds %= 60
minutes++
}
var pattern = fmt.Sprintf(
`%d %d %d %d %d %s`,
seconds, minutes, now.Hour(), now.Day(), now.Month(), now.Weekday().String(),
)
cron.SetLogger(g.Log())
g.Log().Debugf(ctx, `pattern: %s`, pattern)