logger_test.go 796 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package log_test
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/xtls/xray-core/common"
  8. "github.com/xtls/xray-core/common/buf"
  9. . "github.com/xtls/xray-core/common/log"
  10. )
  11. func TestFileLogger(t *testing.T) {
  12. f, err := os.CreateTemp("", "vtest")
  13. common.Must(err)
  14. path := f.Name()
  15. common.Must(f.Close())
  16. defer os.Remove(path)
  17. creator, err := CreateFileLogWriter(path)
  18. common.Must(err)
  19. handler := NewLogger(creator)
  20. handler.Handle(&GeneralMessage{Content: "Test Log"})
  21. time.Sleep(2 * time.Second)
  22. common.Must(common.Close(handler))
  23. f, err = os.Open(path)
  24. common.Must(err)
  25. defer f.Close()
  26. b, err := buf.ReadAllToBytes(f)
  27. common.Must(err)
  28. if !strings.Contains(string(b), "Test Log") {
  29. t.Fatal("Expect log text contains 'Test Log', but actually: ", string(b))
  30. }
  31. }