-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add main runloop with fake command executor
- Loading branch information
Showing
4 changed files
with
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cron | ||
|
||
type Executor interface { | ||
Execute(command string) error | ||
} | ||
|
||
type FakeExecutor struct { | ||
Commands []string | ||
} | ||
|
||
func (executor *FakeExecutor) Execute(command string) error { | ||
executor.Commands = append(executor.Commands, command) | ||
return nil | ||
} | ||
|
||
func CreateFakeExecutor() *FakeExecutor { | ||
return &FakeExecutor{make([]string, 0)} | ||
} |
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,6 @@ | ||
package cron | ||
|
||
type Job struct { | ||
Schedule Schedule | ||
Command string | ||
} |
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,41 @@ | ||
package cron | ||
|
||
import( | ||
"time" | ||
) | ||
|
||
type Server struct { | ||
executor Executor | ||
ticker *time.Ticker | ||
jobs []Job | ||
} | ||
|
||
func Start(executor Executor, jobs []Job, interval time.Duration) *Server { | ||
ticker := time.NewTicker(interval) | ||
|
||
server := &Server{ | ||
executor: executor, | ||
jobs: jobs, | ||
ticker: ticker, | ||
} | ||
|
||
go func () { | ||
for now := range ticker.C { | ||
server.Once(now) | ||
} | ||
}() | ||
|
||
return server | ||
} | ||
|
||
func (server *Server) Once(now time.Time) { | ||
for _, job := range server.jobs { | ||
if job.Schedule.ShouldRun(now) { | ||
server.executor.Execute(job.Command) | ||
} | ||
} | ||
} | ||
|
||
func (server *Server) Stop() { | ||
server.ticker.Stop() | ||
} |
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,69 @@ | ||
package cron | ||
|
||
import( | ||
"testing" | ||
"time" | ||
) | ||
|
||
|
||
func TestOnceWithNoJobsDoesNothing(t *testing.T) { | ||
executor := CreateFakeExecutor() | ||
server := &Server{ | ||
executor: executor, | ||
jobs: make([]Job, 0), | ||
} | ||
|
||
server.Once(time.Now()) | ||
|
||
if len(executor.Commands) > 0 { | ||
t.Error("Should not have run commands") | ||
} | ||
} | ||
|
||
func TestOnceWithAWildcardJobRunsIt(t *testing.T) { | ||
executor := CreateFakeExecutor() | ||
server := &Server{ | ||
executor: executor, | ||
jobs: []Job{ | ||
Job{Parse("* * * * * *"), "echo hi"}, | ||
}, | ||
} | ||
|
||
server.Once(time.Now()) | ||
|
||
if len(executor.Commands) != 1 { | ||
t.Error("Should have run 1 command") | ||
} | ||
|
||
if executor.Commands[0] != "echo hi" { | ||
t.Error("Should have run 'echo hi'") | ||
} | ||
} | ||
|
||
func TestStartStopWithSingleJob(t *testing.T) { | ||
executor := CreateFakeExecutor() | ||
server := Start( | ||
executor, | ||
[]Job{Job{Parse("* * * * * *"), "echo hi"}}, | ||
time.Millisecond * 10, | ||
) | ||
|
||
timer := time.NewTimer(time.Millisecond * 12) | ||
<-timer.C | ||
|
||
server.Stop() | ||
|
||
if len(executor.Commands) != 1 { | ||
t.Error("Should have run 1 command") | ||
} | ||
|
||
if executor.Commands[0] != "echo hi" { | ||
t.Error("Should have run 'echo hi'") | ||
} | ||
|
||
timer = time.NewTimer(time.Millisecond * 12) | ||
<-timer.C | ||
if len(executor.Commands) != 1 { | ||
t.Error("Should have run 1 command") | ||
} | ||
} |