github.com/cilium/cilium@v1.16.2/pkg/endpointmanager/policymap_pressure_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package endpointmanager 5 6 import ( 7 "sync/atomic" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/cilium/cilium/pkg/endpoint" 14 ) 15 16 func TestPolicyMapPressure(t *testing.T) { 17 assert := assert.New(t) 18 policyMapPressureMinInterval = 0 19 p := newPolicyMapPressure() 20 p.gauge = &fakeGauge{} 21 assert.Equal(float64(0), p.gauge.(*fakeGauge).Load()) 22 p.Update(endpoint.PolicyMapPressureEvent{ 23 EndpointID: 1, 24 Value: .5, 25 }) 26 assertMetricEq := func(expected float64) { 27 assert.Eventually(func() bool { 28 return p.gauge.(*fakeGauge).Load() == expected 29 }, time.Second, 1*time.Millisecond) 30 } 31 assertMetricEq(.5) 32 p.Update(endpoint.PolicyMapPressureEvent{ 33 EndpointID: 2, 34 Value: 1, 35 }) 36 assertMetricEq(1) 37 p.Remove(2) 38 assertMetricEq(.5) 39 } 40 41 type fakeGauge struct { 42 lastValue atomic.Value 43 } 44 45 func (f *fakeGauge) Set(value float64) { 46 f.lastValue.Store(value) 47 } 48 49 func (f *fakeGauge) Load() float64 { 50 v := f.lastValue.Load() 51 if v == nil { 52 return 0 53 } 54 return v.(float64) 55 }