Skip to content
This repository was archived by the owner on Jan 11, 2019. It is now read-only.

Commit 88b0669

Browse files
authored
Merge pull request #30 from dadgar/b-concurrent
Allow library to be used in parallel
2 parents d520615 + 675cac9 commit 88b0669

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

cronexpr_parse.go

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"regexp"
2020
"sort"
2121
"strings"
22+
"sync"
2223
)
2324

2425
/******************************************************************************/
@@ -194,6 +195,7 @@ var (
194195
fieldFinder = regexp.MustCompile(`\S+`)
195196
entryFinder = regexp.MustCompile(`[^,]+`)
196197
layoutRegexp = make(map[string]*regexp.Regexp)
198+
layoutRegexpLock sync.Mutex
197199
)
198200

199201
/******************************************************************************/
@@ -488,6 +490,9 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error)
488490
/******************************************************************************/
489491

490492
func makeLayoutRegexp(layout, value string) *regexp.Regexp {
493+
layoutRegexpLock.Lock()
494+
defer layoutRegexpLock.Unlock()
495+
491496
layout = strings.Replace(layout, `%value%`, value, -1)
492497
re := layoutRegexp[layout]
493498
if re == nil {

cronexpr_test.go

+17-19
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
*
1111
*/
1212

13-
package cronexpr_test
13+
package cronexpr
1414

1515
/******************************************************************************/
1616

1717
import (
1818
"testing"
1919
"time"
20-
21-
"github.com/gorhill/cronexpr"
2220
)
2321

2422
/******************************************************************************/
@@ -203,9 +201,9 @@ func TestExpressions(t *testing.T) {
203201
for _, test := range crontests {
204202
for _, times := range test.times {
205203
from, _ := time.Parse("2006-01-02 15:04:05", times.from)
206-
expr, err := cronexpr.Parse(test.expr)
204+
expr, err := Parse(test.expr)
207205
if err != nil {
208-
t.Errorf(`cronexpr.Parse("%s") returned "%s"`, test.expr, err.Error())
206+
t.Errorf(`Parse("%s") returned "%s"`, test.expr, err.Error())
209207
}
210208
next := expr.Next(from)
211209
nextstr := next.Format(test.layout)
@@ -220,17 +218,17 @@ func TestExpressions(t *testing.T) {
220218

221219
func TestZero(t *testing.T) {
222220
from, _ := time.Parse("2006-01-02", "2013-08-31")
223-
next := cronexpr.MustParse("* * * * * 1980").Next(from)
221+
next := MustParse("* * * * * 1980").Next(from)
224222
if next.IsZero() == false {
225223
t.Error(`("* * * * * 1980").Next("2013-08-31").IsZero() returned 'false', expected 'true'`)
226224
}
227225

228-
next = cronexpr.MustParse("* * * * * 2050").Next(from)
226+
next = MustParse("* * * * * 2050").Next(from)
229227
if next.IsZero() == true {
230228
t.Error(`("* * * * * 2050").Next("2013-08-31").IsZero() returned 'true', expected 'false'`)
231229
}
232230

233-
next = cronexpr.MustParse("* * * * * 2099").Next(time.Time{})
231+
next = MustParse("* * * * * 2099").Next(time.Time{})
234232
if next.IsZero() == false {
235233
t.Error(`("* * * * * 2014").Next(time.Time{}).IsZero() returned 'true', expected 'false'`)
236234
}
@@ -247,7 +245,7 @@ func TestNextN(t *testing.T) {
247245
"Sat, 29 Nov 2014 00:00:00",
248246
}
249247
from, _ := time.Parse("2006-01-02 15:04:05", "2013-09-02 08:44:30")
250-
result := cronexpr.MustParse("0 0 * * 6#5").NextN(from, uint(len(expected)))
248+
result := MustParse("0 0 * * 6#5").NextN(from, uint(len(expected)))
251249
if len(result) != len(expected) {
252250
t.Errorf(`MustParse("0 0 * * 6#5").NextN("2013-09-02 08:44:30", 5):\n"`)
253251
t.Errorf(` Expected %d returned time values but got %d instead`, len(expected), len(result))
@@ -270,38 +268,38 @@ func TestNextN_every5min(t *testing.T) {
270268
"Mon, 2 Sep 2013 09:05:00",
271269
}
272270
from, _ := time.Parse("2006-01-02 15:04:05", "2013-09-02 08:44:32")
273-
result := cronexpr.MustParse("*/5 * * * *").NextN(from, uint(len(expected)))
271+
result := MustParse("*/5 * * * *").NextN(from, uint(len(expected)))
274272
if len(result) != len(expected) {
275273
t.Errorf(`MustParse("*/5 * * * *").NextN("2013-09-02 08:44:30", 5):\n"`)
276274
t.Errorf(` Expected %d returned time values but got %d instead`, len(expected), len(result))
277275
}
278276
for i, next := range result {
279277
nextStr := next.Format("Mon, 2 Jan 2006 15:04:05")
280278
if nextStr != expected[i] {
281-
t.Errorf(`MustParse("*/5 * * * *").NextN("2013-09-02 08:44:30", 5):\n"`)
279+
t.Errorf(`MustParse("*/5 * * * *").NextN("2013-09-02 08:44:30", 5):\n"`)
282280
t.Errorf(` result[%d]: expected "%s" but got "%s"`, i, expected[i], nextStr)
283281
}
284282
}
285283
}
286284

287285
// Issue: https://github.com/gorhill/cronexpr/issues/16
288-
func TestInterval_Interval60Issue(t *testing.T){
289-
_, err := cronexpr.Parse("*/60 * * * * *")
286+
func TestInterval_Interval60Issue(t *testing.T) {
287+
_, err := Parse("*/60 * * * * *")
290288
if err == nil {
291289
t.Errorf("parsing with interval 60 should return err")
292290
}
293291

294-
_, err = cronexpr.Parse("*/61 * * * * *")
292+
_, err = Parse("*/61 * * * * *")
295293
if err == nil {
296294
t.Errorf("parsing with interval 61 should return err")
297295
}
298296

299-
_, err = cronexpr.Parse("2/60 * * * * *")
297+
_, err = Parse("2/60 * * * * *")
300298
if err == nil {
301299
t.Errorf("parsing with interval 60 should return err")
302300
}
303301

304-
_, err = cronexpr.Parse("2-20/61 * * * * *")
302+
_, err = Parse("2-20/61 * * * * *")
305303
if err == nil {
306304
t.Errorf("parsing with interval 60 should return err")
307305
}
@@ -322,14 +320,14 @@ var benchmarkExpressionsLen = len(benchmarkExpressions)
322320

323321
func BenchmarkParse(b *testing.B) {
324322
for i := 0; i < b.N; i++ {
325-
_ = cronexpr.MustParse(benchmarkExpressions[i%benchmarkExpressionsLen])
323+
_ = MustParse(benchmarkExpressions[i%benchmarkExpressionsLen])
326324
}
327325
}
328326

329327
func BenchmarkNext(b *testing.B) {
330-
exprs := make([]*cronexpr.Expression, benchmarkExpressionsLen)
328+
exprs := make([]*Expression, benchmarkExpressionsLen)
331329
for i := 0; i < benchmarkExpressionsLen; i++ {
332-
exprs[i] = cronexpr.MustParse(benchmarkExpressions[i])
330+
exprs[i] = MustParse(benchmarkExpressions[i])
333331
}
334332
from := time.Now()
335333
b.ResetTimer()

example_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
*
99
*/
1010

11-
package cronexpr_test
11+
package cronexpr
1212

1313
/******************************************************************************/
1414

1515
import (
1616
"fmt"
1717
"time"
18-
19-
"github.com/gorhill/cronexpr"
2018
)
2119

2220
/******************************************************************************/
2321

2422
// ExampleMustParse
2523
func ExampleMustParse() {
2624
t := time.Date(2013, time.August, 31, 0, 0, 0, 0, time.UTC)
27-
nextTimes := cronexpr.MustParse("0 0 29 2 *").NextN(t, 5)
25+
nextTimes := MustParse("0 0 29 2 *").NextN(t, 5)
2826
for i := range nextTimes {
2927
fmt.Println(nextTimes[i].Format(time.RFC1123))
3028
// Output:

0 commit comments

Comments
 (0)