github.com/cloudwego/kitex@v0.9.0/pkg/loadbalance/lbcache/hookable_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package lbcache 18 19 import ( 20 "testing" 21 22 "github.com/cloudwego/kitex/internal/test" 23 "github.com/cloudwego/kitex/pkg/discovery" 24 ) 25 26 func TestHookableRebalance(t *testing.T) { 27 mockRb := &mockRebalancer{} 28 29 hr := newHookRebalancer(mockRb) 30 idx := hr.RegisterRebalanceHook(func(ch *discovery.Change) {}) 31 test.Assert(t, idx == 0) 32 idx = hr.RegisterRebalanceHook(func(ch *discovery.Change) {}) 33 test.Assert(t, idx == 1) 34 idx = hr.RegisterRebalanceHook(func(ch *discovery.Change) {}) 35 test.Assert(t, idx == 2) 36 hr.DeregisterRebalanceHook(idx) 37 idx = hr.RegisterRebalanceHook(func(ch *discovery.Change) {}) 38 test.Assert(t, idx == 3) 39 idx = hr.RegisterRebalanceHook(func(ch *discovery.Change) {}) 40 test.Assert(t, idx == 4) 41 hr.DeregisterRebalanceHook(idx) 42 hr.DeregisterRebalanceHook(10) 43 } 44 45 func TestHookableDelete(t *testing.T) { 46 mockRb := &mockRebalancer{} 47 48 hr := newHookRebalancer(mockRb) 49 idx := hr.RegisterDeleteHook(func(ch *discovery.Change) {}) 50 test.Assert(t, idx == 0) 51 idx = hr.RegisterDeleteHook(func(ch *discovery.Change) {}) 52 test.Assert(t, idx == 1) 53 idx = hr.RegisterDeleteHook(func(ch *discovery.Change) {}) 54 test.Assert(t, idx == 2) 55 hr.DeregisterDeleteHook(idx) 56 idx = hr.RegisterDeleteHook(func(ch *discovery.Change) {}) 57 test.Assert(t, idx == 3) 58 idx = hr.RegisterDeleteHook(func(ch *discovery.Change) {}) 59 test.Assert(t, idx == 4) 60 hr.DeregisterDeleteHook(idx) 61 hr.DeregisterDeleteHook(10) 62 }