github.com/xraypb/xray-core@v1.6.6/app/dispatcher/stats_test.go (about)

     1  package dispatcher_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/xraypb/xray-core/app/dispatcher"
     7  	"github.com/xraypb/xray-core/common"
     8  	"github.com/xraypb/xray-core/common/buf"
     9  )
    10  
    11  type TestCounter int64
    12  
    13  func (c *TestCounter) Value() int64 {
    14  	return int64(*c)
    15  }
    16  
    17  func (c *TestCounter) Add(v int64) int64 {
    18  	x := int64(*c) + v
    19  	*c = TestCounter(x)
    20  	return x
    21  }
    22  
    23  func (c *TestCounter) Set(v int64) int64 {
    24  	*c = TestCounter(v)
    25  	return v
    26  }
    27  
    28  func TestStatsWriter(t *testing.T) {
    29  	var c TestCounter
    30  	writer := &SizeStatWriter{
    31  		Counter: &c,
    32  		Writer:  buf.Discard,
    33  	}
    34  
    35  	mb := buf.MergeBytes(nil, []byte("abcd"))
    36  	common.Must(writer.WriteMultiBuffer(mb))
    37  
    38  	mb = buf.MergeBytes(nil, []byte("efg"))
    39  	common.Must(writer.WriteMultiBuffer(mb))
    40  
    41  	if c.Value() != 7 {
    42  		t.Fatal("unexpected counter value. want 7, but got ", c.Value())
    43  	}
    44  }