trpc.group/trpc-go/trpc-go@v1.0.3/naming/loadbalance/roundrobin/roundrobin_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 roundrobin 15 16 import ( 17 "sync" 18 "testing" 19 "time" 20 21 "trpc.group/trpc-go/trpc-go/naming/registry" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestRoundRobinGetOne(t *testing.T) { 27 rr := NewRoundRobin(0) 28 length := len(list1) 29 for i := 0; i < length*2; i++ { 30 n, err := rr.Select("test1", list1) 31 assert.Nil(t, err) 32 assert.Equal(t, list1[i%length], n) 33 } 34 } 35 36 func TestRoundRobinListLengthChange(t *testing.T) { 37 rr := NewRoundRobin(time.Second * 10) 38 n1, err := rr.Select("test1", list1) 39 assert.Nil(t, err) 40 assert.Equal(t, n1, list1[0]) 41 42 length := len(list2) 43 assert.Equal(t, 3, length) 44 for i := 0; i < length*2; i++ { 45 n, err := rr.Select("test1", list2) 46 assert.Nil(t, err) 47 assert.Equal(t, list2[i%length], n) 48 } 49 } 50 51 func TestRoundRobinInterval(t *testing.T) { 52 rr := NewRoundRobin(time.Second * 1) 53 n1, err := rr.Select("test1", list1) 54 assert.Nil(t, err) 55 assert.Equal(t, n1, list1[0]) 56 57 n2, err := rr.Select("test1", list3) 58 assert.Nil(t, err) 59 assert.Equal(t, n2, list1[1]) 60 61 time.Sleep(time.Second) 62 n3, err := rr.Select("test1", list3) 63 assert.Nil(t, err) 64 assert.Equal(t, n3, list3[0]) 65 } 66 67 func TestRoundRobinConCurrentSelect(t *testing.T) { 68 rr := NewRoundRobin(time.Second * 1) 69 70 var wg sync.WaitGroup 71 wg.Add(10) 72 for i := 0; i < 10; i++ { 73 go func() { 74 _, err := rr.Select("test1", list1) 75 assert.Nil(t, err) 76 wg.Done() 77 }() 78 } 79 80 wg.Wait() 81 } 82 83 func TestRoundRobinSelectDifferentService(t *testing.T) { 84 rr := NewRoundRobin(time.Second * 1) 85 for i := 0; i < 10; i++ { 86 n1, err := rr.Select("test1", list1) 87 assert.Nil(t, err) 88 assert.Equal(t, n1, list1[i%len(list1)]) 89 90 n2, err := rr.Select("test2", list2) 91 assert.Nil(t, err) 92 assert.Equal(t, n2, list2[i%len(list2)]) 93 } 94 } 95 96 var list1 = []*registry.Node{ 97 { 98 Address: "list1.ip.1:8080", 99 }, 100 { 101 Address: "list1.ip.2:8080", 102 }, 103 { 104 Address: "list1.ip.3:8080", 105 }, 106 { 107 Address: "list1.ip.4:8080", 108 }, 109 } 110 111 var list2 = []*registry.Node{ 112 { 113 Address: "list2.ip.2:8080", 114 }, 115 { 116 Address: "list2.ip.3:8080", 117 }, 118 { 119 Address: "list2.ip.4:8080", 120 }, 121 } 122 123 var list3 = []*registry.Node{ 124 { 125 Address: "list3.ip.5:8080", 126 }, 127 { 128 Address: "list3.ip.6:8080", 129 }, 130 { 131 Address: "list3.ip.7:8080", 132 }, 133 { 134 Address: "list3.ip.8:8080", 135 }, 136 }