Skip to content

Commit

Permalink
add main runloop with fake command executor
Browse files Browse the repository at this point in the history
  • Loading branch information
pitluga committed Jan 6, 2016
1 parent 5b5556b commit e1e5a1b
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
18 changes: 18 additions & 0 deletions executor.go
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)}
}
6 changes: 6 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cron

type Job struct {
Schedule Schedule
Command string
}
41 changes: 41 additions & 0 deletions server.go
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()
}
69 changes: 69 additions & 0 deletions server_test.go
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")
}
}

0 comments on commit e1e5a1b

Please sign in to comment.