Skip to content

Commit

Permalink
fix: return an error when no matching flow is found when running regi…
Browse files Browse the repository at this point in the history
…stry
  • Loading branch information
hiroara committed Aug 9, 2023
1 parent 8cdf54b commit 37a8f4f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
11 changes: 10 additions & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package registry

import (
"context"
"errors"
"fmt"

"github.com/hiroara/carbo/flow"
)
Expand All @@ -20,9 +22,16 @@ func New() *Registry {
return &Registry{factories: make(map[string]flow.Factory)}
}

var ErrNoMatchingFlow = errors.New("no matching flow is found")

// Build a registered Flow selected with the passed name, and run it.
func (r *Registry) Run(ctx context.Context, name string) error {
f, err := r.factories[name].Build()
fac, ok := r.factories[name]
if !ok {
return fmt.Errorf("%w with name \"%s\"", ErrNoMatchingFlow, name)
}

f, err := fac.Build()
if err != nil {
return err
}
Expand Down
40 changes: 25 additions & 15 deletions registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,34 @@ import (
func TestRegistryRun(t *testing.T) {
t.Parallel()

r := registry.New()
t.Run("NormalCase", func(t *testing.T) {
t.Parallel()

src := source.FromSlice([]string{"item1", "item2"})
r := registry.New()

out := make([]string, 0)
sink := sink.ToSlice(&out)
conn := task.Connect(src.AsTask(), sink.AsTask(), 2)
called := false
src := source.FromSlice([]string{"item1", "item2"})
out := make([]string, 0)
sink := sink.ToSlice(&out)
conn := task.Connect(src.AsTask(), sink.AsTask(), 2)
called := false

r.Register("flow1", flow.NewFactory(func() (*flow.Flow, error) {
called = true
return flow.FromTask(conn), nil
}))
r.Register("flow1", flow.NewFactory(func() (*flow.Flow, error) {
called = true
return flow.FromTask(conn), nil
}))

err := r.Run(context.Background(), "flow1")
require.NoError(t, err)
err := r.Run(context.Background(), "flow1")
require.NoError(t, err)

if assert.True(t, called) {
assert.Equal(t, []string{"item1", "item2"}, out)
}
if assert.True(t, called) {
assert.Equal(t, []string{"item1", "item2"}, out)
}
})

t.Run("NoMatchingFlowCase", func(t *testing.T) {
t.Parallel()

r := registry.New()
assert.ErrorIs(t, r.Run(context.Background(), "unknown"), registry.ErrNoMatchingFlow)
})
}

0 comments on commit 37a8f4f

Please sign in to comment.