github.com/wfusion/gofusion@v1.1.14/test/log/cases/rotate_test.go (about) 1 package cases 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "runtime" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/suite" 12 13 "github.com/wfusion/gofusion/common/utils" 14 "github.com/wfusion/gofusion/log" 15 16 testLog "github.com/wfusion/gofusion/test/log" 17 ) 18 19 func TestRotate(t *testing.T) { 20 testingSuite := &Rotate{Test: new(testLog.Test)} 21 testingSuite.Init(testingSuite) 22 suite.Run(t, testingSuite) 23 } 24 25 type Rotate struct { 26 *testLog.Test 27 } 28 29 func (t *Rotate) BeforeTest(suiteName, testName string) { 30 t.Catch(func() { 31 log.Info(context.Background(), "right before %s %s", suiteName, testName) 32 }) 33 } 34 35 func (t *Rotate) AfterTest(suiteName, testName string) { 36 t.Catch(func() { 37 log.Info(context.Background(), "right after %s %s", suiteName, testName) 38 }) 39 } 40 41 func (t *Rotate) TestRotateSize() { 42 t.Catch(func() { 43 // Given 44 msg := utils.RandomLetterAndNumber(1024 - 72) 45 logger := log.Use("rotate_size", log.AppName(t.AppName())) 46 47 // When 48 ctx, cancel := context.WithCancel(context.Background()) 49 defer cancel() 50 for i := 0; i < 10; i++ { 51 logger.Info(ctx, "%v:"+msg, i) 52 } 53 54 // Then 55 _, filename, _, ok := runtime.Caller(0) 56 t.Require().True(ok) 57 projectRoot := filepath.Dir(filename) 58 59 time.Sleep(time.Second) 60 matches, err := filepath.Glob(filepath.Join(projectRoot, "gofusion*.log")) 61 t.NoError(err) 62 t.Greater(len(matches), 1) 63 t.LessOrEqual(len(matches), 1+5) 64 for _, match := range matches { 65 fs, err := os.Stat(match) 66 t.NoError(err) 67 t.LessOrEqual(fs.Size(), int64(1024)) 68 t.NoError(os.Remove(match)) 69 } 70 }) 71 } 72 73 func (t *Rotate) TestRotateTime() { 74 t.Catch(func() { 75 // Given 76 msg := utils.RandomLetterAndNumber(1024 - 72) 77 logger := log.Use("rotate_time", log.AppName(t.AppName())) 78 79 // When 80 ctx, cancel := context.WithCancel(context.Background()) 81 defer cancel() 82 for i := 0; i < 3; i++ { 83 logger.Info(ctx, "%v:"+msg, i) 84 time.Sleep(time.Second) 85 } 86 87 // Then 88 _, filename, _, ok := runtime.Caller(0) 89 t.Require().True(ok) 90 projectRoot := filepath.Dir(filename) 91 92 time.Sleep(time.Second) 93 matches, err := filepath.Glob(filepath.Join(projectRoot, "gofusion*.log")) 94 t.NoError(err) 95 t.Equal(len(matches), 1+1) 96 for _, match := range matches { 97 fs, err := os.Stat(match) 98 t.NoError(err) 99 t.NotZero(fs.Size()) 100 t.NoError(os.Remove(match)) 101 } 102 }) 103 } 104 105 func (t *Rotate) TestRotateCount() { 106 t.Catch(func() { 107 // Given 108 msg := utils.RandomLetterAndNumber(1024 - 74) 109 logger := log.Use("rotate_count", log.AppName(t.AppName())) 110 111 // When 112 ctx, cancel := context.WithCancel(context.Background()) 113 defer cancel() 114 for i := 0; i < 100; i++ { 115 logger.Info(ctx, "%v:"+msg, i) 116 } 117 118 // Then 119 _, filename, _, ok := runtime.Caller(0) 120 t.Require().True(ok) 121 projectRoot := filepath.Dir(filename) 122 123 time.Sleep(time.Second) 124 matches, err := filepath.Glob(filepath.Join(projectRoot, "gofusion*.log")) 125 t.NoError(err) 126 t.LessOrEqual(len(matches), 1+1) 127 for _, match := range matches { 128 fs, err := os.Stat(match) 129 t.NoError(err) 130 t.LessOrEqual(fs.Size(), int64(1024)) 131 t.NoError(os.Remove(match)) 132 } 133 }) 134 }