Skip to content

Commit

Permalink
Events mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanthvn committed Aug 22, 2023
1 parent 8ab0bbe commit e8e4d89
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 12 deletions.
38 changes: 26 additions & 12 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,26 @@ import (

var log = logger.Get()

type Events struct {
type Events interface {
InitRingBuffer(mapFDlist []int) (map[int]chan []byte, error)
SetupRingBuffer(mapFD int, maxEntries uint32) (chan []byte, error)
CleanupRingBuffer()
reconcileEventsDataChannel()
ReadRingBuffer(eventRing *RingBuffer)
parseBuffer(consumerPosition uint64, eventRing *RingBuffer) bool
}

var _ Events = &events{}

func New() Events {
return &events{
PageSize: os.Getpagesize(),
RingCnt: 0,
}

}

type events struct {
RingBuffers []*RingBuffer
PageSize int
RingCnt int
Expand Down Expand Up @@ -62,18 +81,13 @@ func isValidMapFDList(mapFDlist []int) bool {
return true
}

func InitRingBuffer(mapFDlist []int) (map[int]chan []byte, error) {
func (ev *events) InitRingBuffer(mapFDlist []int) (map[int]chan []byte, error) {

// Validate mapFD
if !isValidMapFDList(mapFDlist) {
return nil, fmt.Errorf("mapFDs passed to InitRingBuffer is invalid")
}

ev := &Events{
PageSize: os.Getpagesize(),
RingCnt: 0,
}

epoll, err := poller.NewEventPoller()
if err != nil {
return nil, fmt.Errorf("failed to create epoll instance: %s", err)
Expand Down Expand Up @@ -101,7 +115,7 @@ func InitRingBuffer(mapFDlist []int) (map[int]chan []byte, error) {
return ringBufferChanList, nil
}

func (ev *Events) SetupRingBuffer(mapFD int, maxEntries uint32) (chan []byte, error) {
func (ev *events) SetupRingBuffer(mapFD int, maxEntries uint32) (chan []byte, error) {
ringbuffer := &RingBuffer{
RingBufferMapFD: mapFD,
Mask: uint64(maxEntries - 1),
Expand Down Expand Up @@ -148,7 +162,7 @@ func (ev *Events) SetupRingBuffer(mapFD int, maxEntries uint32) (chan []byte, er
return ev.eventsDataChannel, nil
}

func (ev *Events) CleanupRingBuffer() {
func (ev *events) CleanupRingBuffer() {

for i := 0; i < ev.RingCnt; i++ {
_ = unix.Munmap(ev.RingBuffers[i].Producer)
Expand All @@ -165,7 +179,7 @@ func (ev *Events) CleanupRingBuffer() {
return
}

func (ev *Events) reconcileEventsDataChannel() {
func (ev *events) reconcileEventsDataChannel() {

pollerCh := ev.epoller.EpollStart()
defer func() {
Expand All @@ -188,15 +202,15 @@ func (ev *Events) reconcileEventsDataChannel() {
}

// Similar to libbpf poll ring
func (ev *Events) ReadRingBuffer(eventRing *RingBuffer) {
func (ev *events) ReadRingBuffer(eventRing *RingBuffer) {
readDone := true
consPosition := eventRing.getConsumerPosition()
for !readDone {
readDone = ev.parseBuffer(consPosition, eventRing)
}
}

func (ev *Events) parseBuffer(consumerPosition uint64, eventRing *RingBuffer) bool {
func (ev *events) parseBuffer(consumerPosition uint64, eventRing *RingBuffer) bool {
readDone := true
producerPosition := eventRing.getProducerPosition()
for consumerPosition < producerPosition {
Expand Down
15 changes: 15 additions & 0 deletions pkg/events/generate_mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package events

//go:generate go run github.com/golang/mock/mockgen -destination mocks/events_mocks.go . Events
115 changes: 115 additions & 0 deletions pkg/events/mocks/events_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e8e4d89

Please sign in to comment.