github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/conn_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package log
     7  
     8  import (
     9  	"fmt"
    10  	"io"
    11  	"net"
    12  	"strings"
    13  	"sync"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func listenReadAndClose(t *testing.T, l net.Listener, expected string) {
    21  	conn, err := l.Accept()
    22  	assert.NoError(t, err)
    23  	defer conn.Close()
    24  	written, err := io.ReadAll(conn)
    25  
    26  	assert.NoError(t, err)
    27  	assert.Equal(t, expected, string(written))
    28  }
    29  
    30  func TestConnLogger(t *testing.T) {
    31  	protocol := "tcp"
    32  	address := ":3099"
    33  
    34  	l, err := net.Listen(protocol, address)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer l.Close()
    39  
    40  	prefix := "TestPrefix "
    41  	level := INFO
    42  	flags := LstdFlags | LUTC | Lfuncname
    43  
    44  	logger := NewConn()
    45  	connLogger := logger.(*ConnLogger)
    46  
    47  	logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, true, true, protocol, address))
    48  
    49  	assert.Equal(t, flags, connLogger.Flags)
    50  	assert.Equal(t, level, connLogger.Level)
    51  	assert.Equal(t, level, logger.GetLevel())
    52  
    53  	location, _ := time.LoadLocation("EST")
    54  
    55  	date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
    56  
    57  	dateString := date.UTC().Format("2006/01/02 15:04:05")
    58  
    59  	event := Event{
    60  		level:    INFO,
    61  		msg:      "TEST MSG",
    62  		caller:   "CALLER",
    63  		filename: "FULL/FILENAME",
    64  		line:     1,
    65  		time:     date,
    66  	}
    67  	expected := fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
    68  	var wg sync.WaitGroup
    69  	wg.Add(2)
    70  	go func() {
    71  		defer wg.Done()
    72  		listenReadAndClose(t, l, expected)
    73  	}()
    74  	go func() {
    75  		defer wg.Done()
    76  		err := logger.LogEvent(&event)
    77  		assert.NoError(t, err)
    78  	}()
    79  	wg.Wait()
    80  
    81  	event.level = WARN
    82  	expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
    83  	wg.Add(2)
    84  	go func() {
    85  		defer wg.Done()
    86  		listenReadAndClose(t, l, expected)
    87  	}()
    88  	go func() {
    89  		defer wg.Done()
    90  		err := logger.LogEvent(&event)
    91  		assert.NoError(t, err)
    92  	}()
    93  	wg.Wait()
    94  
    95  	logger.Close()
    96  }
    97  
    98  func TestConnLoggerBadConfig(t *testing.T) {
    99  	logger := NewConn()
   100  
   101  	err := logger.Init("{")
   102  	assert.Error(t, err)
   103  	assert.Contains(t, err.Error(), "Unable to parse JSON")
   104  	logger.Close()
   105  }
   106  
   107  func TestConnLoggerCloseBeforeSend(t *testing.T) {
   108  	protocol := "tcp"
   109  	address := ":3099"
   110  
   111  	prefix := "TestPrefix "
   112  	level := INFO
   113  	flags := LstdFlags | LUTC | Lfuncname
   114  
   115  	logger := NewConn()
   116  
   117  	logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, false, false, protocol, address))
   118  	logger.Close()
   119  }
   120  
   121  func TestConnLoggerFailConnect(t *testing.T) {
   122  	protocol := "tcp"
   123  	address := ":3099"
   124  
   125  	prefix := "TestPrefix "
   126  	level := INFO
   127  	flags := LstdFlags | LUTC | Lfuncname
   128  
   129  	logger := NewConn()
   130  
   131  	logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, false, false, protocol, address))
   132  
   133  	assert.Equal(t, level, logger.GetLevel())
   134  
   135  	location, _ := time.LoadLocation("EST")
   136  
   137  	date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
   138  
   139  	// dateString := date.UTC().Format("2006/01/02 15:04:05")
   140  
   141  	event := Event{
   142  		level:    INFO,
   143  		msg:      "TEST MSG",
   144  		caller:   "CALLER",
   145  		filename: "FULL/FILENAME",
   146  		line:     1,
   147  		time:     date,
   148  	}
   149  
   150  	err := logger.LogEvent(&event)
   151  	assert.Error(t, err)
   152  
   153  	logger.Close()
   154  }
   155  
   156  func TestConnLoggerClose(t *testing.T) {
   157  	protocol := "tcp"
   158  	address := ":3099"
   159  
   160  	l, err := net.Listen(protocol, address)
   161  	if err != nil {
   162  		t.Fatal(err)
   163  	}
   164  	defer l.Close()
   165  
   166  	prefix := "TestPrefix "
   167  	level := INFO
   168  	flags := LstdFlags | LUTC | Lfuncname
   169  
   170  	logger := NewConn()
   171  	connLogger := logger.(*ConnLogger)
   172  
   173  	logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, false, false, protocol, address))
   174  
   175  	assert.Equal(t, flags, connLogger.Flags)
   176  	assert.Equal(t, level, connLogger.Level)
   177  	assert.Equal(t, level, logger.GetLevel())
   178  	location, _ := time.LoadLocation("EST")
   179  
   180  	date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
   181  
   182  	dateString := date.UTC().Format("2006/01/02 15:04:05")
   183  
   184  	event := Event{
   185  		level:    INFO,
   186  		msg:      "TEST MSG",
   187  		caller:   "CALLER",
   188  		filename: "FULL/FILENAME",
   189  		line:     1,
   190  		time:     date,
   191  	}
   192  	expected := fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
   193  	var wg sync.WaitGroup
   194  	wg.Add(2)
   195  	go func() {
   196  		defer wg.Done()
   197  		err := logger.LogEvent(&event)
   198  		assert.NoError(t, err)
   199  		logger.Close()
   200  	}()
   201  	go func() {
   202  		defer wg.Done()
   203  		listenReadAndClose(t, l, expected)
   204  	}()
   205  	wg.Wait()
   206  
   207  	logger = NewConn()
   208  	connLogger = logger.(*ConnLogger)
   209  
   210  	logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, false, true, protocol, address))
   211  
   212  	assert.Equal(t, flags, connLogger.Flags)
   213  	assert.Equal(t, level, connLogger.Level)
   214  	assert.Equal(t, level, logger.GetLevel())
   215  
   216  	event.level = WARN
   217  	expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
   218  	wg.Add(2)
   219  	go func() {
   220  		defer wg.Done()
   221  		listenReadAndClose(t, l, expected)
   222  	}()
   223  	go func() {
   224  		defer wg.Done()
   225  		err := logger.LogEvent(&event)
   226  		assert.NoError(t, err)
   227  		logger.Close()
   228  	}()
   229  	wg.Wait()
   230  	logger.Flush()
   231  	logger.Close()
   232  }