github.com/aergoio/aergo@v1.3.1/types/p2p_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package types
     7  
     8  import (
     9  	"encoding/hex"
    10  	"fmt"
    11  	"github.com/aergoio/aergo/internal/enc"
    12  	"github.com/golang/protobuf/proto"
    13  	"github.com/stretchr/testify/assert"
    14  	"testing"
    15  )
    16  
    17  func TestUnmarshalSize(t *testing.T) {
    18  	var dummyTxHash, _ = enc.ToBytes("4H4zAkAyRV253K5SNBJtBxqUgHEbZcXbWFFc6cmQHY45")
    19  	fmt.Println("Hash: ", hex.EncodeToString(dummyTxHash))
    20  
    21  	sample := &NewTransactionsNotice{}
    22  
    23  	expectedLen := proto.Size(sample)
    24  	actual, err := proto.Marshal(sample)
    25  	assert.Nil(t, err)
    26  	fmt.Println("Empty notice size ",len(actual))
    27  	assert.Equal(t,expectedLen, len(actual) )
    28  
    29  	// single member
    30  	hashes := make([][]byte, 0, 10)
    31  	hashes = append(hashes, dummyTxHash)
    32  	sample.TxHashes = hashes
    33  	expectedLen = proto.Size(sample)
    34  	actual, err = proto.Marshal(sample)
    35  	assert.Nil(t, err)
    36  	fmt.Println("Single hash notice size ",len(actual))
    37  	fmt.Println("Hex: ", hex.EncodeToString(actual))
    38  	assert.Equal(t,expectedLen, len(actual) )
    39  
    40  	// 100 hashes
    41  	hashes = make([][]byte,100)
    42  	for i:=0; i<100 ; i++ {
    43  		hashes[i] = dummyTxHash
    44  	}
    45  	sample.TxHashes = hashes
    46  	expectedLen = proto.Size(sample)
    47  	actual, err = proto.Marshal(sample)
    48  	assert.Nil(t, err)
    49  	fmt.Println("Hundread hashes notice size ",len(actual))
    50  	fmt.Println("Hex: ", hex.EncodeToString(actual[0:40]))
    51  	assert.Equal(t,expectedLen, len(actual) )
    52  
    53  	// 1000 hashes
    54  	hashes = make([][]byte,1000)
    55  	for i:=0; i<1000 ; i++ {
    56  		hashes[i] = dummyTxHash
    57  	}
    58  	sample.TxHashes = hashes
    59  	expectedLen = proto.Size(sample)
    60  	actual, err = proto.Marshal(sample)
    61  	assert.Nil(t, err)
    62  	fmt.Println("Thousand hashes notice size ",len(actual))
    63  	fmt.Println("Hex: ", hex.EncodeToString(actual[0:40]))
    64  	assert.Equal(t,expectedLen, len(actual) )
    65  
    66  }