github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/pipeline/actor_factory_test.go (about)

     1  package pipeline
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/awesome-flow/flow/pkg/cfg"
     8  	core "github.com/awesome-flow/flow/pkg/corev1alpha1"
     9  	"github.com/awesome-flow/flow/pkg/types"
    10  	flowplugin "github.com/awesome-flow/flow/pkg/util/plugin"
    11  	flowtest "github.com/awesome-flow/flow/pkg/util/test/corev1alpha1"
    12  )
    13  
    14  func TestCoreActorFctoryBuild(t *testing.T) {
    15  	module := "test.builder"
    16  	builders := map[string]core.Builder{
    17  		module: func(name string, ctx *core.Context, params core.Params) (core.Actor, error) {
    18  			return flowtest.NewTestActor(name, ctx, params)
    19  		},
    20  	}
    21  	factory := NewCoreActorFactoryWithBuilders(builders)
    22  	name := "test-actor-1"
    23  	repo := cfg.NewRepository()
    24  	ctx, err := core.NewContext(core.NewConfig(repo))
    25  	if err != nil {
    26  		t.Fatalf("failed to create a context: %s", err)
    27  	}
    28  	if err := ctx.Start(); err != nil {
    29  		t.Fatalf("failed to start context: %s", err)
    30  	}
    31  	defer ctx.Stop()
    32  	actcfg := &types.CfgBlockActor{
    33  		Module: module,
    34  	}
    35  	actor, err := factory.Build(name, ctx, actcfg)
    36  	if err != nil {
    37  		t.Fatalf("failed to build actor: %s", err)
    38  	}
    39  	if _, ok := actor.(*flowtest.TestActor); !ok {
    40  		t.Fatalf("unexpected type of actor: got: %s, want: %s", reflect.TypeOf(actor).String(), "TestActor")
    41  	}
    42  	if actor.Name() != name {
    43  		t.Fatalf("unexpected actor name: got: %s, want: %s", actor.Name(), name)
    44  	}
    45  }
    46  
    47  func TestPluginActorFactoryBuild(t *testing.T) {
    48  	name := "test-plugin-actor-1"
    49  	loader := func(path, name string) (flowplugin.Plugin, error) {
    50  		return &flowtest.TestPlugin{
    51  			Path: path,
    52  			Name: name,
    53  		}, nil
    54  	}
    55  	repo := cfg.NewRepository()
    56  	if _, err := cfg.NewScalarConfigProvider(
    57  		&types.KeyValue{
    58  			Key:   types.NewKey("plugin.path"),
    59  			Value: "/never/where",
    60  		},
    61  		repo,
    62  		42, // Doesn't matter: it's the only provider
    63  	); err != nil {
    64  		t.Fatalf("failed to create scalar config: %s", err)
    65  	}
    66  	factory := NewPluginActorFactoryWithLoader(loader)
    67  	ctx, err := core.NewContext(core.NewConfig(repo))
    68  	if err != nil {
    69  		t.Fatalf("failed to create a context: %s", err)
    70  	}
    71  	if err := ctx.Start(); err != nil {
    72  		t.Fatalf("failed to start context: %s", err)
    73  	}
    74  	defer ctx.Stop()
    75  	actcfg := &types.CfgBlockActor{
    76  		Module: "plugin.test-actor",
    77  	}
    78  	actor, err := factory.Build(name, ctx, actcfg)
    79  	if err != nil {
    80  		t.Fatalf("failed to build actor: %s", err)
    81  	}
    82  	if _, ok := actor.(*flowtest.TestActor); !ok {
    83  		t.Fatalf("unexpected type of actor: got: %s, want: %s", reflect.TypeOf(actor).String(), "*pipeline.TestActor")
    84  	}
    85  	if actor.Name() != name {
    86  		t.Fatalf("unexpected actor name: got: %s, want: %s", actor.Name(), name)
    87  	}
    88  }