github.com/whiteboxio/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/pipeline/pipeline_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  	"github.com/awesome-flow/flow/pkg/util/data"
    11  	flowplugin "github.com/awesome-flow/flow/pkg/util/plugin"
    12  	flowtest "github.com/awesome-flow/flow/pkg/util/test/corev1alpha1"
    13  )
    14  
    15  func TestStartPropogatesToActors(t *testing.T) {
    16  	repo := cfg.NewRepository()
    17  	cfg := core.NewConfig(repo)
    18  	ctx, _ := core.NewContext(cfg)
    19  	if err := ctx.Start(); err != nil {
    20  		t.Fatalf("failed to start context: %s", err)
    21  	}
    22  	defer ctx.Stop()
    23  
    24  	act1, err := flowtest.NewTestActor("test-actor-1", ctx, nil)
    25  	if err != nil {
    26  		t.Fatalf("failed to create a test actor: %s", err)
    27  	}
    28  	act2, err := flowtest.NewTestActor("test-actor-2", ctx, nil)
    29  	if err != nil {
    30  		t.Fatalf("failed to create a test actor: %s", err)
    31  	}
    32  
    33  	act1.Connect(1, act2)
    34  
    35  	top := data.NewTopology()
    36  	top.AddNode(act1)
    37  	top.AddNode(act2)
    38  	top.Connect(act1, act2)
    39  
    40  	events := make([]string, 0, 2)
    41  
    42  	act1.(*flowtest.TestActor).OnStart(func() {
    43  		events = append(events, act1.Name())
    44  	})
    45  
    46  	act2.(*flowtest.TestActor).OnStart(func() {
    47  		events = append(events, act2.Name())
    48  	})
    49  
    50  	p := &Pipeline{
    51  		ctx: ctx,
    52  		actors: map[string]core.Actor{
    53  			"test-actor-1": act1,
    54  			"test-actor-2": act1,
    55  		},
    56  		topology: top,
    57  	}
    58  
    59  	if err := p.Start(); err != nil {
    60  		t.Fatalf("failed to start the pipeline: %s", err)
    61  	}
    62  
    63  	wantevents := []string{"test-actor-2", "test-actor-1"}
    64  
    65  	if !reflect.DeepEqual(events, wantevents) {
    66  		t.Fatalf("unexpected events: got: %v, want: %v", events, wantevents)
    67  	}
    68  }
    69  
    70  func TestStopPropogatesToActors(t *testing.T) {
    71  	repo := cfg.NewRepository()
    72  	cfg := core.NewConfig(repo)
    73  	ctx, _ := core.NewContext(cfg)
    74  	if err := ctx.Start(); err != nil {
    75  		t.Fatalf("failed to start context: %s", err)
    76  	}
    77  	defer ctx.Stop()
    78  
    79  	act1, err := flowtest.NewTestActor("test-actor-1", ctx, nil)
    80  	if err != nil {
    81  		t.Fatalf("failed to create a test actor: %s", err)
    82  	}
    83  	act2, err := flowtest.NewTestActor("test-actor-2", ctx, nil)
    84  	if err != nil {
    85  		t.Fatalf("failed to create a test actor: %s", err)
    86  	}
    87  
    88  	act1.Connect(1, act2)
    89  
    90  	top := data.NewTopology()
    91  	top.AddNode(act1)
    92  	top.AddNode(act2)
    93  	top.Connect(act1, act2)
    94  
    95  	events := make([]string, 0, 2)
    96  
    97  	act1.(*flowtest.TestActor).OnStop(func() {
    98  		events = append(events, act1.Name())
    99  	})
   100  
   101  	act2.(*flowtest.TestActor).OnStop(func() {
   102  		events = append(events, act2.Name())
   103  	})
   104  
   105  	p := &Pipeline{
   106  		ctx: ctx,
   107  		actors: map[string]core.Actor{
   108  			"test-actor-1": act1,
   109  			"test-actor-2": act1,
   110  		},
   111  		topology: top,
   112  	}
   113  
   114  	if err := p.Start(); err != nil {
   115  		t.Fatalf("failed to start the pipeline: %s", err)
   116  	}
   117  
   118  	if err := p.Stop(); err != nil {
   119  		t.Fatalf("failed to stop the pipeline: %s", err)
   120  	}
   121  
   122  	wantevents := []string{"test-actor-1", "test-actor-2"}
   123  
   124  	if !reflect.DeepEqual(events, wantevents) {
   125  		t.Fatalf("unexpected events: got: %v, want: %v", events, wantevents)
   126  	}
   127  }
   128  
   129  func TestBuildActors(t *testing.T) {
   130  	coreActorName := "test-core-actor-1"
   131  	pluginActorName := "test-plugin-actor-1"
   132  
   133  	factories := map[string]ActorFactory{
   134  		"core": NewCoreActorFactoryWithBuilders(
   135  			map[string]core.Builder{
   136  				"core.test-actor": flowtest.NewTestActor,
   137  			},
   138  		),
   139  		"plugin": NewPluginActorFactoryWithLoader(
   140  			func(path, name string) (flowplugin.Plugin, error) {
   141  				return &flowtest.TestPlugin{
   142  					Path: path,
   143  					Name: name,
   144  				}, nil
   145  			},
   146  		),
   147  	}
   148  	repo := cfg.NewRepository()
   149  	ctx, err := core.NewContext(core.NewConfig(repo))
   150  	if err != nil {
   151  		t.Fatalf("failed to create a context: %s", err)
   152  	}
   153  	actorscfg := map[string]types.CfgBlockActor{
   154  		coreActorName: types.CfgBlockActor{
   155  			Module: "core.test-actor",
   156  		},
   157  		pluginActorName: types.CfgBlockActor{
   158  			Module: "plugin.test-plugin",
   159  		},
   160  	}
   161  
   162  	if _, err := cfg.NewScalarConfigProvider(
   163  		&types.KeyValue{
   164  			Key:   types.NewKey("actors"),
   165  			Value: actorscfg,
   166  		},
   167  		repo,
   168  		42, // doesn't matter
   169  	); err != nil {
   170  		t.Fatalf("failed to create scalar provider: %s", err)
   171  	}
   172  	if _, err := cfg.NewScalarConfigProvider(
   173  		&types.KeyValue{
   174  			Key:   types.NewKey("plugin.path"),
   175  			Value: "/never/where",
   176  		},
   177  		repo,
   178  		42, // doesn't matter
   179  	); err != nil {
   180  		t.Fatalf("failed to create scalar provider: %s", err)
   181  	}
   182  
   183  	if err := ctx.Start(); err != nil {
   184  		t.Fatalf("failed to start context: %s", err)
   185  	}
   186  	defer ctx.Stop()
   187  
   188  	actors, err := buildActors(ctx, factories)
   189  	if err != nil {
   190  		t.Fatalf("failed to build actors: %s", err)
   191  	}
   192  
   193  	if len(actors) > 2 {
   194  		t.Fatalf("Unexpected contents of actors map: %+v", actors)
   195  	}
   196  
   197  	for _, name := range []string{coreActorName, pluginActorName} {
   198  		if _, ok := actors[name]; !ok {
   199  			t.Fatalf("actor %s is missing from actors map", name)
   200  		}
   201  		if _, ok := actors[name].(*flowtest.TestActor); !ok {
   202  			t.Fatalf("unexpected actor type: got: %s, want: %s", reflect.TypeOf(actors[name]), "*pipeline.TestActor")
   203  		}
   204  		if actorname := actors[name].Name(); actorname != name {
   205  			t.Fatalf("unexpected actor name: got: %s, want: %s", actors[name].Name(), name)
   206  		}
   207  	}
   208  }