-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from adhocore/18-batch-check
Batch due check
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package gronx | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Expr represents an item in array for batch check | ||
type Expr struct { | ||
Expr string | ||
Due bool | ||
Err error | ||
segs []string | ||
} | ||
|
||
// BatchDue checks if multiple expressions are due for given time (or now). | ||
// It returns []Expr with filled in Due and Err values. | ||
func (g *Gronx) BatchDue(exprs []string, ref ...time.Time) []Expr { | ||
if len(ref) > 0 { | ||
g.C.SetRef(ref[0]) | ||
} else { | ||
g.C.SetRef(time.Now()) | ||
} | ||
|
||
batch := make([]Expr, len(exprs)) | ||
for i := range exprs { | ||
if batch[i].segs, batch[i].Err = Segments(exprs[i]); batch[i].Err != nil { | ||
continue | ||
} | ||
due := true | ||
for pos, seg := range batch[i].segs { | ||
if seg != "*" && seg != "?" { | ||
if due, batch[i].Err = g.C.CheckDue(seg, pos); !due || batch[i].Err != nil { | ||
break | ||
} | ||
} | ||
} | ||
batch[i].Due = due | ||
} | ||
return batch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package gronx | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBatch(t *testing.T) { | ||
gron := New() | ||
|
||
t.Run("batch no error", func(t *testing.T) { | ||
ref := time.Now() | ||
exprs := []string{"@everysecond", "* * * * * *", "* * * * * *"} | ||
exprs = append(exprs, fmt.Sprintf("* %d * * * * %d", ref.Minute(), ref.Year())) | ||
exprs = append(exprs, fmt.Sprintf("* * * * * * %d-%d", ref.Year()-1, ref.Year()+1)) | ||
|
||
for _, expr := range gron.BatchDue(exprs) { | ||
if expr.Err != nil { | ||
t.Errorf("%s error: %#v", expr.Expr, expr.Err) | ||
} | ||
if !expr.Due { | ||
t.Errorf("%s must be due", expr.Expr) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("batch error", func(t *testing.T) { | ||
exprs := []string{"* * * *", "A B C D E F"} | ||
ref, _ := time.Parse(FullDateFormat, "2022-02-02 02:02:02") | ||
for _, expr := range gron.BatchDue(exprs, ref) { | ||
if expr.Err == nil { | ||
t.Errorf("%s expected error", expr.Expr) | ||
} | ||
if expr.Due { | ||
t.Errorf("%s must not be due when there is error", expr.Expr) | ||
} | ||
} | ||
}) | ||
} |