Skip to content

Commit

Permalink
Merge pull request #7 from saantiaguilera/feature/sa/main_samples
Browse files Browse the repository at this point in the history
Refactor _test for main packaged samples
  • Loading branch information
saantiaguilera authored Feb 12, 2020
2 parents db18e5c + 92e357f commit 06e6bf5
Show file tree
Hide file tree
Showing 49 changed files with 413 additions and 357 deletions.
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
yml: ./codecov.yml
19 changes: 19 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
coverage:
precision: 2
round: down
range: "70...100"

parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no

comment:
layout: "reach,diff,flags,tree"
behavior: default

ignore:
- "examples/**/*" # Ignore the examples files.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -24,7 +24,7 @@ func (s *boilEggsStep) Run(ctx pipeline.Context) error {
return nil
}

func CreateBoilEggsStep(eggs int, eggsChan chan int) pipeline.Step {
func createBoilEggsStep(eggs int, eggsChan chan int) pipeline.Step {
return &boilEggsStep{
Eggs: eggs,
Stream: eggsChan,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -25,7 +25,7 @@ func (s *cutCarrotsStep) Run(ctx pipeline.Context) error {
return nil
}

func CreateCutCarrotsStep(carrotsChan chan int) pipeline.Step {
func createCutCarrotsStep(carrotsChan chan int) pipeline.Step {
return &cutCarrotsStep{
Stream: carrotsChan,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -25,7 +25,7 @@ func (s *cutEggsStep) Run(ctx pipeline.Context) error {
return nil
}

func CreateCutEggsStep(eggsChan chan int) pipeline.Step {
func createCutEggsStep(eggsChan chan int) pipeline.Step {
return &cutEggsStep{
Stream: eggsChan,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -25,27 +25,27 @@ func (s *cutMeatStep) Run(ctx pipeline.Context) error {
return nil
}

func CreateCutMeatStep(meatSize, ovenSize int, meatChan chan int) pipeline.Step {
func createCutMeatStep(meatSize, ovenSize int, meatChan chan int) pipeline.Step {
return &cutMeatStep{
MeatSize: meatSize,
OvenSize: ovenSize,
Stream: meatChan,
}
}

func CreateMeatTooBigStatement(meatSize, ovenSize int) func(ctx pipeline.Context) bool {
s := &MeatTooBig{
func createMeatTooBigStatement(meatSize, ovenSize int) func(ctx pipeline.Context) bool {
s := &meatTooBig{
MeatSize: meatSize,
OvenSize: ovenSize,
}
return s.IsMeatTooBigForTheOven
return s.isMeatTooBigForTheOven
}

type MeatTooBig struct {
type meatTooBig struct {
MeatSize int
OvenSize int
}

func (m *MeatTooBig) IsMeatTooBigForTheOven(ctx pipeline.Context) bool {
func (m *meatTooBig) isMeatTooBigForTheOven(ctx pipeline.Context) bool {
return m.MeatSize > m.OvenSize
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package cook_example_test
package main

import (
"flag"
"fmt"
"os"
"testing"

"github.com/saantiaguilera/go-pipeline"
"github.com/stretchr/testify/assert"
)

var render = flag.Bool("pipeline.render", false, "render pipeline")

// Graph creates a dynamic workflow for this sample. It's all in a single func completely coupled for showing purposes
// you should probably decouple this into more atomic ones (eg. a func for making the salad that returns a Stage)
func Graph(numberOfCarrots, numberOfEggs, meatSize, ovenSize int) pipeline.Stage {
// Channels as a mean for communicating input / output.
// Use whatever you prefer here. It doesn't even have to be channels, but this is an example.
Expand All @@ -31,18 +31,18 @@ func Graph(numberOfCarrots, numberOfEggs, meatSize, ovenSize int) pipeline.Stage
pipeline.CreateConcurrentGroup(
// Sequential stage for the eggs flow
pipeline.CreateSequentialStage(
CreateBoilEggsStep(numberOfEggs, eggsChan),
CreateCutEggsStep(eggsChan),
createBoilEggsStep(numberOfEggs, eggsChan),
createCutEggsStep(eggsChan),
),
// Another sequential stage for the carrots (eggs and carrots will be concurrent though!)
pipeline.CreateSequentialStage(
CreateWashCarrotsStep(numberOfCarrots, carrotsChan),
CreateCutCarrotsStep(carrotsChan),
createWashCarrotsStep(numberOfCarrots, carrotsChan),
createCutCarrotsStep(carrotsChan),
),
),
// This is sequential. When carrots and eggs are done, this will run
pipeline.CreateSequentialStage(
CreateMakeSaladStep(eggsChan, carrotsChan, saladChan),
createMakeSaladStep(eggsChan, carrotsChan, saladChan),
),
),
// Another sequential stage for the meat (concurrently with salad)
Expand All @@ -51,27 +51,27 @@ func Graph(numberOfCarrots, numberOfEggs, meatSize, ovenSize int) pipeline.Stage
pipeline.CreateConcurrentGroup(
// Conditional stage, the meat might be too big
pipeline.CreateConditionalStage(
pipeline.CreateSimpleStatement("is_meat_too_big", CreateMeatTooBigStatement(meatSize, ovenSize)),
pipeline.CreateSimpleStatement("is_meat_too_big", createMeatTooBigStatement(meatSize, ovenSize)),
// True:
CreateCutMeatStep(meatSize, ovenSize, meatChan),
createCutMeatStep(meatSize, ovenSize, meatChan),
// False:
pipeline.CreateSimpleStep("leave_meat_as_it_is", func(ctx pipeline.Context) error {
meatChan <- meatSize // Simply pass the meat size
return nil
}),
),
pipeline.CreateSequentialStage(
CreateTurnOnOvenStep(),
createTurnOnOvenStep(),
),
),
pipeline.CreateSequentialStage(
CreatePutMeatInOvenStep(meatChan),
createPutMeatInOvenStep(meatChan),
),
),
),
// When everything is done. Serve
pipeline.CreateSequentialStage(
CreateServeStep(meatChan, saladChan),
createServeStep(meatChan, saladChan),
),
)

Expand All @@ -80,16 +80,17 @@ func Graph(numberOfCarrots, numberOfEggs, meatSize, ovenSize int) pipeline.Stage

// You could have your own executor using hystrix or whatever.
// Decorate it with tracers / circuit-breakers / loggers / new-relic / etc.
type SampleExecutor struct{}
type sampleExecutor struct{}

func (t *SampleExecutor) Run(cmd pipeline.Runnable, ctx pipeline.Context) error {
func (t *sampleExecutor) Run(cmd pipeline.Runnable, ctx pipeline.Context) error {
fmt.Printf("Running task %s\n", cmd.Name()) // Log when the task starts running
err := cmd.Run(ctx)
fmt.Printf("Finished task %s\n", cmd.Name()) // Log when the task ends running
return err
}

func Test_GraphRendering(t *testing.T) {
// RunGraphRendering represents the graph in UML Activity and renders it as an SVG file (template.svg)
func RunGraphRendering() {
if *render {
diagram := pipeline.CreateUMLActivityGraphDiagram()
renderer := pipeline.CreateUMLActivityRenderer(pipeline.UMLOptions{
Expand All @@ -101,10 +102,13 @@ func Test_GraphRendering(t *testing.T) {

err := renderer.Render(diagram, file)

assert.Nil(t, err)
if err != nil {
panic(err)
}
}
}

// RunPipeline runs the provided pipeline.
// Output: (one of many)
//
// Turning oven on
Expand All @@ -116,10 +120,10 @@ func Test_GraphRendering(t *testing.T) {
// Cutting 8 carrots into 40 pieces
// Making salad with 25 eggs and 40 carrots
// Serving 65 of salad and 500 of meat
func Test_Pipeline(t *testing.T) {
func RunPipeline() {
// Create a pipeline with our own traced executor (no circuit breakers, nothing). Also a graph, its stateless
// so it can be re-run as many times as we like
pipe := pipeline.CreatePipeline(&SampleExecutor{})
pipe := pipeline.CreatePipeline(&sampleExecutor{})
graph := Graph(8, 5, 600, 500)

// Initial context input data.
Expand All @@ -128,5 +132,13 @@ func Test_Pipeline(t *testing.T) {

// Run and assert.
err := pipe.Run(graph, ctx)
assert.Nil(t, err)

if err != nil {
panic(err)
}
}

func main() {
RunGraphRendering()
RunPipeline()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand Down Expand Up @@ -27,7 +27,7 @@ func (s *makeSaladStep) Run(ctx pipeline.Context) error {
return nil
}

func CreateMakeSaladStep(eggsChan chan int, carrotsChan chan int, saladChan chan int) pipeline.Step {
func createMakeSaladStep(eggsChan chan int, carrotsChan chan int, saladChan chan int) pipeline.Step {
return &makeSaladStep{
Eggs: eggsChan,
Carrots: carrotsChan,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -24,7 +24,7 @@ func (s *putMeatInTheOvenStep) Run(ctx pipeline.Context) error {
return nil
}

func CreatePutMeatInOvenStep(meatChan chan int) pipeline.Step {
func createPutMeatInOvenStep(meatChan chan int) pipeline.Step {
return &putMeatInTheOvenStep{
Meat: meatChan,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -24,7 +24,7 @@ func (s *serveStep) Run(ctx pipeline.Context) error {
return nil
}

func CreateServeStep(meatChan chan int, saladChan chan int) pipeline.Step {
func createServeStep(meatChan chan int, saladChan chan int) pipeline.Step {
return &serveStep{
Meat: meatChan,
Salad: saladChan,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -19,6 +19,6 @@ func (s *turnOnOvenStep) Run(ctx pipeline.Context) error {
return nil
}

func CreateTurnOnOvenStep() pipeline.Step {
func createTurnOnOvenStep() pipeline.Step {
return &turnOnOvenStep{}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -24,7 +24,7 @@ func (s *washCarrotsStep) Run(ctx pipeline.Context) error {
return nil
}

func CreateWashCarrotsStep(carrots int, carrotsChan chan int) pipeline.Step {
func createWashCarrotsStep(carrots int, carrotsChan chan int) pipeline.Step {
return &washCarrotsStep{
Carrots: carrots,
Stream: carrotsChan,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -14,12 +14,12 @@ func (s *boilEggsStep) Name() string {
}

func (s *boilEggsStep) Run(ctx pipeline.Context) error {
eggs, _ := ctx.GetInt(TagNumberOfEggs)
eggs, _ := ctx.GetInt(tagNumberOfEggs)
fmt.Printf("Boiling %d eggs\n", eggs)
time.Sleep(1 * time.Second) // Simulate time it takes to do this action
return nil
}

func CreateBoilEggsStep() pipeline.Step {
func createBoilEggsStep() pipeline.Step {
return &boilEggsStep{}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -14,15 +14,15 @@ func (s *cutCarrotsStep) Name() string {
}

func (s *cutCarrotsStep) Run(ctx pipeline.Context) error {
carrots, _ := ctx.GetInt(TagNumberOfCarrots)
carrots, _ := ctx.GetInt(tagNumberOfCarrots)
pieces := carrots * 5
fmt.Printf("Cutting %d carrots into %d pieces\n", carrots, pieces)
time.Sleep(1 * time.Second) // Simulate time it takes to do this action

ctx.Set(TagNumberOfCarrots, pieces)
ctx.Set(tagNumberOfCarrots, pieces)
return nil
}

func CreateCutCarrotsStep() pipeline.Step {
func createCutCarrotsStep() pipeline.Step {
return &cutCarrotsStep{}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cook_example_test
package main

import (
"fmt"
Expand All @@ -14,15 +14,15 @@ func (s *cutEggsStep) Name() string {
}

func (s *cutEggsStep) Run(ctx pipeline.Context) error {
eggs, _ := ctx.GetInt(TagNumberOfEggs)
eggs, _ := ctx.GetInt(tagNumberOfEggs)
pieces := eggs * 5
fmt.Printf("Cutting %d eggs into %d pieces\n", eggs, pieces)
time.Sleep(1 * time.Second)

ctx.Set(TagNumberOfEggs, pieces)
ctx.Set(tagNumberOfEggs, pieces)
return nil
}

func CreateCutEggsStep() pipeline.Step {
func createCutEggsStep() pipeline.Step {
return &cutEggsStep{}
}
Loading

0 comments on commit 06e6bf5

Please sign in to comment.