github.com/wfusion/gofusion@v1.1.14/test/cron/cases/log_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/robfig/cron/v3"
     9  	"github.com/stretchr/testify/suite"
    10  
    11  	"github.com/wfusion/gofusion/common/constant"
    12  	"github.com/wfusion/gofusion/log"
    13  	"github.com/wfusion/gofusion/log/customlogger"
    14  
    15  	testCron "github.com/wfusion/gofusion/test/cron"
    16  )
    17  
    18  func TestLog(t *testing.T) {
    19  	testingSuite := &Log{Test: new(testCron.Test)}
    20  	testingSuite.Init(testingSuite)
    21  	suite.Run(t, testingSuite)
    22  }
    23  
    24  type Log struct {
    25  	*testCron.Test
    26  }
    27  
    28  func (t *Log) BeforeTest(suiteName, testName string) {
    29  	t.Catch(func() {
    30  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    31  	})
    32  }
    33  
    34  func (t *Log) AfterTest(suiteName, testName string) {
    35  	t.Catch(func() {
    36  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    37  	})
    38  }
    39  
    40  func (t *Log) TestCustomLogger() {
    41  	t.Catch(func() {
    42  		i := 0
    43  		c := cron.New(
    44  			cron.WithSeconds(),
    45  			cron.WithChain(cron.Recover(cron.DefaultLogger), cron.SkipIfStillRunning(cron.DefaultLogger)),
    46  			cron.WithLocation(constant.DefaultLocation()),
    47  			cron.WithLogger(cron.VerbosePrintfLogger(customlogger.DefaultCronLogger())),
    48  		)
    49  		_, err := c.AddFunc("*/1 * * * * *", func() {
    50  			i++
    51  		})
    52  		t.NoError(err)
    53  		c.Start()
    54  		defer c.Stop()
    55  		time.Sleep(2 * time.Second)
    56  		t.Greater(i, 0)
    57  	})
    58  }