github.com/phuslu/log@v1.0.100/syslog_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package log
     5  
     6  import (
     7  	"net"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestSyslogWriterTCP(t *testing.T) {
    14  	w := &SyslogWriter{
    15  		Network: "udp",
    16  		Address: "10.0.0.2:543",
    17  		Tag:     "",
    18  		Dial:    net.Dial,
    19  	}
    20  
    21  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
    22  		_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
    23  		_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277+08:00","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
    24  		_, _ = wlprintf(w, ParseLevel(level), `{"ts":1234567890,"level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
    25  	}
    26  
    27  	_, err := wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
    28  	if err != nil {
    29  		t.Errorf("write syslog writer error: %+v", err)
    30  	}
    31  
    32  	_, err = wlprintf(w, InfoLevel, "a long long long long message.\n")
    33  	if err != nil {
    34  		t.Errorf("write syslog writer error: %+v", err)
    35  	}
    36  
    37  	w.Close()
    38  	w.Close()
    39  
    40  	_, err = wlprintf(w, InfoLevel, "a long long long long message again.\n")
    41  	if err != nil {
    42  		t.Errorf("write syslog writer error: %+v", err)
    43  	}
    44  }
    45  
    46  func TestSyslogWriterTCPError(t *testing.T) {
    47  	w := &SyslogWriter{
    48  		Network: "tcp",
    49  		Address: "127.0.0.1:601",
    50  		Tag:     "",
    51  		Dial:    net.Dial,
    52  	}
    53  
    54  	_, _ = wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
    55  	w.Close()
    56  	_, _ = wlprintf(w, InfoLevel, "a long long long long message again.\n")
    57  }
    58  
    59  func TestSyslogWriterUnix(t *testing.T) {
    60  	const sockname = "/tmp/go-tmp-null.sock"
    61  
    62  	conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: sockname, Net: "unixgram"})
    63  	if err != nil {
    64  		t.Errorf("listen error: %+v", err)
    65  		return
    66  	}
    67  	defer os.Remove(sockname)
    68  
    69  	go func() {
    70  		var data [512]byte
    71  		for {
    72  			buf := data[:]
    73  			n, uaddr, err := conn.ReadFromUnix(buf)
    74  			if err != nil {
    75  				t.Logf("listen: error: %v\n", err)
    76  			} else {
    77  				t.Logf("listen: received %v bytes from %+v\n", n, uaddr)
    78  			}
    79  			time.Sleep(10 * time.Millisecond)
    80  		}
    81  	}()
    82  
    83  	w := &SyslogWriter{
    84  		Network: "unixgram",
    85  		Address: "/tmp/go-tmp-null.sock",
    86  	}
    87  
    88  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
    89  		_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
    90  		_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277+08:00","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
    91  		_, _ = wlprintf(w, ParseLevel(level), `{"ts":1234567890,"level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
    92  	}
    93  
    94  	_, err = wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
    95  	if err != nil {
    96  		t.Errorf("write syslog writer error: %+v", err)
    97  	}
    98  
    99  	_, err = wlprintf(w, InfoLevel, "a long long long long message.\n")
   100  	if err != nil {
   101  		t.Errorf("write syslog writer error: %+v", err)
   102  	}
   103  
   104  	w.Close()
   105  	w.Close()
   106  
   107  	_, err = wlprintf(w, InfoLevel, "a long long long long message again.\n")
   108  	if err != nil {
   109  		t.Errorf("write syslog writer error: %+v", err)
   110  	}
   111  
   112  	os.Remove(sockname)
   113  
   114  	_, err = wlprintf(w, InfoLevel, "a long long long long message again.\n")
   115  	t.Logf("write syslog writer error: %+v", err)
   116  }