File tree 2 files changed +51
-4
lines changed
2 files changed +51
-4
lines changed Original file line number Diff line number Diff line change @@ -26,24 +26,24 @@ var (
26
26
)
27
27
28
28
func compileRegexp (pattern string ) (* re.Regexp , error ) {
29
+ cacheMutex .Lock ()
30
+ defer cacheMutex .Unlock ()
29
31
// Save repeated regexp compilation
30
32
if reDict [pattern ] != nil {
31
33
return reDict [pattern ], nil
32
34
}
33
35
var err error
34
- cacheMutex .Lock ()
35
36
reDict [pattern ], err = re .Compile (pattern )
36
- cacheMutex .Unlock ()
37
37
return reDict [pattern ], err
38
38
}
39
39
40
40
func mustCompileRegexp (pattern string ) * re.Regexp {
41
+ cacheMutex .Lock ()
42
+ defer cacheMutex .Unlock ()
41
43
// Save repeated regexp compilation, with panic on error
42
44
if reDict [pattern ] != nil {
43
45
return reDict [pattern ]
44
46
}
45
- defer cacheMutex .Unlock ()
46
- cacheMutex .Lock ()
47
47
reDict [pattern ] = re .MustCompile (pattern )
48
48
return reDict [pattern ]
49
49
}
Original file line number Diff line number Diff line change @@ -57,3 +57,50 @@ func Test_mustCompileRegexp(t *testing.T) {
57
57
58
58
assert .Panics (t , testPanic )
59
59
}
60
+
61
+ func TestRace_compileRegexp (t * testing.T ) {
62
+ vrex := new (re.Regexp )
63
+
64
+ patterns := []string {
65
+ ".*TestRegexp1.*" ,
66
+ ".*TestRegexp2.*" ,
67
+ ".*TestRegexp3.*" ,
68
+ }
69
+
70
+ comp := func (pattern string ) {
71
+ rex , err := compileRegexp (pattern )
72
+ assert .NoError (t , err )
73
+ assert .NotNil (t , rex )
74
+ assert .IsType (t , vrex , rex )
75
+ }
76
+
77
+ for i := 0 ; i < 20 ; i ++ {
78
+ t .Run (patterns [i % 3 ], func (t * testing.T ) {
79
+ t .Parallel ()
80
+ comp (patterns [i % 3 ])
81
+ })
82
+ }
83
+ }
84
+
85
+ func TestRace_mustCompileRegexp (t * testing.T ) {
86
+ vrex := new (re.Regexp )
87
+
88
+ patterns := []string {
89
+ ".*TestRegexp1.*" ,
90
+ ".*TestRegexp2.*" ,
91
+ ".*TestRegexp3.*" ,
92
+ }
93
+
94
+ comp := func (pattern string ) {
95
+ rex := mustCompileRegexp (pattern )
96
+ assert .NotNil (t , rex )
97
+ assert .IsType (t , vrex , rex )
98
+ }
99
+
100
+ for i := 0 ; i < 20 ; i ++ {
101
+ t .Run (patterns [i % 3 ], func (t * testing.T ) {
102
+ t .Parallel ()
103
+ comp (patterns [i % 3 ])
104
+ })
105
+ }
106
+ }
You can’t perform that action at this time.
0 commit comments