Skip to content

Commit

Permalink
workflow: Use smaller logger dependency with override support (#25)
Browse files Browse the repository at this point in the history
* workflow: Use smaller logger dependency with override support

* clean up

* reduce code duplication

* exclude jlog adapter module

* remove errmeta package

* clean up
  • Loading branch information
andrewwormald authored Sep 19, 2024
1 parent 834c034 commit 6bd9675
Show file tree
Hide file tree
Showing 66 changed files with 1,233 additions and 735 deletions.
4 changes: 2 additions & 2 deletions _examples/callback/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"testing"

"github.com/luno/jettison/jtest"
"github.com/luno/workflow"
"github.com/luno/workflow/adapters/memrecordstore"
"github.com/luno/workflow/adapters/memrolescheduler"
"github.com/luno/workflow/adapters/memstreamer"
"github.com/stretchr/testify/require"

"github.com/luno/workflow/_examples/callback"
)
Expand All @@ -26,7 +26,7 @@ func TestCallbackWorkflow(t *testing.T) {

foreignID := "andrew"
runID, err := wf.Trigger(ctx, foreignID, callback.StatusStarted)
jtest.RequireNil(t, err)
require.Nil(t, err)

workflow.TriggerCallbackOn(t, wf, foreignID, runID, callback.StatusStarted, callback.EmailConfirmationResponse{
Confirmed: true,
Expand Down
4 changes: 2 additions & 2 deletions _examples/gettingstarted/gettingstarted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"testing"

"github.com/luno/jettison/jtest"
"github.com/luno/workflow"
"github.com/luno/workflow/adapters/memrecordstore"
"github.com/luno/workflow/adapters/memrolescheduler"
"github.com/luno/workflow/adapters/memstreamer"
"github.com/luno/workflow/adapters/memtimeoutstore"
"github.com/stretchr/testify/require"

"github.com/luno/workflow/_examples/gettingstarted"
)
Expand All @@ -28,7 +28,7 @@ func TestWorkflow(t *testing.T) {

foreignID := "82347982374982374"
_, err := wf.Trigger(ctx, foreignID, gettingstarted.StatusStarted)
jtest.RequireNil(t, err)
require.Nil(t, err)

workflow.Require(t, wf, foreignID, gettingstarted.StatusReadTheDocs, gettingstarted.GettingStarted{
ReadTheDocs: "✅",
Expand Down
10 changes: 5 additions & 5 deletions _examples/schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package schedule_test

import (
"context"
"errors"
"testing"
"time"

"github.com/luno/jettison/jtest"
"github.com/luno/workflow"
"github.com/luno/workflow/adapters/memrecordstore"
"github.com/luno/workflow/adapters/memrolescheduler"
Expand Down Expand Up @@ -37,23 +37,23 @@ func TestExampleWorkflow(t *testing.T) {

go func() {
err := wf.Schedule(foreignID, schedule.StatusStarted, "@hourly")
jtest.RequireNil(t, err)
require.Nil(t, err)
}()

// Give time for go routine to spin up
time.Sleep(200 * time.Millisecond)

_, err := recordStore.Latest(ctx, wf.Name, foreignID)
// Expect there to be no entries yet
jtest.Require(t, workflow.ErrRecordNotFound, err)
require.True(t, errors.Is(err, workflow.ErrRecordNotFound))

clock.Step(time.Hour)

// Allow scheduling to take place
time.Sleep(200 * time.Millisecond)

firstScheduled, err := recordStore.Latest(ctx, wf.Name, foreignID)
jtest.RequireNil(t, err)
require.Nil(t, err)

require.Equal(t, "schedule trigger example", firstScheduled.WorkflowName)
require.Equal(t, "hourly-run", firstScheduled.ForeignID)
Expand All @@ -64,7 +64,7 @@ func TestExampleWorkflow(t *testing.T) {
time.Sleep(200 * time.Millisecond)

secondScheduled, err := recordStore.Latest(ctx, wf.Name, foreignID)
jtest.RequireNil(t, err)
require.Nil(t, err)

require.Equal(t, "schedule trigger example", secondScheduled.WorkflowName)
require.Equal(t, "hourly-run", secondScheduled.ForeignID)
Expand Down
4 changes: 2 additions & 2 deletions _examples/timeout/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"testing"
"time"

"github.com/luno/jettison/jtest"
"github.com/luno/workflow"
"github.com/luno/workflow/adapters/memrecordstore"
"github.com/luno/workflow/adapters/memrolescheduler"
"github.com/luno/workflow/adapters/memstreamer"
"github.com/luno/workflow/adapters/memtimeoutstore"
"github.com/stretchr/testify/require"
clocktesting "k8s.io/utils/clock/testing"

"github.com/luno/workflow/_examples/timeout"
Expand All @@ -33,7 +33,7 @@ func TestTimeoutWorkflow(t *testing.T) {

foreignID := "andrew"
runID, err := wf.Trigger(ctx, foreignID, timeout.StatusStarted)
jtest.RequireNil(t, err)
require.Nil(t, err)

workflow.AwaitTimeoutInsert(t, wf, foreignID, runID, timeout.StatusStarted)

Expand Down
5 changes: 2 additions & 3 deletions adapters/adaptertest/eventstreaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/google/uuid"
"github.com/luno/jettison/jtest"
"github.com/stretchr/testify/require"
clock_testing "k8s.io/utils/clock/testing"

Expand Down Expand Up @@ -91,14 +90,14 @@ func RunEventStreamerTest(t *testing.T, constructor workflow.EventStreamer) {
CountryCode: "GB",
}
runId, err := wf.Trigger(ctx, foreignID, SyncStatusStarted, workflow.WithInitialValue[User, SyncStatus](&u))
jtest.RequireNil(t, err)
require.Nil(t, err)

workflow.AwaitTimeoutInsert(t, wf, foreignID, runId, SyncStatusEmailSet)

clock.Step(time.Hour)

record, err := wf.Await(ctx, foreignID, runId, SyncStatusCompleted)
jtest.RequireNil(t, err)
require.Nil(t, err)

require.Equal(t, "[email protected]", record.Object.Email)
require.Equal(t, SyncStatusCompleted.String(), record.Status.String())
Expand Down
71 changes: 35 additions & 36 deletions adapters/adaptertest/recordstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/google/uuid"
"github.com/luno/jettison/jtest"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"

Expand Down Expand Up @@ -39,20 +38,20 @@ func testLatest(t *testing.T, factory func() workflow.RecordStore) {
maker := func(recordID int64) (workflow.OutboxEventData, error) { return workflow.OutboxEventData{}, nil }

err := store.Store(ctx, expected, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)

latest, err := store.Latest(ctx, expected.WorkflowName, expected.ForeignID)
jtest.RequireNil(t, err)
require.Nil(t, err)

recordIsEqual(t, *expected, *latest)

expected.Status = int(statusEnd)
expected.RunState = workflow.RunStateCompleted
err = store.Store(ctx, expected, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)

latest, err = store.Latest(ctx, expected.WorkflowName, expected.ForeignID)
jtest.RequireNil(t, err)
require.Nil(t, err)
recordIsEqual(t, *expected, *latest)
})
}
Expand All @@ -65,10 +64,10 @@ func testLookup(t *testing.T, factory func() workflow.RecordStore) {
maker := func(recordID int64) (workflow.OutboxEventData, error) { return workflow.OutboxEventData{}, nil }

err := store.Store(ctx, expected, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)

latest, err := store.Lookup(ctx, 1)
jtest.RequireNil(t, err)
require.Nil(t, err)

recordIsEqual(t, *expected, *latest)
})
Expand All @@ -82,27 +81,27 @@ func testStore(t *testing.T, factory func() workflow.RecordStore) {
maker := func(recordID int64) (workflow.OutboxEventData, error) { return workflow.OutboxEventData{}, nil }

err := store.Store(ctx, expected, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)

latest, err := store.Lookup(ctx, 1)
jtest.RequireNil(t, err)
require.Nil(t, err)

latest.Status = int(statusMiddle)
expected.Status = int(statusMiddle)

err = store.Store(ctx, latest, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)

recordIsEqual(t, *expected, *latest)

latest, err = store.Lookup(ctx, 1)
jtest.RequireNil(t, err)
require.Nil(t, err)

latest.Status = int(statusEnd)
expected.Status = int(statusEnd)

err = store.Store(ctx, latest, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)

recordIsEqual(t, *expected, *latest)
})
Expand All @@ -121,10 +120,10 @@ func testListOutboxEvents(t *testing.T, factory func() workflow.RecordStore) {
}

err := store.Store(ctx, expected, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)

ls, err := store.ListOutboxEvents(ctx, expected.WorkflowName, 1000)
jtest.RequireNil(t, err)
require.Nil(t, err)

require.Equal(t, 1, len(ls))

Expand All @@ -133,7 +132,7 @@ func testListOutboxEvents(t *testing.T, factory func() workflow.RecordStore) {

var r outboxpb.OutboxRecord
err = proto.Unmarshal(ls[0].Data, &r)
jtest.RequireNil(t, err)
require.Nil(t, err)

require.Equal(t, int32(statusStarted), r.Type)
require.Equal(t, "my_workflow-1", r.Headers[string(workflow.HeaderTopic)])
Expand All @@ -155,21 +154,21 @@ func testDeleteOutboxEvent(t *testing.T, factory func() workflow.RecordStore) {
}

err := store.Store(ctx, expected, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)

latest, err := store.Lookup(ctx, 1)
jtest.RequireNil(t, err)
require.Nil(t, err)

latest.Status = int(statusMiddle)

ls, err := store.ListOutboxEvents(ctx, expected.WorkflowName, 1000)
jtest.RequireNil(t, err)
require.Nil(t, err)

err = store.DeleteOutboxEvent(ctx, ls[0].ID)
jtest.RequireNil(t, err)
require.Nil(t, err)

ls, err = store.ListOutboxEvents(ctx, expected.WorkflowName, 1000)
jtest.RequireNil(t, err)
require.Nil(t, err)

require.Equal(t, 0, len(ls))
})
Expand All @@ -186,35 +185,35 @@ func testList(t *testing.T, factory func() workflow.RecordStore) {
seedCount := 1000
for i := 0; i < seedCount; i++ {
err := store.Store(ctx, dummyWireRecord(t), maker)
jtest.RequireNil(t, err)
require.Nil(t, err)
}

ls, err := store.List(ctx, workflowName, 0, 53, workflow.OrderTypeAscending)
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, 53, len(ls))

ls2, err := store.List(ctx, workflowName, 53, 100, workflow.OrderTypeAscending)
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, 100, len(ls2))

// Make sure the last of the first page is not the same as the first of the next page
require.NotEqual(t, ls[52].ID, ls2[0])

ls3, err := store.List(ctx, workflowName, 153, seedCount-153, workflow.OrderTypeAscending)
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, seedCount-153, len(ls3))

// Make sure the last of the first page is not the same as the first of the next page
require.NotEqual(t, ls3[152].ID, ls3[0])

// Make sure that if 950 is the offset and we only have 1000 then only 1 item would be returned
lastPageAsc, err := store.List(ctx, workflowName, 950, 1000, workflow.OrderTypeAscending)
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, 50, len(lastPageAsc))
require.Equal(t, int64(1000), lastPageAsc[len(lastPageAsc)-1].ID)

lastPageDesc, err := store.List(ctx, workflowName, 950, 1000, workflow.OrderTypeDescending)
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, 50, len(lastPageDesc))
require.Equal(t, int64(1000), lastPageDesc[0].ID)
})
Expand All @@ -229,20 +228,20 @@ func testList(t *testing.T, factory func() workflow.RecordStore) {
wr.ForeignID = foreignID

err := store.Store(ctx, wr, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)
}
}

