trpc.group/trpc-go/trpc-go@v1.0.3/naming/loadbalance/weightroundrobin/weightroundrobin_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package weightroundrobin 15 16 import ( 17 "testing" 18 "time" 19 20 "github.com/stretchr/testify/assert" 21 "trpc.group/trpc-go/trpc-go/naming/registry" 22 ) 23 24 func TestWrrSmoothBalancing(t *testing.T) { 25 wrr := NewWeightRoundRobin(0) 26 // weight: a: 5, b: 1, c: 1 27 // list shound be: a, a, b, a, c, a, a 28 tests := []int{0, 0, 1, 0, 2, 0, 0} 29 for i := 0; i < 7; i++ { 30 n, err := wrr.Select("test1", list1) 31 assert.Nil(t, err) 32 assert.Equal(t, list1[tests[i]], n) 33 } 34 } 35 36 func TestWrrListLengthChange(t *testing.T) { 37 wrr := NewWeightRoundRobin(defaultUpdateRate) 38 n1, err := wrr.Select("test1", list1) 39 assert.Nil(t, err) 40 assert.Equal(t, n1, list1[0]) 41 42 tests := []int{3, 0, 1, 2, 3, 0, 3, 1, 2, 0, 3} 43 for i := 0; i < 11; i++ { 44 n, err := wrr.Select("test1", list2) 45 assert.Nil(t, err) 46 assert.Equal(t, list2[tests[i]], n) 47 } 48 } 49 50 func TestWrrInterval(t *testing.T) { 51 wrr := NewWeightRoundRobin(time.Second * 1) 52 53 n1, err := wrr.Select("test1", list1) 54 assert.Nil(t, err) 55 assert.Equal(t, n1, list1[0]) 56 tests := []int{0, 1, 0, 2, 0, 0} 57 for i := 0; i < 6; i++ { 58 n, err := wrr.Select("test1", list3) 59 assert.Nil(t, err) 60 assert.Equal(t, list1[tests[i]], n) 61 } 62 63 time.Sleep(time.Second) 64 65 tests = []int{0, 1, 0, 2, 0, 1, 0} 66 for i := 0; i < 6; i++ { 67 n, err := wrr.Select("test1", list3) 68 assert.Nil(t, err) 69 assert.Equal(t, list3[tests[i]], n) 70 } 71 } 72 73 func TestWrrDifferentService(t *testing.T) { 74 wrr := NewWeightRoundRobin(defaultUpdateRate) 75 // weight: a: 5, b: 1, c: 1 76 // list shound be: a, a, b, a, c, a, a 77 tests := []int{0, 0, 1, 0, 2, 0, 0} 78 for i := 0; i < 7; i++ { 79 n, err := wrr.Select("test1", list1) 80 assert.Nil(t, err) 81 assert.Equal(t, list1[tests[i]], n) 82 } 83 84 tests = []int{3, 0, 1, 2, 3, 0, 3, 1, 2, 0, 3} 85 for i := 0; i < 11; i++ { 86 n, err := wrr.Select("test2", list2) 87 assert.Nil(t, err) 88 assert.Equal(t, list2[tests[i]], n) 89 } 90 } 91 92 var list1 = []*registry.Node{ 93 { 94 Address: "list1.ip.1:8080", 95 Weight: 5, 96 Metadata: map[string]interface{}{ 97 "weight": 5, 98 }, 99 }, 100 { 101 Address: "list1.ip.3:8080", 102 Weight: 1, 103 Metadata: map[string]interface{}{ 104 "weight": 1, 105 }, 106 }, 107 { 108 Address: "list1.ip.4:8080", 109 Weight: 1, 110 Metadata: map[string]interface{}{ 111 "weight": 1, 112 }, 113 }, 114 } 115 116 var list2 = []*registry.Node{ 117 { 118 Address: "list2.ip.5:8080", 119 Weight: 3, 120 Metadata: map[string]interface{}{ 121 "weight": 3, 122 }, 123 }, 124 { 125 Address: "list2.ip.6:8080", 126 Weight: 2, 127 Metadata: map[string]interface{}{ 128 "weight": 2, 129 }, 130 }, 131 { 132 Address: "list2.ip.7:8080", 133 Weight: 2, 134 Metadata: map[string]interface{}{ 135 "weight": 2, 136 }, 137 }, 138 { 139 Address: "list2.ip.8:8080", 140 Weight: 4, 141 Metadata: map[string]interface{}{ 142 "weight": 4, 143 }, 144 }, 145 } 146 147 var list3 = []*registry.Node{ 148 { 149 Address: "list3.ip.1:8080", 150 Weight: 4, 151 Metadata: map[string]interface{}{ 152 "weight": 4, 153 }, 154 }, 155 { 156 Address: "list3.ip.3:8080", 157 Weight: 2, 158 Metadata: map[string]interface{}{ 159 "weight": 2, 160 }, 161 }, 162 { 163 Address: "list3.ip.4:8080", 164 Weight: 1, Metadata: map[string]interface{}{ 165 "weight": 1, 166 }, 167 }, 168 }