writer_test.go 675 字节
Newer Older
Y
Your Name 已提交
1
package gokulog
E
eoLinker API Management 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

import (
	"context"
	"fmt"
	"testing"
	"time"
)

type MinPeriod struct {
}

func (p *MinPeriod) String() string {
	return "Minute"
}

func (p *MinPeriod) FormatLayout() string {
	return "200601021504"
}

func TestFileWriterByPeriod(t *testing.T) {
Y
Your Name 已提交
22
	w := NewFileWriteBytePeriod("/Users/huangmengzhu/test/log", "app.log", new(MinPeriod))
E
eoLinker API Management 已提交
23
	defer w.Close()
Y
Your Name 已提交
24
	ctx, _ := context.WithTimeout(context.Background(), time.Minute*3)
E
eoLinker API Management 已提交
25

Y
Your Name 已提交
26
	tick := time.NewTicker(time.Millisecond)
E
eoLinker API Management 已提交
27
	defer tick.Stop()
Y
Your Name 已提交
28
	index := 0
E
eoLinker API Management 已提交
29 30 31

	for {
		select {
Y
Your Name 已提交
32
		case <-ctx.Done():
E
eoLinker API Management 已提交
33 34 35 36 37 38 39 40
			{
				w.Close()
				return

			}
		case <-tick.C:
			{
				index++
Y
Your Name 已提交
41
				fmt.Fprintf(w, "line:%d\n", index)
E
eoLinker API Management 已提交
42 43 44
			}
		}
	}
Y
Your Name 已提交
45
}