@@ -27,6 +27,8 @@ import (
2727 "github.com/falcosecurity/plugin-sdk-go/pkg/sdk"
2828)
2929
30+ const testAsyncMaxPlugins = 32 // note: must be <= cgo.MaxHandle
31+
3032type sampleAsyncExtract struct {
3133 sampleExtract
3234 counter uint64
@@ -156,7 +158,7 @@ func TestAsyncExtract(t *testing.T) {
156158 }
157159
158160 // run with increasing number of concurrent consumers
159- for i := 1 ; i <= cgo . MaxHandle ; i *= 2 {
161+ for i := 1 ; i <= testAsyncMaxPlugins ; i *= 2 {
160162 // run with increasing number of extractions
161163 for j := 1 ; j < 10000 ; j *= 10 {
162164 workload (i , j )
@@ -165,64 +167,77 @@ func TestAsyncExtract(t *testing.T) {
165167}
166168
167169func TestStartStopAsync (t * testing.T ) {
168- nPlugins := cgo .MaxHandle
169- testWithMockPlugins (nPlugins , func (handles []cgo.Handle ) {
170- // test unbalanced start/stop calls
171- assertPanic (t , func () {
172- a := asyncContext {}
173- a .StopAsync (handles [0 ], testReleaseAsyncBatch )
174- })
175- assertPanic (t , func () {
176- a := asyncContext {}
177- a .StartAsync (handles [0 ], testAllocAsyncBatch )
178- a .StopAsync (handles [0 ], testReleaseAsyncBatch )
179- a .StopAsync (handles [0 ], testReleaseAsyncBatch )
170+ nPlugins := testAsyncMaxPlugins
171+ // checking that an odd or event number of plugins is not relevant
172+ for i := nPlugins - 1 ; i <= nPlugins + 1 ; i ++ {
173+ t .Run (fmt .Sprintf ("unbalanced-startstop#%d" , i ), func (t * testing.T ) {
174+ testWithMockPlugins (nPlugins , func (handles []cgo.Handle ) {
175+ // test unbalanced start/stop calls
176+ assertPanic (t , func () {
177+ a := asyncContext {}
178+ a .StopAsync (handles [0 ], testReleaseAsyncBatch )
179+ })
180+ assertPanic (t , func () {
181+ a := asyncContext {}
182+ a .StartAsync (handles [0 ], testAllocAsyncBatch )
183+ a .StopAsync (handles [0 ], testReleaseAsyncBatch )
184+ a .StopAsync (handles [0 ], testReleaseAsyncBatch )
185+ })
186+
187+ // test with bad start/stop-handle pair
188+ assertPanic (t , func () {
189+ a := asyncContext {}
190+ a .StartAsync (handles [0 ], testAllocAsyncBatch )
191+ a .StartAsync (handles [1 ], testAllocAsyncBatch )
192+ a .StopAsync (handles [0 ], testReleaseAsyncBatch )
193+ a .StopAsync (handles [0 ], testReleaseAsyncBatch )
194+ })
195+ })
180196 })
181197
182- // test with bad start/stop-handle pair
183- assertPanic (t , func () {
184- a := asyncContext {}
185- a .StartAsync (handles [0 ], testAllocAsyncBatch )
186- a .StartAsync (handles [1 ], testAllocAsyncBatch )
187- a .StopAsync (handles [0 ], testReleaseAsyncBatch )
188- a .StopAsync (handles [0 ], testReleaseAsyncBatch )
189- })
190-
191- // test with inconsistent enabled values
192- a := asyncContext {}
193- enabled := true
194- for i := 0 ; i < nPlugins ; i ++ {
195- a .SetAsync (enabled )
196- a .StartAsync (handles [i ], testAllocAsyncBatch )
197- enabled = ! enabled
198- }
199- for i := 0 ; i < nPlugins ; i ++ {
200- a .StopAsync (handles [i ], testReleaseAsyncBatch )
201- }
198+ t .Run (fmt .Sprintf ("inconsistent-async#%d" , i ), func (t * testing.T ) {
199+ testWithMockPlugins (nPlugins , func (handles []cgo.Handle ) {
200+ // test with inconsistent enabled values
201+ a := asyncContext {}
202+ enabled := false
203+ for i := 0 ; i < nPlugins ; i ++ {
204+ a .SetAsync (enabled )
205+ a .StartAsync (handles [i ], testAllocAsyncBatch )
206+ enabled = ! enabled
207+ }
208+ for i := 0 ; i < nPlugins ; i ++ {
209+ a .StopAsync (handles [i ], testReleaseAsyncBatch )
210+ }
202211
203- // test workload after already having started/stopped the same context
204- var wg sync.WaitGroup
205- for _ , h := range handles {
206- wg .Add (1 )
207- a .StartAsync (h , testAllocAsyncBatch )
208- go func (h cgo.Handle ) {
209- counter := uint64 (0 )
210- field , freeField := allocSSPluginExtractField (1 , sdk .FieldTypeUint64 , "" , "" )
211- defer freeField ()
212- for e := 0 ; e < 1000 ; e ++ {
213- testSimulateAsyncRequest (t , & a , h , field )
214- value := * * ((* * uint64 )(unsafe .Pointer (& field .res [0 ])))
215- if value != counter {
216- panic (fmt .Sprintf ("extracted %d but expected %d" , value , counter ))
217- }
218- counter ++
212+ // test workload after already having started/stopped the same context
213+ var wg sync.WaitGroup
214+ for _ , h := range handles {
215+ wg .Add (1 )
216+ a .SetAsync (enabled )
217+ a .StartAsync (h , testAllocAsyncBatch )
218+ go func (h cgo.Handle , enabled bool ) {
219+ counter := uint64 (0 )
220+ field , freeField := allocSSPluginExtractField (1 , sdk .FieldTypeUint64 , "" , "" )
221+ defer freeField ()
222+ for e := 0 ; e < 1000 ; e ++ {
223+ if enabled {
224+ testSimulateAsyncRequest (t , & a , h , field )
225+ value := * * ((* * uint64 )(unsafe .Pointer (& field .res [0 ])))
226+ if value != counter {
227+ panic (fmt .Sprintf ("extracted %d but expected %d" , value , counter ))
228+ }
229+ }
230+ counter ++
231+ }
232+ wg .Done ()
233+ }(h , enabled )
234+ enabled = ! enabled
219235 }
220- wg .Done ()
221- }(h )
222- }
223- wg .Wait ()
224- for _ , h := range handles {
225- a .StopAsync (h , testReleaseAsyncBatch )
226- }
227- })
236+ wg .Wait ()
237+ for _ , h := range handles {
238+ a .StopAsync (h , testReleaseAsyncBatch )
239+ }
240+ })
241+ })
242+ }
228243}
0 commit comments