Skip to content

Commit

Permalink
workflow: Add more internal testing (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewwormald authored Aug 16, 2024
1 parent 89afb49 commit 2278c47
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ sonar.projectName = workflow
sonar.links.scm = https://github.com/luno/workflow

sonar.sources = .
sonar.exclusions=**/*_test.go, _examples/**/*, adapters/reflexstreamer/**/*, adapters/kafkastreamer/**/*, adapters/sqlstore/**/*, adapters/sqltimeout/**/*
sonar.exclusions=**/*_test.go, **/*pb.go, _examples/**/*, adapters/reflexstreamer/**/*, adapters/kafkastreamer/**/*, adapters/sqlstore/**/*, adapters/sqltimeout/**/*
sonar.go.coverage.reportPaths = coverage.out
sonar.go.tests.reportPaths = sonar-report.json
sonar.tests = .
sonar.test.inclusions = **/*_test.go
sonar.test.exclusions=_examples/**/*, adapters/reflexstreamer/**/*, adapters/kafkastreamer/**/*, adapters/sqlstore/**/*, adapters/sqltimeout/**/*
sonar.test.exclusions=_examples/**/*, **/*pb.go, adapters/reflexstreamer/**/*, adapters/kafkastreamer/**/*, adapters/sqlstore/**/*, adapters/sqltimeout/**/*
2 changes: 1 addition & 1 deletion timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type TimeoutRecord struct {
CreatedAt time.Time
}

// pollTimeouts attempts to find the very next
// pollTimeouts attempts to find the very next expired timeout and execute it
func pollTimeouts[Type any, Status StatusType](
ctx context.Context,
w *Workflow[Type, Status],
Expand Down
6 changes: 5 additions & 1 deletion trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
)

func (w *Workflow[Type, Status]) Trigger(ctx context.Context, foreignID string, startingStatus Status, opts ...TriggerOption[Type, Status]) (runID string, err error) {
return trigger(ctx, w, w.recordStore.Latest, foreignID, startingStatus, opts...)
}

func trigger[Type any, Status StatusType](ctx context.Context, w *Workflow[Type, Status], lookup latestLookup, foreignID string, startingStatus Status, opts ...TriggerOption[Type, Status]) (runID string, err error) {
if !w.calledRun {
return "", errors.Wrap(ErrWorkflowNotRunning, "ensure Run() is called before attempting to trigger the workflow")
}
Expand All @@ -33,7 +37,7 @@ func (w *Workflow[Type, Status]) Trigger(ctx context.Context, foreignID string,
return "", err
}

lastRecord, err := w.recordStore.Latest(ctx, w.Name, foreignID)
lastRecord, err := lookup(ctx, w.Name, foreignID)
if errors.Is(err, ErrRecordNotFound) {
lastRecord = &Record{}
} else if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions trigger_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package workflow

import (
"context"
"testing"

"github.com/luno/jettison/jtest"
)

func Test_trigger(t *testing.T) {
b := NewBuilder[string, testStatus]("trigger test")
b.AddStep(statusStart, func(ctx context.Context, r *Run[string, testStatus]) (testStatus, error) {
return statusMiddle, nil
}, statusMiddle)
w := b.Build(nil, nil, nil, WithDebugMode())

t.Run("Expected ErrWorkflowNotRunning when Trigger called before Run()", func(t *testing.T) {
ctx := context.Background()
_, err := trigger(ctx, w, nil, "1", statusStart)
jtest.Require(t, ErrWorkflowNotRunning, err)
})

t.Run("Expects ErrStatusProvidedNotConfigured when starting status is not configured", func(t *testing.T) {
ctx := context.Background()
w.calledRun = true

_, err := trigger(ctx, w, nil, "1", statusEnd)
jtest.Require(t, ErrStatusProvidedNotConfigured, err)
})

t.Run("Expects ErrWorkflowInProgress if a workflow run is already in progress", func(t *testing.T) {
ctx := context.Background()
w.calledRun = true

_, err := trigger(ctx, w, func(ctx context.Context, workflowName, foreignID string) (*Record, error) {
return &Record{
ID: 1,
WorkflowName: "trigger test",
ForeignID: "1",
RunState: RunStateRunning,
Status: int(statusMiddle),
}, nil
}, "1", statusStart)
jtest.Require(t, ErrWorkflowInProgress, err)
})
}

0 comments on commit 2278c47

Please sign in to comment.