github.com/TIBCOSoftware/flogo-lib@v0.5.9/engine/runner/pooled_test.go (about)

     1  package runner
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/TIBCOSoftware/flogo-lib/core/action"
     9  	"github.com/TIBCOSoftware/flogo-lib/core/data"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/mock"
    12  )
    13  
    14  type MockFullAction struct {
    15  	mock.Mock
    16  }
    17  
    18  func (m *MockFullAction) Config() *action.Config {
    19  	return nil
    20  }
    21  
    22  func (m *MockFullAction) Metadata() *action.Metadata {
    23  	return nil
    24  }
    25  
    26  func (m *MockFullAction) IOMetadata() *data.IOMetadata {
    27  	return nil
    28  }
    29  
    30  func (m *MockFullAction) Run(context context.Context, inputs map[string]*data.Attribute, handler action.ResultHandler) error {
    31  	args := m.Called(context, inputs, handler)
    32  	return args.Error(0)
    33  }
    34  
    35  // This mock action will handle the result and mark it done
    36  type MockResultAction struct {
    37  	mock.Mock
    38  }
    39  
    40  func (m *MockResultAction) Config() *action.Config {
    41  	return nil
    42  }
    43  
    44  func (m *MockResultAction) Metadata() *action.Metadata {
    45  	return nil
    46  }
    47  
    48  func (m *MockResultAction) IOMetadata() *data.IOMetadata {
    49  	return nil
    50  }
    51  
    52  func (m *MockResultAction) Run(context context.Context, inputs map[string]*data.Attribute, handler action.ResultHandler) error {
    53  	args := m.Called(context, inputs, handler)
    54  	go func() {
    55  		dataAttr, _ := data.NewAttribute("data", data.TypeString, "mock")
    56  		codeAttr, _ := data.NewAttribute("code", data.TypeInteger, 200)
    57  		resultData := map[string]*data.Attribute{
    58  			"data": dataAttr,
    59  			"code": codeAttr,
    60  		}
    61  		handler.HandleResult(resultData, nil)
    62  		handler.Done()
    63  	}()
    64  	return args.Error(0)
    65  }
    66  
    67  // TestNewPooledOk test creation of new Pooled runner
    68  func TestNewPooledOk(t *testing.T) {
    69  	config := &PooledConfig{NumWorkers: 1, WorkQueueSize: 1}
    70  	runner := NewPooled(config)
    71  	assert.NotNil(t, runner)
    72  }
    73  
    74  // TestStartOk test that Start method is fine
    75  func TestStartOk(t *testing.T) {
    76  	config := &PooledConfig{NumWorkers: 3, WorkQueueSize: 3}
    77  	runner := NewPooled(config)
    78  	assert.NotNil(t, runner)
    79  	err := runner.Start()
    80  	assert.Nil(t, err)
    81  	// It should have a worker queue of the size expected
    82  	assert.Equal(t, 3, cap(runner.workerQueue))
    83  	// It should have a workers of the expected size
    84  	assert.Equal(t, 3, len(runner.workers))
    85  	// Runner should be active
    86  	assert.True(t, runner.active)
    87  }
    88  
    89  // TestRunNilError test that running a nil action trows and error
    90  func TestRunNilError(t *testing.T) {
    91  	config := &PooledConfig{NumWorkers: 5, WorkQueueSize: 5}
    92  	runner := NewPooled(config)
    93  	assert.NotNil(t, runner)
    94  	err := runner.Start()
    95  	assert.Nil(t, err)
    96  	_, err = runner.Execute(nil, nil, nil)
    97  	assert.NotNil(t, err)
    98  }
    99  
   100  // TestRunInnactiveError test that running an innactive runner trows and error
   101  func TestRunInnactiveError(t *testing.T) {
   102  	config := &PooledConfig{NumWorkers: 5, WorkQueueSize: 5}
   103  	runner := NewPooled(config)
   104  	assert.NotNil(t, runner)
   105  	a := new(MockFullAction)
   106  	_, err := runner.Execute(nil, a, nil)
   107  	assert.NotNil(t, err)
   108  }
   109  
   110  // TestRunErrorInAction test that running an action returns an error
   111  func TestRunErrorInAction(t *testing.T) {
   112  	config := &PooledConfig{NumWorkers: 5, WorkQueueSize: 5}
   113  	runner := NewPooled(config)
   114  	assert.NotNil(t, runner)
   115  	err := runner.Start()
   116  	assert.Nil(t, err)
   117  	a := new(MockFullAction)
   118  	a.On("Run", nil, mock.AnythingOfType("map[string]*data.Attribute"), mock.AnythingOfType("*runner.AsyncResultHandler")).Return(errors.New("Error in action"))
   119  	_, err = runner.Execute(nil, a, nil)
   120  	assert.NotNil(t, err)
   121  	assert.Equal(t, "Error in action", err.Error())
   122  }
   123  
   124  // TestRunOk test that running an action is ok
   125  func TestRunOk(t *testing.T) {
   126  	config := &PooledConfig{NumWorkers: 5, WorkQueueSize: 5}
   127  	runner := NewPooled(config)
   128  	assert.NotNil(t, runner)
   129  	err := runner.Start()
   130  	assert.Nil(t, err)
   131  	a := new(MockResultAction)
   132  	a.On("Run", nil, mock.AnythingOfType("map[string]*data.Attribute"), mock.AnythingOfType("*runner.AsyncResultHandler")).Return(nil)
   133  	results, err := runner.Execute(nil, a, nil)
   134  	assert.Nil(t, err)
   135  	codeAttr := results["code"]
   136  	assert.NotNil(t, codeAttr)
   137  	assert.Equal(t, 200, codeAttr.Value())
   138  	dataAttr := results["data"]
   139  	assert.NotNil(t, dataAttr)
   140  	assert.Equal(t, "mock", dataAttr.Value())
   141  }
   142  
   143  // TestStopOk test that Stop method is fine
   144  func TestStopOk(t *testing.T) {
   145  	config := &PooledConfig{NumWorkers: 3, WorkQueueSize: 3}
   146  	runner := NewPooled(config)
   147  	assert.NotNil(t, runner)
   148  	err := runner.Start()
   149  	assert.Nil(t, err)
   150  	err = runner.Stop()
   151  	assert.Nil(t, err)
   152  	assert.False(t, runner.active)
   153  
   154  }