Skip to content

Commit

Permalink
refactor(triggers): Normalize Orchestration
Browse files Browse the repository at this point in the history
Share functionality between custom and built-in triggers, expanding
testability of those built-in.

fixes #960

Signed-off-by: Alex Ullrich <[email protected]>
  • Loading branch information
AlexCuse committed Dec 29, 2021
1 parent d30acd6 commit 2d6c7c3
Show file tree
Hide file tree
Showing 23 changed files with 1,198 additions and 808 deletions.
65 changes: 0 additions & 65 deletions internal/app/mocks/TriggerServiceBinding.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (svc *Service) MakeItRun() error {
svc.ctx.stop = stop

// determine input type and create trigger for it
t := svc.setupTrigger(svc.config, svc.runtime)
t := svc.setupTrigger(svc.config)
if t == nil {
return errors.New("failed to create Trigger")
}
Expand Down
10 changes: 8 additions & 2 deletions internal/app/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ func TestSetupHTTPTrigger(t *testing.T) {

testRuntime := runtime.NewGolangRuntime("", nil, dic)
testRuntime.SetDefaultFunctionsPipeline(nil)
trigger := sdk.setupTrigger(sdk.config, testRuntime)

sdk.runtime = testRuntime

trigger := sdk.setupTrigger(sdk.config)
result := IsInstanceOf(trigger, (*triggerHttp.Trigger)(nil))
assert.True(t, result, "Expected Instance of HTTP Trigger")
}
Expand All @@ -259,7 +262,10 @@ func TestSetupMessageBusTrigger(t *testing.T) {
}
testRuntime := runtime.NewGolangRuntime("", nil, dic)
testRuntime.SetDefaultFunctionsPipeline(nil)
trigger := sdk.setupTrigger(sdk.config, testRuntime)

sdk.runtime = testRuntime

trigger := sdk.setupTrigger(sdk.config)
result := IsInstanceOf(trigger, (*messagebus.Trigger)(nil))
assert.True(t, result, "Expected Instance of Message Bus Trigger")
}
Expand Down
12 changes: 7 additions & 5 deletions internal/app/triggerfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package app
import (
"fmt"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/common"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/runtime"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger/http"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger/messagebus"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger/mqtt"
Expand All @@ -36,21 +35,24 @@ const (
TriggerTypeHTTP = "HTTP"
)

func (svc *Service) setupTrigger(configuration *common.ConfigurationStruct, runtime *runtime.GolangRuntime) interfaces.Trigger {
func (svc *Service) setupTrigger(configuration *common.ConfigurationStruct) interfaces.Trigger {
var t interfaces.Trigger

bnd := NewTriggerServiceBinding(svc)
mp := &triggerMessageProcessor{bnd}

switch triggerType := strings.ToUpper(configuration.Trigger.Type); triggerType {
case TriggerTypeHTTP:
svc.LoggingClient().Info("HTTP trigger selected")
t = http.NewTrigger(svc.dic, svc.runtime, svc.webserver)
t = http.NewTrigger(bnd, mp, svc.webserver)

case TriggerTypeMessageBus:
svc.LoggingClient().Info("EdgeX MessageBus trigger selected")
t = messagebus.NewTrigger(svc.dic, svc.runtime)
t = messagebus.NewTrigger(bnd, mp)

case TriggerTypeMQTT:
svc.LoggingClient().Info("External MQTT trigger selected")
t = mqtt.NewTrigger(svc.dic, svc.runtime)
t = mqtt.NewTrigger(bnd, mp)

default:
if factory, found := svc.customTriggerFactories[triggerType]; found {
Expand Down
12 changes: 6 additions & 6 deletions internal/app/triggerfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestSetupTrigger_HTTP(t *testing.T) {
lc: logger.MockLogger{},
}

trigger := sdk.setupTrigger(sdk.config, nil)
trigger := sdk.setupTrigger(sdk.config)

require.NotNil(t, trigger, "should be defined")
require.IsType(t, &http.Trigger{}, trigger, "should be an http trigger")
Expand All @@ -118,7 +118,7 @@ func TestSetupTrigger_EdgeXMessageBus(t *testing.T) {
lc: logger.MockLogger{},
}

trigger := sdk.setupTrigger(sdk.config, nil)
trigger := sdk.setupTrigger(sdk.config)

require.NotNil(t, trigger, "should be defined")
require.IsType(t, &messagebus.Trigger{}, trigger, "should be an edgex-messagebus trigger")
Expand All @@ -143,7 +143,7 @@ func TestSetupTrigger_MQTT(t *testing.T) {
lc: lc,
}

trigger := sdk.setupTrigger(sdk.config, nil)
trigger := sdk.setupTrigger(sdk.config)

require.NotNil(t, trigger, "should be defined")
require.IsType(t, &mqtt.Trigger{}, trigger, "should be an external-MQTT trigger")
Expand Down Expand Up @@ -173,7 +173,7 @@ func Test_Service_setupTrigger_CustomType(t *testing.T) {
})
require.NoError(t, err)

trigger := sdk.setupTrigger(sdk.config, nil)
trigger := sdk.setupTrigger(sdk.config)

require.NotNil(t, trigger, "should be defined")
require.IsType(t, &mockCustomTrigger{}, trigger, "should be a custom trigger")
Expand All @@ -196,7 +196,7 @@ func Test_Service_SetupTrigger_CustomTypeError(t *testing.T) {
})
require.NoError(t, err)

trigger := sdk.setupTrigger(sdk.config, nil)
trigger := sdk.setupTrigger(sdk.config)

require.Nil(t, trigger, "should be nil")
}
Expand All @@ -213,7 +213,7 @@ func Test_Service_SetupTrigger_CustomTypeNotFound(t *testing.T) {
lc: logger.MockLogger{},
}

trigger := sdk.setupTrigger(sdk.config, nil)
trigger := sdk.setupTrigger(sdk.config)

require.Nil(t, trigger, "should be nil")
}
4 changes: 2 additions & 2 deletions internal/app/triggermessageprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (b *simpleTriggerServiceBinding) Config() *common.ConfigurationStruct {
return b.Service.config
}

// customTriggerBinding wraps the CustomTriggerServiceBinding interface so that we can attach methods
// triggerMessageProcessor wraps the ServiceBinding interface so that we can attach methods
type triggerMessageProcessor struct {
bnd trigger.ServiceBinding
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (mp *triggerMessageProcessor) Process(ctx interfaces.AppFunctionContext, en
func (mp *triggerMessageProcessor) MessageReceived(ctx interfaces.AppFunctionContext, envelope types.MessageEnvelope, responseHandler interfaces.PipelineResponseHandler) error {
lc := mp.bnd.LoggingClient()

lc.Debugf("custom trigger attempting to find pipeline(s) for topic %s", envelope.ReceivedTopic)
lc.Debugf("trigger attempting to find pipeline(s) for topic %s", envelope.ReceivedTopic)

// ensure we have a context established that we can safely cast to *appfunction.Context to pass to runtime
if _, ok := ctx.(*appfunction.Context); ctx == nil || !ok {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/triggermessageprocessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func Test_triggerMessageProcessor_MessageReceived(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tsb := triggerMocks.ServiceBinding{}

tsb.On("ProcessMessage", mock.Anything, mock.Anything, mock.Anything).Return(tt.setup.runtimeProcessor)
tsb.On("ProcessMessage", mock.AnythingOfType("*appfunction.Context"), mock.AnythingOfType("types.MessageEnvelope"), mock.AnythingOfType("*interfaces.FunctionPipeline")).Return(tt.setup.runtimeProcessor)
tsb.On("GetMatchingPipelines", tt.args.envelope.ReceivedTopic).Return(tt.setup.pipelineMatcher)
tsb.On("LoggingClient").Return(lc)

Expand Down
31 changes: 31 additions & 0 deletions internal/trigger/http/mocks/TriggerRequestReader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions internal/trigger/http/mocks/TriggerResponseWriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions internal/trigger/http/mocks/TriggerRouteManager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2d6c7c3

Please sign in to comment.