File tree Expand file tree Collapse file tree 3 files changed +34
-3
lines changed Expand file tree Collapse file tree 3 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -6,15 +6,18 @@ package jsonata
6
6
7
7
import (
8
8
"errors"
9
+ "fmt"
9
10
"math"
10
11
"reflect"
11
12
"regexp"
12
13
"sort"
13
14
"strings"
15
+ "sync"
14
16
"testing"
15
17
16
18
"github.com/stepzen-dev/jsonata-go/jparse"
17
19
"github.com/stepzen-dev/jsonata-go/jtypes"
20
+ "github.com/stretchr/testify/assert"
18
21
)
19
22
20
23
var (
@@ -2761,3 +2764,29 @@ func TestCallableParamCount(t *testing.T) {
2761
2764
}
2762
2765
}
2763
2766
}
2767
+
2768
+ func TestConcurrentGoCallable (t * testing.T ) {
2769
+ const src = `$contains(/^DEF-/)`
2770
+ n := 100
2771
+ var wg sync.WaitGroup
2772
+ for i := 0 ; i < n ; i ++ {
2773
+ id := fmt .Sprintf ("ABC-%d" , i )
2774
+ if i % 3 == 0 {
2775
+ id = fmt .Sprintf ("DEF-%d" , i )
2776
+ }
2777
+
2778
+ wg .Add (1 )
2779
+ go func () {
2780
+ defer wg .Done ()
2781
+ result , err := MustCompile (src ).Eval (id )
2782
+ assert .NoError (t , err )
2783
+
2784
+ if strings .HasPrefix (id , "DEF-" ) {
2785
+ assert .Equal (t , true , result )
2786
+ } else {
2787
+ assert .Equal (t , false , result )
2788
+ }
2789
+ }()
2790
+ }
2791
+ wg .Wait ()
2792
+ }
Original file line number Diff line number Diff line change 65
65
argCountEquals1 = jtypes .ArgCountEquals (1 )
66
66
)
67
67
68
- var baseEnv = initBaseEnv ( map [string ]Extension {
68
+ var standardFunctions = map [string ]Extension {
69
69
70
70
// String functions
71
71
@@ -385,7 +385,7 @@ var baseEnv = initBaseEnv(map[string]Extension{
385
385
UndefinedHandler : nil ,
386
386
EvalContextHandler : nil ,
387
387
},
388
- })
388
+ }
389
389
390
390
func initBaseEnv (exts map [string ]Extension ) * environment {
391
391
Original file line number Diff line number Diff line change @@ -232,7 +232,9 @@ func (e *Expr) newEnv(input reflect.Value) *environment {
232
232
233
233
tc := timeCallables (time .Now ())
234
234
235
- env := newEnvironment (baseEnv , len (tc )+ len (e .registry )+ 1 )
235
+ // create a new base environment (with the standard functions) to
236
+ // ensure each execution gets its own set of goCallables for functions.
237
+ env := newEnvironment (initBaseEnv (standardFunctions ), len (tc )+ len (e .registry )+ 1 )
236
238
237
239
env .bind ("$" , input )
238
240
env .bindAll (tc )
You can’t perform that action at this time.
0 commit comments