github.com/cilium/cilium@v1.16.2/pkg/bpf/events_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package bpf 5 6 import ( 7 "fmt" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/cilium/cilium/pkg/container" 14 ) 15 16 func TestEventsSubscribe(t *testing.T) { 17 assert := assert.New(t) 18 eb := &eventsBuffer{ 19 buffer: container.NewRingBuffer(0), 20 eventTTL: time.Second, 21 } 22 handle, err := eb.dumpAndSubscribe(nil, true) 23 assert.NoError(err) 24 25 // should not block, buffer not full. 26 eb.add(&Event{cacheEntry: cacheEntry{Key: IntTestKey(123)}}) 27 eb.add(&Event{cacheEntry: cacheEntry{Key: IntTestKey(124)}}) 28 eb.add(&Event{cacheEntry: cacheEntry{Key: IntTestKey(125)}}) 29 assert.Equal("key=123", (<-handle.C()).Key.String()) 30 assert.Equal("key=124", (<-handle.C()).Key.String()) 31 32 for i := 0; i < eventSubChanBufferSize; i++ { 33 assert.False(handle.isClosed(), "should not close until buffer is full") 34 assert.Len(eb.subscriptions, 1) 35 assert.Len(eb.subscriptions[0].c, i+1) 36 eb.add(&Event{cacheEntry: cacheEntry{Key: IntTestKey(i)}}) 37 } 38 time.Sleep(time.Millisecond * 20) 39 assert.True(handle.isClosed(), "after filling buffer, should be closed") 40 assert.Len(eb.subscriptions, 0) 41 42 handle, err = eb.dumpAndSubscribe(nil, true) 43 assert.NoError(err) 44 assert.False(handle.isClosed()) 45 handle.Close() 46 handle.Close() 47 assert.True(handle.isClosed(), "after calling close, should be closed") 48 assert.Equal(0, eb.buffer.Size()) 49 } 50 51 type IntTestKey uint32 52 53 func (k IntTestKey) String() string { return fmt.Sprintf("key=%d", k) } 54 func (k IntTestKey) New() MapKey { return new(IntTestKey) }