Skip to content

Commit

Permalink
Add concurrent tests with race checker
Browse files Browse the repository at this point in the history
It's already safe;  we're just adding extra checks.
  • Loading branch information
tonyhb committed Jan 5, 2024
1 parent 8e33502 commit b1b4210
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ func (a *aggregator) addGroup(ctx context.Context, node *Node, parsed *ParsedExp
}

func (a *aggregator) addNode(ctx context.Context, n *Node, gid groupID, parsed *ParsedExpression) error {
// Don't allow anything to update in parallel.
// Don't allow anything to update in parallel. This enrues that Add() can be called
// concurrently.
a.lock.Lock()
defer a.lock.Unlock()

Expand Down
48 changes: 48 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,54 @@ func TestEvaluate(t *testing.T) {
})
}

func TestEvaluate_Concurrently(t *testing.T) {
ctx := context.Background()
parser, err := NewTreeParser(NewCachingParser(newEnv(), nil))
require.NoError(t, err)
e := NewAggregateEvaluator(parser, testBoolEvaluator)

expected := tex(`event.data.account_id == "yes" && event.data.match == "true"`)
_, err = e.Add(ctx, expected)
require.NoError(t, err)

go func() {
for i := 0; i < 1_000; i++ {
//nolint:all
go func() {
byt := make([]byte, 8)
_, err := rand.Read(byt)
require.NoError(t, err)
str := hex.EncodeToString(byt)
_, err = e.Add(ctx, tex(fmt.Sprintf(`event.data.account_id == "%s"`, str)))
require.NoError(t, err)
}()
}
}()

t.Run("It matches items", func(t *testing.T) {
wg := sync.WaitGroup{}
for i := 0; i <= 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
evals, matched, err := e.Evaluate(ctx, map[string]any{
"event": map[string]any{
"data": map[string]any{
"account_id": "yes",
"match": "true",
},
},
})
require.NoError(t, err)
require.EqualValues(t, 1, matched)
require.EqualValues(t, []Evaluable{expected}, evals)
}()
}
wg.Wait()
})

}

func TestEvaluate_ArrayIndexes(t *testing.T) {
ctx := context.Background()
parser, err := NewTreeParser(NewCachingParser(newEnv(), nil))
Expand Down

0 comments on commit b1b4210

Please sign in to comment.