github.com/lingyao2333/mo-zero@v1.4.1/core/stores/mon/util_test.go (about)

     1  package mon
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/lingyao2333/mo-zero/core/logx"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestFormatAddrs(t *testing.T) {
    15  	tests := []struct {
    16  		addrs  []string
    17  		expect string
    18  	}{
    19  		{
    20  			addrs:  []string{"a", "b"},
    21  			expect: "a,b",
    22  		},
    23  		{
    24  			addrs:  []string{"a", "b", "c"},
    25  			expect: "a,b,c",
    26  		},
    27  		{
    28  			addrs:  []string{},
    29  			expect: "",
    30  		},
    31  		{
    32  			addrs:  nil,
    33  			expect: "",
    34  		},
    35  	}
    36  
    37  	for _, test := range tests {
    38  		assert.Equal(t, test.expect, FormatAddr(test.addrs))
    39  	}
    40  }
    41  
    42  func Test_logDuration(t *testing.T) {
    43  	var buf strings.Builder
    44  	w := logx.NewWriter(&buf)
    45  	o := logx.Reset()
    46  	logx.SetWriter(w)
    47  
    48  	defer func() {
    49  		logx.Reset()
    50  		logx.SetWriter(o)
    51  	}()
    52  
    53  	buf.Reset()
    54  	logDuration(context.Background(), "foo", "bar", time.Millisecond, nil)
    55  	assert.Contains(t, buf.String(), "foo")
    56  	assert.Contains(t, buf.String(), "bar")
    57  
    58  	buf.Reset()
    59  	logDuration(context.Background(), "foo", "bar", time.Millisecond, errors.New("bar"))
    60  	assert.Contains(t, buf.String(), "foo")
    61  	assert.Contains(t, buf.String(), "bar")
    62  	assert.Contains(t, buf.String(), "fail")
    63  }