github.com/kaydxh/golang@v0.0.131/pkg/file-rotate/rotate_file_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package rotatefile_test
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"os"
    28  	"path/filepath"
    29  	"regexp"
    30  	"testing"
    31  	"time"
    32  
    33  	rotate_ "github.com/kaydxh/golang/pkg/file-rotate"
    34  	"github.com/sirupsen/logrus"
    35  )
    36  
    37  func getWdOrDie() string {
    38  	path, err := os.Getwd()
    39  	if err != nil {
    40  		logrus.Fatalf("failed to get wd, err: %v", err)
    41  	}
    42  
    43  	return path
    44  }
    45  
    46  func TestRotateFileWithInterval(t *testing.T) {
    47  
    48  	rotateFiler, _ := rotate_.NewRotateFiler(
    49  		filepath.Join(getWdOrDie(), "log"),
    50  		rotate_.WithRotateInterval(time.Second),
    51  		rotate_.WithSuffixName(".log"),
    52  		rotate_.WithPrefixName(filepath.Base(os.Args[0])),
    53  	)
    54  
    55  	for i := 0; i < 10; i++ {
    56  		_, n, err := rotateFiler.Write([]byte("hello word"))
    57  		if err != nil {
    58  			t.Errorf("faild to write, err: %v", err)
    59  		}
    60  		time.Sleep(1 * time.Second)
    61  		t.Logf("successed to write %v bytes", n)
    62  	}
    63  
    64  }
    65  
    66  func TestRotateFileWithIntervalAndSize(t *testing.T) {
    67  
    68  	rotateFiler, _ := rotate_.NewRotateFiler(
    69  		filepath.Join(getWdOrDie(), "log"),
    70  		rotate_.WithRotateInterval(time.Hour),
    71  		rotate_.WithRotateSize(15),
    72  		rotate_.WithSuffixName(".log"),
    73  		rotate_.WithPrefixName(filepath.Base(os.Args[0])),
    74  	)
    75  
    76  	for i := 0; i < 0; i++ {
    77  		_, n, err := rotateFiler.Write([]byte("hello word"))
    78  		if err != nil {
    79  			t.Errorf("faild to write, err: %v", err)
    80  		}
    81  		time.Sleep(time.Second)
    82  		t.Logf("successed to write %v bytes", n)
    83  	}
    84  
    85  }
    86  
    87  func TestRotateFileWithSize(t *testing.T) {
    88  
    89  	rotateFiler, _ := rotate_.NewRotateFiler(
    90  		filepath.Join(getWdOrDie(), "log"),
    91  		rotate_.WithRotateSize(10),
    92  		rotate_.WithRotateInterval(15*time.Second),
    93  		rotate_.WithSuffixName(".log"),
    94  		rotate_.WithPrefixName(filepath.Base(os.Args[0])),
    95  		rotate_.WithRotateCallback(func(ctx context.Context, path string) {
    96  			t.Logf("=======callback path: %v", path)
    97  		}),
    98  	)
    99  
   100  	for i := 0; i < 5; i++ {
   101  		//_, n, err := rotateFiler.Write([]byte("hello word"))
   102  		_, n, err := rotateFiler.WriteBytesLine([][]byte{[]byte("hello word")})
   103  		if err != nil {
   104  			t.Errorf("faild to write, err: %v", err)
   105  		}
   106  		time.Sleep(time.Second)
   107  		t.Logf("successed to write %v bytes", n)
   108  	}
   109  
   110  	select {}
   111  
   112  }
   113  
   114  func TestRotateMaxCount(t *testing.T) {
   115  
   116  	rotateFiler, _ := rotate_.NewRotateFiler(
   117  		filepath.Join(getWdOrDie(), "log"),
   118  		rotate_.WithRotateSize(15),
   119  		rotate_.WithMaxCount(5),
   120  		rotate_.WithSuffixName(".log"),
   121  		rotate_.WithPrefixName(filepath.Base(os.Args[0])),
   122  	)
   123  
   124  	for i := 0; i < 10; i++ {
   125  		_, n, err := rotateFiler.Write([]byte("hello word"))
   126  		if err != nil {
   127  			t.Errorf("faild to write, err: %v", err)
   128  		}
   129  		time.Sleep(time.Second)
   130  		t.Logf("successed to write %v bytes", n)
   131  	}
   132  
   133  }
   134  
   135  func TestRegex(t *testing.T) {
   136  	var regexps = []*regexp.Regexp{
   137  		regexp.MustCompile(`%[%+A-Za-z]`),
   138  		regexp.MustCompile(`\*+`),
   139  	}
   140  	globPattern := "1%%%AA20160304"
   141  	for _, re := range regexps {
   142  		globPattern = re.ReplaceAllString(globPattern, "*")
   143  		fmt.Printf("re: %v , globPattern: %v\n", re, globPattern)
   144  	}
   145  	//	return globPattern + `*`
   146  }