trpc.group/trpc-go/trpc-go@v1.0.3/log/rollwriter/roll_writer_windows_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  //go:build windows
    15  // +build windows
    16  
    17  package rollwriter
    18  
    19  import (
    20  	"io/fs"
    21  	"log"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestRollWriterResume(t *testing.T) {
    32  	logDir, logName := t.TempDir(), "test_trpc.log"
    33  	if _, err := os.Stat(logDir); os.IsNotExist(err) {
    34  		require.Nil(t, os.Mkdir(logDir, os.ModeDir))
    35  	}
    36  
    37  	createAndLog := func() {
    38  		w, err := NewRollWriter(filepath.Join(logDir, logName),
    39  			WithMaxSize(1),
    40  			WithMaxAge(1),
    41  			WithMaxBackups(20),
    42  		)
    43  		require.Nil(t, err)
    44  		log.SetOutput(w)
    45  		const testTimes = 40000
    46  		for i := 0; i < testTimes; i++ {
    47  			log.Printf("this is a test log: %d\n", i)
    48  		}
    49  		time.Sleep(time.Millisecond)
    50  		require.Nil(t, w.Close())
    51  	}
    52  
    53  	createAndLog()
    54  	createAndLog()
    55  
    56  	var tmpCnt int
    57  	require.Nil(t, filepath.WalkDir(logDir, func(path string, d fs.DirEntry, err error) error {
    58  		if d.IsDir() {
    59  			return nil
    60  		}
    61  		if strings.Contains(filepath.Base(path), "tmp-") {
    62  			tmpCnt++
    63  		}
    64  		return nil
    65  	}))
    66  	require.Less(t, tmpCnt, 2)
    67  }
    68  
    69  func TestRollWriterBackupExistingNonlinkFile(t *testing.T) {
    70  	logDir, logName := t.TempDir(), "test_trpc.log"
    71  	if _, err := os.Stat(logDir); os.IsNotExist(err) {
    72  		require.Nil(t, os.Mkdir(logDir, os.ModeDir))
    73  	}
    74  
    75  	f, err := os.Create(filepath.Join(logDir, logName))
    76  	require.Nil(t, err)
    77  	require.Nil(t, f.Close())
    78  
    79  	w, err := NewRollWriter(filepath.Join(logDir, logName),
    80  		WithMaxSize(1),
    81  		WithMaxAge(1),
    82  		WithMaxBackups(20),
    83  	)
    84  	require.Nil(t, err)
    85  	log.SetOutput(w)
    86  	const testTimes = 40000
    87  	for i := 0; i < testTimes; i++ {
    88  		log.Printf("this is a test log: %d\n", i)
    89  	}
    90  	time.Sleep(time.Millisecond)
    91  	require.Nil(t, w.Close())
    92  
    93  	var bkCnt int
    94  	require.Nil(t, filepath.WalkDir(logDir, func(path string, d fs.DirEntry, err error) error {
    95  		if d.IsDir() {
    96  			return nil
    97  		}
    98  		if strings.HasPrefix(filepath.Base(path), "bk-") {
    99  			bkCnt++
   100  		}
   101  		return nil
   102  	}))
   103  	require.Equal(t, 1, bkCnt)
   104  }