github.com/aergoio/aergo@v1.3.1/p2p/p2putil/loggingutil_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2putil
     7  
     8  import (
     9  	"bytes"
    10  	"fmt"
    11  	"github.com/aergoio/aergo-lib/log"
    12  	"github.com/aergoio/aergo/p2p/p2pcommon"
    13  	"github.com/aergoio/aergo/types"
    14  	"github.com/funkygao/golib/rand"
    15  	"github.com/libp2p/go-libp2p-core/crypto"
    16  	"testing"
    17  
    18  	"github.com/rs/zerolog"
    19  )
    20  
    21  func TestLogStringersMarshaler_MarshalZerologArray(t *testing.T) {
    22  
    23  	sampleArr := make([]fmt.Stringer, 20)
    24  	for i := 0; i < 20; i++ {
    25  		sampleArr[i] = NumOrderer{i}
    26  	}
    27  	type fields struct {
    28  		arr   []fmt.Stringer
    29  		limit int
    30  	}
    31  
    32  	tests := []struct {
    33  		name   string
    34  		fields fields
    35  
    36  		wantSize int
    37  	}{
    38  		{"TEmpty", fields{nil, 10}, 0},
    39  		{"TOne", fields{sampleArr[:1], 10}, 1},
    40  		{"TMid", fields{sampleArr[1:6], 10}, 5},
    41  		{"TMax", fields{sampleArr[:10], 10}, 10},
    42  		{"TOver", fields{sampleArr[0:11], 10}, 10},
    43  		{"TOver2", fields{sampleArr, 10}, 10},
    44  	}
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			buf1 := bytes.NewBuffer(nil)
    48  			log1 := log.NewLogger("test.p2p").Output(buf1)
    49  			buf2 := bytes.NewBuffer(nil)
    50  			log2 := log.NewLogger("test.p2p").Output(buf2)
    51  
    52  			m := NewLogStringersMarshaller(tt.fields.arr, tt.fields.limit)
    53  			a := zerolog.Arr()
    54  			m.MarshalZerologArray(a)
    55  			log1.Info().Array("t", m).Msg("Print ")
    56  			log2.Info().Array("t", a).Msg("Print ")
    57  
    58  			if !bytes.Equal(buf1.Bytes(), buf2.Bytes()) {
    59  				t.Errorf("output is differ \n%v \n%v ", buf1.String(), buf2.String())
    60  			} else {
    61  				//fmt.Println(buf1.String())
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestLogPeerMetasMarshaler_MarshalZerologArray(t *testing.T) {
    68  	sampleArr := make([]p2pcommon.PeerMeta, 20)
    69  	for i := 0; i < 20; i++ {
    70  		meta := p2pcommon.PeerMeta{}
    71  		meta.ID = pseudoGenID()
    72  		meta.IPAddress = fmt.Sprintf("192.168.0.%d", i)
    73  		meta.Port = uint32(i * 1000)
    74  		meta.Hidden = i%2 == 0
    75  		sampleArr[i] = meta
    76  	}
    77  	type fields struct {
    78  		arr   []p2pcommon.PeerMeta
    79  		limit int
    80  	}
    81  
    82  	tests := []struct {
    83  		name   string
    84  		fields fields
    85  
    86  		wantSize int
    87  	}{
    88  		{"TEmpty", fields{nil, 10}, 0},
    89  		{"TOne", fields{sampleArr[:1], 10}, 1},
    90  		{"TMid", fields{sampleArr[1:6], 10}, 5},
    91  		{"TMax", fields{sampleArr[:10], 10}, 10},
    92  		{"TOver", fields{sampleArr[0:11], 10}, 10},
    93  		{"TOver2", fields{sampleArr, 10}, 10},
    94  	}
    95  	for _, tt := range tests {
    96  		t.Run(tt.name, func(t *testing.T) {
    97  			buf1 := bytes.NewBuffer(nil)
    98  			log1 := log.NewLogger("test.p2p").Output(buf1)
    99  			buf2 := bytes.NewBuffer(nil)
   100  			log2 := log.NewLogger("test.p2p").Output(buf2)
   101  
   102  			m := NewLogPeerMetasMarshaller(tt.fields.arr, tt.fields.limit)
   103  			a := zerolog.Arr()
   104  			m.MarshalZerologArray(a)
   105  			log1.Info().Array("t", m).Msg("Print ")
   106  			log2.Info().Array("t", a).Msg("Print ")
   107  
   108  			if !bytes.Equal(buf1.Bytes(), buf2.Bytes()) {
   109  				t.Errorf("output is differ \n%v \n%v ", buf1.String(), buf2.String())
   110  			} else {
   111  				//fmt.Println(buf1.String())
   112  			}
   113  		})
   114  	}
   115  }
   116  
   117  func TestLogB58EncMarshaler_MarshalZerologArray(t *testing.T) {
   118  	sampleArr := make([][]byte, 20)
   119  	for i := 0; i < 20; i++ {
   120  		sampleArr[i] = rand.RandomByteSlice(32)
   121  	}
   122  	type fields struct {
   123  		arr   [][]byte
   124  		limit int
   125  	}
   126  
   127  	tests := []struct {
   128  		name   string
   129  		fields fields
   130  
   131  		wantSize int
   132  	}{
   133  		{"TEmpty", fields{nil, 10}, 0},
   134  		{"TOne", fields{sampleArr[:1], 10}, 1},
   135  		{"TMid", fields{sampleArr[1:6], 10}, 5},
   136  		{"TMax", fields{sampleArr[:10], 10}, 10},
   137  		{"TOver", fields{sampleArr[0:11], 10}, 10},
   138  		{"TOver2", fields{sampleArr, 10}, 10},
   139  	}
   140  	for _, tt := range tests {
   141  		t.Run(tt.name, func(t *testing.T) {
   142  			buf1 := bytes.NewBuffer(nil)
   143  			log1 := log.NewLogger("test.p2p").Output(buf1)
   144  			buf2 := bytes.NewBuffer(nil)
   145  			log2 := log.NewLogger("test.p2p").Output(buf2)
   146  
   147  			m := types.NewLogB58EncMarshaller(tt.fields.arr, tt.fields.limit)
   148  			a := zerolog.Arr()
   149  			m.MarshalZerologArray(a)
   150  			log1.Info().Array("t", m).Msg("Print ")
   151  			log2.Info().Array("t", a).Msg("Print ")
   152  
   153  			if !bytes.Equal(buf1.Bytes(), buf2.Bytes()) {
   154  				t.Errorf("output is differ \n%v \n%v ", buf1.String(), buf2.String())
   155  			} else {
   156  				//fmt.Println(buf1.String())
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func pseudoGenID() types.PeerID {
   163  	priv, _, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
   164  	id, _ := types.IDFromPrivateKey(priv)
   165  	return id
   166  }
   167  
   168  type NumOrderer struct {
   169  	num int
   170  }
   171  
   172  func (no NumOrderer) String() string {
   173  	return fmt.Sprintf("I am no.%d", no.num)
   174  }