github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/connection/connectionstate/stats_test.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package connectionstate
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var (
    28  	exampleStats = Statistics{
    29  		BytesReceived: 1,
    30  		BytesSent:     2,
    31  	}
    32  )
    33  
    34  func TestStatistics_Diff(t *testing.T) {
    35  	tests := []struct {
    36  		name string
    37  		old  Statistics
    38  		new  Statistics
    39  		want Statistics
    40  	}{
    41  		{
    42  			name: "calculates statistics correctly if they are continuous",
    43  			old:  Statistics{},
    44  			new:  exampleStats,
    45  			want: exampleStats,
    46  		},
    47  		{
    48  			name: "calculates statistics correctly if they are not continuous",
    49  			old: Statistics{
    50  				BytesReceived: 5,
    51  				BytesSent:     6,
    52  			},
    53  			new:  exampleStats,
    54  			want: exampleStats,
    55  		},
    56  		{
    57  			name: "returns zeros on no change",
    58  			old:  exampleStats,
    59  			new:  exampleStats,
    60  			want: Statistics{},
    61  		},
    62  	}
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			result := tt.old.Diff(tt.new)
    66  			assert.True(t, reflect.DeepEqual(result, tt.want))
    67  		})
    68  	}
    69  }
    70  
    71  func TestStatistics_Plus(t *testing.T) {
    72  	tests := []struct {
    73  		name  string
    74  		stats Statistics
    75  		diff  Statistics
    76  		want  Statistics
    77  	}{
    78  		{
    79  			name:  "adds up stats correctly",
    80  			diff:  exampleStats,
    81  			stats: exampleStats,
    82  			want: Statistics{
    83  				BytesReceived: 2,
    84  				BytesSent:     4,
    85  			},
    86  		},
    87  	}
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			result := tt.stats.Plus(tt.diff)
    91  			assert.EqualValues(t, tt.want.BytesReceived, result.BytesReceived)
    92  			assert.EqualValues(t, tt.want.BytesSent, result.BytesSent)
    93  		})
    94  	}
    95  }