google.golang.org/grpc@v1.72.2/xds/internal/balancer/outlierdetection/callcounter_test.go (about) 1 /* 2 * 3 * Copyright 2022 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package outlierdetection 19 20 import ( 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 ) 25 26 func (b1 *bucket) Equal(b2 *bucket) bool { 27 if b1 == nil && b2 == nil { 28 return true 29 } 30 if (b1 != nil) != (b2 != nil) { 31 return false 32 } 33 if b1.numSuccesses != b2.numSuccesses { 34 return false 35 } 36 return b1.numFailures == b2.numFailures 37 } 38 39 func (cc *callCounter) Equal(cc2 *callCounter) bool { 40 if cc == nil && cc2 == nil { 41 return true 42 } 43 if (cc != nil) != (cc2 != nil) { 44 return false 45 } 46 ab1 := cc.activeBucket.Load() 47 ab2 := cc2.activeBucket.Load() 48 if !ab1.Equal(ab2) { 49 return false 50 } 51 return cc.inactiveBucket.Equal(cc2.inactiveBucket) 52 } 53 54 // TestClear tests that clear on the call counter clears (everything set to 0) 55 // the active and inactive buckets. 56 func (s) TestClear(t *testing.T) { 57 cc := newCallCounter() 58 ab := cc.activeBucket.Load() 59 ab.numSuccesses = 1 60 ab.numFailures = 2 61 cc.inactiveBucket.numSuccesses = 4 62 cc.inactiveBucket.numFailures = 5 63 cc.clear() 64 // Both the active and inactive buckets should be cleared. 65 ccWant := newCallCounter() 66 if diff := cmp.Diff(cc, ccWant); diff != "" { 67 t.Fatalf("callCounter is different than expected, diff (-got +want): %v", diff) 68 } 69 } 70 71 // TestSwap tests that swap() on the callCounter successfully has the desired 72 // end result of inactive bucket containing the previous active buckets data, 73 // and the active bucket being cleared. 74 func (s) TestSwap(t *testing.T) { 75 cc := newCallCounter() 76 ab := cc.activeBucket.Load() 77 ab.numSuccesses = 1 78 ab.numFailures = 2 79 cc.inactiveBucket.numSuccesses = 4 80 cc.inactiveBucket.numFailures = 5 81 ib := cc.inactiveBucket 82 cc.swap() 83 // Inactive should pick up active's data, active should be swapped to zeroed 84 // inactive. 85 ccWant := newCallCounter() 86 ccWant.inactiveBucket.numSuccesses = 1 87 ccWant.inactiveBucket.numFailures = 2 88 ccWant.activeBucket.Store(ib) 89 if diff := cmp.Diff(cc, ccWant); diff != "" { 90 t.Fatalf("callCounter is different than expected, diff (-got +want): %v", diff) 91 } 92 }