github.com/aergoio/aergo@v1.3.1/p2p/metric/metricsman_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package metric
     7  
     8  import (
     9  	"github.com/aergoio/aergo/p2p/p2pcommon"
    10  	"github.com/aergoio/aergo/types"
    11  	"github.com/stretchr/testify/assert"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestMetricsManager_Stop(t *testing.T) {
    17  
    18  	tests := []struct {
    19  		name string
    20  	}{
    21  		{"T1"},
    22  		// TODO: test cases
    23  	}
    24  	for _, test := range tests {
    25  		t.Run(test.name, func(t *testing.T) {
    26  			mm := NewMetricManager(1)
    27  			go mm.Start()
    28  
    29  			time.Sleep(time.Millisecond * 230 )
    30  			mm.Stop()
    31  		})
    32  	}
    33  }
    34  
    35  func TestMetricsManager_Size(t *testing.T) {
    36  	pid, _ := types.IDB58Decode("16Uiu2HAmFqptXPfcdaCdwipB2fhHATgKGVFVPehDAPZsDKSU7jRm")
    37  
    38  	tests := []struct {
    39  		name string
    40  
    41  		// inSize should be small or equal to out
    42  		outSize int
    43  		inSize int
    44  
    45  		remove bool
    46  	}{
    47  		{"Tzero", 0,0, false},
    48  		{"TzeroRM", 0,0, true},
    49  		{"TSameInOut", 999,999, false},
    50  		{"TSameInOutRM", 999,999, true},
    51  		{"TLeave", 2999,999, false},
    52  		{"TLeaveRM", 2999,999, true},
    53  		// TODO: test cases
    54  	}
    55  	for _, test := range tests {
    56  		t.Run(test.name, func(t *testing.T) {
    57  			mm := NewMetricManager(1)
    58  			peerMetric := mm.NewMetric(pid, 1)
    59  
    60  			assert.Equal(t, int64(0), mm.deadTotalIn)
    61  			assert.Equal(t, int64(0), mm.deadTotalOut)
    62  			if test.outSize > 0 {
    63  				peerMetric.OnWrite(p2pcommon.PingRequest,test.outSize)
    64  				peerMetric.OnRead(p2pcommon.PingResponse,test.inSize)
    65  
    66  				assert.Equal(t, int64(0), mm.deadTotalIn)
    67  				assert.Equal(t, int64(0), mm.deadTotalOut)
    68  			}
    69  			if test.remove {
    70  				result := mm.Remove(pid, 1)
    71  				assert.Equal(t, int64(test.inSize), result.totalIn)
    72  				assert.Equal(t, int64(test.outSize), result.totalOut)
    73  				assert.Equal(t, int64(test.inSize),  mm.deadTotalIn)
    74  				assert.Equal(t, int64(test.outSize), mm.deadTotalOut)
    75  			}
    76  
    77  			summary := mm.Summary()
    78  			assert.Equal(t, int64(test.inSize), summary["in"].(int64))
    79  			assert.Equal(t, int64(test.outSize), summary["out"].(int64))
    80  
    81  		})
    82  	}
    83  }