Xray-core/common/log/logger_test.go

39 lines
773 B
Go
Raw Normal View History

2020-11-25 11:01:53 +00:00
package log_test
import (
"os"
"strings"
"testing"
"time"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
. "github.com/xtls/xray-core/common/log"
2020-11-25 11:01:53 +00:00
)
func TestFileLogger(t *testing.T) {
f, err := os.CreateTemp("", "vtest")
2020-11-25 11:01:53 +00:00
common.Must(err)
path := f.Name()
common.Must(f.Close())
creator, err := CreateFileLogWriter(path)
common.Must(err)
handler := NewLogger(creator)
handler.Handle(&GeneralMessage{Content: "Test Log"})
time.Sleep(2 * time.Second)
common.Must(common.Close(handler))
f, err = os.Open(path)
common.Must(err)
defer f.Close()
b, err := buf.ReadAllToBytes(f)
common.Must(err)
if !strings.Contains(string(b), "Test Log") {
t.Fatal("Expect log text contains 'Test Log', but actually: ", string(b))
}
}