ls, err := store.List(ctx, workflowName, 0, 100, workflow.OrderTypeAscending, workflow.FilterByForeignID(foreignIDs[0]))
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, 20, len(ls))

ls2, err := store.List(ctx, workflowName, 0, 100, workflow.OrderTypeAscending, workflow.FilterByForeignID(foreignIDs[1]))
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, 20, len(ls2))

ls3, err := store.List(ctx, workflowName, 0, 100, workflow.OrderTypeAscending, workflow.FilterByForeignID("random"))
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, 0, len(ls3))
})

Expand All @@ -263,13 +262,13 @@ func testList(t *testing.T, factory func() workflow.RecordStore) {
wr.RunState = runState

err := store.Store(ctx, wr, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)
}
}

for runState, count := range config {
ls, err := store.List(ctx, workflowName, 0, 100, workflow.OrderTypeAscending, workflow.FilterByRunState(runState))
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, count, len(ls), fmt.Sprintf("Expected to have %v entries of %v", count, runState.String()))

for _, l := range ls {
Expand All @@ -292,13 +291,13 @@ func testList(t *testing.T, factory func() workflow.RecordStore) {
newRecord.Status = int(status)

err := store.Store(ctx, newRecord, maker)
jtest.RequireNil(t, err)
require.Nil(t, err)
}
}

for status, count := range config {
ls, err := store.List(ctx, workflowName, 0, 100, workflow.OrderTypeAscending, workflow.FilterByStatus(int64(status)))
jtest.RequireNil(t, err)
require.Nil(t, err)
require.Equal(t, count, len(ls))

for _, l := range ls {
Expand All @@ -316,15 +315,15 @@ func dummyWireRecordWithID(t *testing.T, id int64) *workflow.Record {
workflowName := "my_workflow"
foreignID := "Andrew Wormald"
runID, err := uuid.NewUUID()
jtest.RequireNil(t, err)
require.Nil(t, err)

type example struct {
name string
}

e := example{name: foreignID}
b, err := json.Marshal(e)
jtest.RequireNil(t, err)
require.Nil(t, err)

createdAt := time.Now()

Expand Down
Loading

0 comments on commit 6bd9675

Please sign in to comment.