go.uber.org/yarpc@v1.72.1/peer/x/peerheap/heap_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package peerheap
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestPeerHeapEmpty(t *testing.T) {
    31  	var ph peerHeap
    32  	assert.Zero(t, ph.Len(), "New peer heap should be empty")
    33  	popAndVerifyHeap(t, &ph)
    34  }
    35  
    36  func TestPeerHeapOrdering(t *testing.T) {
    37  	p1 := &peerScore{score: 1}
    38  	p2 := &peerScore{score: 2}
    39  	p3 := &peerScore{score: 3}
    40  
    41  	// same score as p3, but always pushed after p3, so it will be returned last.
    42  	p4 := &peerScore{score: 3}
    43  
    44  	want := []*peerScore{p1, p2, p3, p4}
    45  	tests := [][]*peerScore{
    46  		{p1, p2, p3, p4},
    47  		{p3, p4, p2, p1},
    48  		{p3, p1, p2, p4},
    49  	}
    50  
    51  	for _, tt := range tests {
    52  		var h peerHeap
    53  		for _, ps := range tt {
    54  			h.pushPeer(ps)
    55  		}
    56  
    57  		popped := popAndVerifyHeap(t, &h)
    58  		assert.Equal(t, want, popped, "Unexpected ordering of peers")
    59  	}
    60  }
    61  
    62  func TestPeerHeapUpdate(t *testing.T) {
    63  	var h peerHeap
    64  	p1 := &peerScore{score: 1}
    65  	p2 := &peerScore{score: 2}
    66  	p3 := &peerScore{score: 3}
    67  
    68  	h.pushPeer(p3)
    69  	h.pushPeer(p1)
    70  	h.pushPeer(p2)
    71  
    72  	ps, ok := h.popPeer()
    73  	require.True(t, ok, "pop with non-empty heap should succeed")
    74  	assert.Equal(t, p1, ps, "Wrong peer")
    75  
    76  	// Now update p2's score to be higher than p3.
    77  	p2.score = 10
    78  	h.update(p2.idx)
    79  
    80  	popped := popAndVerifyHeap(t, &h)
    81  	assert.Equal(t, []*peerScore{p3, p2}, popped, "Unexpected order after p2 update")
    82  }
    83  
    84  func TestPeerHeapDelete(t *testing.T) {
    85  	const numPeers = 10
    86  
    87  	var h peerHeap
    88  	peers := make([]*peerScore, numPeers)
    89  	for i := range peers {
    90  		peers[i] = &peerScore{score: int64(i)}
    91  		h.pushPeer(peers[i])
    92  	}
    93  
    94  	// The first peer is the lowest, remove it so it swaps with the last peer.
    95  	h.delete(0)
    96  
    97  	// Now when we pop peers, we expect peers 1 to N.
    98  	want := peers[1:]
    99  	popped := popAndVerifyHeap(t, &h)
   100  	assert.Equal(t, want, popped, "Unexpected peers after delete peer 0")
   101  }
   102  
   103  func TestPeerHeapValidate(t *testing.T) {
   104  	var h peerHeap
   105  	h.pushPeer(&peerScore{score: 1})
   106  
   107  	for _, i := range []int{0, -1, 5} {
   108  		ps := &peerScore{idx: i}
   109  		assert.Error(t, h.validate(ps), "peer %v should not validate", ps)
   110  	}
   111  }
   112  
   113  func popAndVerifyHeap(t *testing.T, h *peerHeap) []*peerScore {
   114  	var popped []*peerScore
   115  
   116  	lastScore := int64(-1)
   117  	for h.Len() > 0 {
   118  		verifyIndexes(t, h)
   119  
   120  		ps, ok := h.popPeer()
   121  		require.True(t, ok, "pop with non-empty heap should succeed")
   122  		popped = append(popped, ps)
   123  
   124  		if lastScore == -1 {
   125  			lastScore = ps.score
   126  			continue
   127  		}
   128  
   129  		if ps.score < lastScore {
   130  			t.Fatalf("heap returned peer %v with lower score than %v", ps, lastScore)
   131  		}
   132  		lastScore = ps.score
   133  	}
   134  
   135  	_, ok := h.popPeer()
   136  	require.False(t, ok, "Expected no peers to be returned with empty list")
   137  	return popped
   138  }
   139  
   140  func verifyIndexes(t *testing.T, h *peerHeap) {
   141  	for i := range h.peers {
   142  		assert.Equal(t, i, h.peers[i].idx, "wrong index for peer %v", h.peers[i])
   143  	}
   144  }