github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/server/serverpb/admin_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package serverpb
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  // TestTableStatsResponseAdd verifies that TableStatsResponse.Add()
    20  // correctly represents the result of combining stats from two spans.
    21  // Specifically, most TableStatsResponse's stats are a straight-forward sum,
    22  // but NodeCount should decrement as more missing nodes are added.
    23  func TestTableStatsResponseAdd(t *testing.T) {
    24  
    25  	// Initial object: no missing nodes.
    26  	underTest := TableStatsResponse{
    27  		RangeCount:           4,
    28  		ReplicaCount:         4,
    29  		ApproximateDiskBytes: 1000,
    30  		NodeCount:            8,
    31  	}
    32  
    33  	// Add stats: no missing nodes, so NodeCount should stay the same.
    34  	underTest.Add(&TableStatsResponse{
    35  		RangeCount:           1,
    36  		ReplicaCount:         2,
    37  		ApproximateDiskBytes: 2345,
    38  		NodeCount:            8,
    39  	})
    40  	assert.Equal(t, int64(5), underTest.RangeCount)
    41  	assert.Equal(t, int64(6), underTest.ReplicaCount)
    42  	assert.Equal(t, uint64(3345), underTest.ApproximateDiskBytes)
    43  	assert.Equal(t, int64(8), underTest.NodeCount)
    44  
    45  	// Add more stats: this time "node1" is missing. NodeCount should decrement.
    46  	underTest.Add(&TableStatsResponse{
    47  		RangeCount:           0,
    48  		ReplicaCount:         0,
    49  		ApproximateDiskBytes: 0,
    50  		NodeCount:            7,
    51  		MissingNodes: []TableStatsResponse_MissingNode{
    52  			{
    53  				NodeID:       "node1",
    54  				ErrorMessage: "error msg",
    55  			},
    56  		},
    57  	})
    58  	assert.Equal(t, int64(5), underTest.RangeCount)
    59  	assert.Equal(t, int64(6), underTest.ReplicaCount)
    60  	assert.Equal(t, uint64(3345), underTest.ApproximateDiskBytes)
    61  	assert.Equal(t, int64(7), underTest.NodeCount)
    62  	assert.Equal(t, []TableStatsResponse_MissingNode{
    63  		{
    64  			NodeID:       "node1",
    65  			ErrorMessage: "error msg",
    66  		},
    67  	}, underTest.MissingNodes)
    68  
    69  	// Add more stats: "node1" is missing again. NodeCount shouldn't decrement.
    70  	underTest.Add(&TableStatsResponse{
    71  		RangeCount:           0,
    72  		ReplicaCount:         0,
    73  		ApproximateDiskBytes: 0,
    74  		NodeCount:            7,
    75  		MissingNodes: []TableStatsResponse_MissingNode{
    76  			{
    77  				NodeID:       "node1",
    78  				ErrorMessage: "different error msg",
    79  			},
    80  		},
    81  	})
    82  	assert.Equal(t, int64(5), underTest.RangeCount)
    83  	assert.Equal(t, int64(6), underTest.ReplicaCount)
    84  	assert.Equal(t, uint64(3345), underTest.ApproximateDiskBytes)
    85  	assert.Equal(t, int64(7), underTest.NodeCount)
    86  
    87  	// Add more stats: new node is missing ("node2"). NodeCount should decrement.
    88  	underTest.Add(&TableStatsResponse{
    89  		RangeCount:           0,
    90  		ReplicaCount:         0,
    91  		ApproximateDiskBytes: 0,
    92  		NodeCount:            7,
    93  		MissingNodes: []TableStatsResponse_MissingNode{
    94  			{
    95  				NodeID:       "node2",
    96  				ErrorMessage: "totally new error msg",
    97  			},
    98  		},
    99  	})
   100  	assert.Equal(t, int64(5), underTest.RangeCount)
   101  	assert.Equal(t, int64(6), underTest.ReplicaCount)
   102  	assert.Equal(t, uint64(3345), underTest.ApproximateDiskBytes)
   103  	assert.Equal(t, int64(6), underTest.NodeCount)
   104  	assert.Equal(t, []TableStatsResponse_MissingNode{
   105  		{
   106  			NodeID:       "node1",
   107  			ErrorMessage: "error msg",
   108  		},
   109  		{
   110  			NodeID:       "node2",
   111  			ErrorMessage: "totally new error msg",
   112  		},
   113  	}, underTest.MissingNodes)
   114  
   115  }