Skip to content

Commit

Permalink
move to planner_test
Browse files Browse the repository at this point in the history
I prefer to keep the `*_test.go` files under a testing package because
when you copy paste part of the code it imports the right dependencies
and it makes the test easier to read.
  • Loading branch information
Gianluca Arbezzano committed Oct 6, 2020
1 parent bf8ea31 commit 5703cac
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions scheduer_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package planner
package planner_test

import (
"context"
"strings"
"testing"
"time"

"github.com/gianarb/planner"
)

const Seq = "seq"

type FakePlan struct {
P []Procedure
P []planner.Procedure
Counter int
}

func (p *FakePlan) Create(ctx context.Context) ([]Procedure, error) {
func (p *FakePlan) Create(ctx context.Context) ([]planner.Procedure, error) {
if p.Counter > 0 {
return nil, nil
}
Expand All @@ -35,13 +37,13 @@ func (o *Two) Name() string {
return "two"
}

func (o *Two) Do(ctx context.Context) ([]Procedure, error) {
func (o *Two) Do(ctx context.Context) ([]planner.Procedure, error) {
if o.Counter > 0 {
return nil, nil
}
o.Seq <- "two"
o.Counter++
return []Procedure{
return []planner.Procedure{
&One{
String: "from-two",
Seq: o.Seq,
Expand All @@ -58,13 +60,13 @@ func (o *One) Name() string {
return "one"
}

func (o *One) Do(ctx context.Context) ([]Procedure, error) {
func (o *One) Do(ctx context.Context) ([]planner.Procedure, error) {
o.Seq <- "one"
return nil, nil
}

type FakeStep struct {
do func(ctx context.Context) ([]Procedure, error)
do func(ctx context.Context) ([]planner.Procedure, error)
name string
}

Expand All @@ -75,7 +77,7 @@ func (o *FakeStep) Name() string {
return o.name
}

func (o *FakeStep) Do(ctx context.Context) ([]Procedure, error) {
func (o *FakeStep) Do(ctx context.Context) ([]planner.Procedure, error) {
return o.do(ctx)
}

Expand All @@ -84,24 +86,24 @@ func TestTriggerSchedulerTimeout(t *testing.T) {
ctx, cF := context.WithTimeout(ctx, 200*time.Millisecond)
defer cF()
p := &FakePlan{
P: []Procedure{},
P: []planner.Procedure{},
Counter: 0,
}
p.P = append(p.P, &FakeStep{
name: "sleep",
do: func(ctx context.Context) ([]Procedure, error) {
do: func(ctx context.Context) ([]planner.Procedure, error) {
time.Sleep(210 * time.Millisecond)
return nil, nil
},
})
p.P = append(p.P, &FakeStep{
name: "sleep",
do: func(ctx context.Context) ([]Procedure, error) {
do: func(ctx context.Context) ([]planner.Procedure, error) {
time.Sleep(210 * time.Millisecond)
return nil, nil
},
})
s := NewScheduler()
s := planner.NewScheduler()
err := s.Execute(ctx, p)
if err != context.DeadlineExceeded {
t.Fatalf("expected to get an deadline exceeded error. we got %s", err)
Expand All @@ -114,13 +116,13 @@ func TestExecutionSingleStep(t *testing.T) {

ctx := context.Background()
p := &FakePlan{
P: []Procedure{},
P: []planner.Procedure{},
Counter: 0,
}
p.P = append(p.P, &One{
Seq: seq,
})
s := NewScheduler()
s := planner.NewScheduler()
s.Execute(ctx, p)
close(seq)
for ss := range seq {
Expand All @@ -137,13 +139,13 @@ func TestExecutionStepTwoThatReturnStepOne(t *testing.T) {

ctx := context.Background()
p := &FakePlan{
P: []Procedure{},
P: []planner.Procedure{},
Counter: 0,
}
p.P = append(p.P, &Two{
Seq: seq,
})
s := NewScheduler()
s := planner.NewScheduler()
s.Execute(ctx, p)
close(seq)
for ss := range seq {
Expand All @@ -160,7 +162,7 @@ func TestExecutionStepTwoAndStapOne(t *testing.T) {

ctx := context.Background()
p := &FakePlan{
P: []Procedure{},
P: []planner.Procedure{},
Counter: 0,
}
p.P = append(p.P, &Two{
Expand All @@ -170,7 +172,7 @@ func TestExecutionStepTwoAndStapOne(t *testing.T) {
Seq: seq,
String: "plan",
})
s := NewScheduler()
s := planner.NewScheduler()
s.Execute(ctx, p)
close(seq)
for ss := range seq {
Expand Down

0 comments on commit 5703cac

Please sign in to comment.