Skip to content

Commit

Permalink
test: add parallel execution tests (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-gorules authored Feb 14, 2024
1 parent 257785e commit d683d92
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
32 changes: 32 additions & 0 deletions decision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/gorules/zen-go"
"github.com/stretchr/testify/assert"
"sync"
"testing"
)

Expand Down Expand Up @@ -59,3 +60,34 @@ func TestDecision_Evaluate(t *testing.T) {
decision.Dispose()
}
}

func TestDecision_EvaluateParallel(t *testing.T) {
engine := zen.NewEngine(zen.EngineConfig{Loader: readTestFile})
defer engine.Dispose()

type responseData struct {
Output int `json:"output"`
}

var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
current := i
go func() {
defer wg.Done()

decision, err := engine.GetDecision("function.json")
assert.NoError(t, err)
defer decision.Dispose()

resp, err := decision.Evaluate(map[string]any{"input": current})
assert.NoError(t, err)

var respData responseData
assert.NoError(t, json.Unmarshal(resp.Result, &respData))
assert.Equal(t, current*2, respData.Output)
}()
}

wg.Wait()
}
28 changes: 28 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"os"
"path"
"sync"
"testing"

"github.com/gorules/zen-go"
Expand Down Expand Up @@ -154,3 +155,30 @@ func TestEngine_ErrorTransparency(t *testing.T) {
assert.ErrorContains(t, err, "myKey")
assert.ErrorContains(t, err, errorStr)
}

func TestEngine_EvaluateParallel(t *testing.T) {
engine := zen.NewEngine(zen.EngineConfig{Loader: readTestFile})
defer engine.Dispose()

type responseData struct {
Output int `json:"output"`
}

var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
current := i
go func() {
defer wg.Done()

resp, err := engine.Evaluate("function.json", map[string]any{"input": current})
assert.NoError(t, err)

var respData responseData
assert.NoError(t, json.Unmarshal(resp.Result, &respData))
assert.Equal(t, current*2, respData.Output)
}()
}

wg.Wait()
}

0 comments on commit d683d92

Please sign in to comment.