-
Notifications
You must be signed in to change notification settings - Fork 478
tetragon: handle test events sequentially #4291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kevsecurity
wants to merge
1
commit into
main
Choose a base branch
from
pr/kevsecurity/handle-test-events-sequentially
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−34
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,83 +100,77 @@ func ProcessEvents(t *testing.T, ctx context.Context, eventFn EventFn, wgStarted | |
|
|
||
| wgStarted.Done() | ||
|
|
||
| errChan := make(chan error) | ||
| errChan := make(chan error, 2) | ||
| defer close(errChan) | ||
|
|
||
| complChan := make(chan bool) | ||
| complChan := make(chan bool, 2) | ||
| defer close(complChan) | ||
|
|
||
| // Create an events queue that both the ring buffers can send to. | ||
| // This means we can handle the events sequentially by reading from the | ||
| // queue, avoiding issues of handling events concurrently. | ||
| type testEvent struct { | ||
| RawSample *[]byte | ||
| ComplChecker *testsensor.CompletionChecker | ||
| Ctx context.Context | ||
| CancelFunc context.CancelFunc | ||
| } | ||
|
|
||
| eventsQueue := make(chan *testEvent, 65536) // arbitrary size for tests | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(1) | ||
| ctxPerfRing, cancelPerfRing := context.WithCancel(ctx) | ||
| defer wg.Wait() | ||
|
|
||
| go func() { | ||
| defer wg.Done() | ||
|
|
||
| complChecker := testsensor.NewCompletionChecker() | ||
|
|
||
| for ctx.Err() == nil { | ||
| for ctxPerfRing.Err() == nil { | ||
|
|
||
| record, err := perfReader.Read() | ||
| if err != nil { | ||
| if ctx.Err() == nil && !errors.Is(err, os.ErrClosed) { | ||
| if ctxPerfRing.Err() == nil && !errors.Is(err, os.ErrClosed) { | ||
| errChan <- fmt.Errorf("error reading perfring data: %w", err) | ||
| } | ||
| break | ||
| } | ||
|
|
||
| if len(record.RawSample) > 0 { | ||
| _, events, handlerErr := observer.HandlePerfData(record.RawSample) | ||
| if handlerErr != nil { | ||
| errChan <- fmt.Errorf("error handling perfring data: %w", handlerErr) | ||
| break | ||
| } | ||
| err = loopEvents(events, eventFn, complChecker) | ||
| if err != nil { | ||
| errChan <- fmt.Errorf("error loop event function returned: %w", err) | ||
| break | ||
| } | ||
| if complChecker.Done() || ctxPerfRing.Err() != nil { | ||
| break | ||
| } | ||
|
|
||
| if complChecker.Done() { | ||
| complChan <- true | ||
| break | ||
| if len(record.RawSample) > 0 { | ||
| eventsQueue <- &testEvent{&record.RawSample, complChecker, ctxPerfRing, cancelPerfRing} | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| if useBPFRingBuffer { | ||
| // Service the BPF ring buffer. | ||
| ctxBPFRing, cancelBPFRing := context.WithCancel(ctx) | ||
| wg.Go(func() { | ||
|
|
||
| complChecker := testsensor.NewCompletionChecker() | ||
|
|
||
| for ctx.Err() == nil { | ||
| for ctxBPFRing.Err() == nil { | ||
|
|
||
| record, err := ringBufReader.Read() | ||
| if err != nil { | ||
| if ctx.Err() == nil && !errors.Is(err, os.ErrClosed) { | ||
| if ctxBPFRing.Err() == nil && !errors.Is(err, os.ErrClosed) { | ||
| errChan <- fmt.Errorf("error reading ringbuf data: %w", err) | ||
| } | ||
| break | ||
| } | ||
|
|
||
| if len(record.RawSample) > 0 { | ||
| _, events, handlerErr := observer.HandlePerfData(record.RawSample) | ||
| if handlerErr != nil { | ||
| errChan <- fmt.Errorf("error handling ringbuf data: %w", handlerErr) | ||
| break | ||
| } | ||
| err = loopEvents(events, eventFn, complChecker) | ||
| if err != nil { | ||
| errChan <- fmt.Errorf("error loop event function returned: %w", err) | ||
| break | ||
| } | ||
| if complChecker.Done() || ctxBPFRing.Err() != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly for this error here. |
||
| break | ||
| } | ||
|
|
||
| if complChecker.Done() { | ||
| complChan <- true | ||
| break | ||
| if len(record.RawSample) > 0 { | ||
| eventsQueue <- &testEvent{&record.RawSample, complChecker, ctxBPFRing, cancelBPFRing} | ||
| } | ||
| } | ||
| }) | ||
|
|
@@ -189,6 +183,21 @@ func ProcessEvents(t *testing.T, ctx context.Context, eventFn EventFn, wgStarted | |
| } | ||
| for { | ||
| select { | ||
| case event := <-eventsQueue: | ||
| _, events, handlerErr := observer.HandlePerfData(*event.RawSample) | ||
| if handlerErr != nil { | ||
| errChan <- fmt.Errorf("error handling ringbuf data: %w", handlerErr) | ||
| break | ||
| } | ||
| err = loopEvents(events, eventFn, event.ComplChecker) | ||
| if err != nil { | ||
| errChan <- fmt.Errorf("error loop event function returned: %w", err) | ||
| break | ||
| } | ||
| if event.ComplChecker.Done() && event.Ctx.Err() == nil { | ||
| event.CancelFunc() | ||
| complChan <- true | ||
| } | ||
| case err := <-errChan: | ||
| t.Fatal(err) | ||
| case <-complChan: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if there is a
ctxPerfRingerror? Do we propagate it to the test somehow?The benefit of having
t *testing.Tis that we can just fail the test, but we do not seem to be doing that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use ctxPerfRing only to indicate that we are done. I don't think we need to propagate any errors. What errors did you have in mind?