Skip to content

Commit

Permalink
refactor: Remove Client monitoring. (#386)
Browse files Browse the repository at this point in the history
closes #385

Signed-off-by: lenny <[email protected]>
  • Loading branch information
lenny-goodell authored Jun 25, 2020
1 parent 0d22c3a commit 0aa127b
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 241 deletions.
68 changes: 5 additions & 63 deletions internal/bootstrap/handlers/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ package handlers

import (
"context"
"fmt"
"sync"
"time"

bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/bootstrap/startup"
"github.com/edgexfoundry/go-mod-bootstrap/di"
"github.com/edgexfoundry/go-mod-core-contracts/clients"
"github.com/edgexfoundry/go-mod-core-contracts/clients/command"
"github.com/edgexfoundry/go-mod-core-contracts/clients/coredata"
"github.com/edgexfoundry/go-mod-core-contracts/clients/notifications"
"github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient/local"

"github.com/edgexfoundry/app-functions-sdk-go/internal"
"github.com/edgexfoundry/app-functions-sdk-go/internal/bootstrap/container"
"github.com/edgexfoundry/app-functions-sdk-go/internal/common"
"github.com/edgexfoundry/app-functions-sdk-go/pkg/urlclient"
)

// Clients contains references to dependencies required by the Clients bootstrap implementation.
Expand All @@ -51,85 +47,31 @@ func (_ *Clients) BootstrapHandler(
startupTimer startup.Timer,
dic *di.Container) bool {

logger := bootstrapContainer.LoggingClientFrom(dic.Get)
config := container.ConfigurationFrom(dic.Get)
registryClient := bootstrapContainer.RegistryFrom(dic.Get)

var eventClient coredata.EventClient
var valueDescriptorClient coredata.ValueDescriptorClient
var commandClient command.CommandClient
var notificationsClient notifications.NotificationsClient

// Need when passing all Clients to other components
clientMonitor, err := time.ParseDuration(config.Service.ClientMonitor)
if err != nil {
logger.Warn(
fmt.Sprintf(
"Service.ClientMonitor failed to parse: %s, use the default value: %v",
err,
internal.ClientMonitorDefault,
),
)
// fall back to default value
clientMonitor = internal.ClientMonitorDefault
}

interval := int(clientMonitor / time.Millisecond)

// Use of these client interfaces is optional, so they are not required to be configured. For instance if not
// sending commands, then don't need to have the Command client in the configuration.
if _, ok := config.Clients[common.CoreDataClientName]; ok {
eventClient = coredata.NewEventClient(
urlclient.New(
ctx,
wg,
registryClient,
clients.CoreDataServiceKey,
clients.ApiEventRoute,
interval,
config.Clients[common.CoreDataClientName].Url()+clients.ApiEventRoute,
),
)
local.New(config.Clients[common.CoreDataClientName].Url() + clients.ApiEventRoute))

valueDescriptorClient = coredata.NewValueDescriptorClient(
urlclient.New(
ctx,
wg,
registryClient,
clients.CoreDataServiceKey,
clients.ApiValueDescriptorRoute,
interval,
config.Clients[common.CoreDataClientName].Url()+clients.ApiValueDescriptorRoute,
),
)
local.New(config.Clients[common.CoreDataClientName].Url() + clients.ApiValueDescriptorRoute))
}

if _, ok := config.Clients[common.CoreCommandClientName]; ok {
commandClient = command.NewCommandClient(
urlclient.New(
ctx,
wg,
registryClient,
clients.CoreCommandServiceKey,
clients.ApiDeviceRoute,
interval,
config.Clients[common.CoreCommandClientName].Url()+clients.ApiDeviceRoute,
),
)
local.New(config.Clients[common.CoreCommandClientName].Url() + clients.ApiDeviceRoute))
}

if _, ok := config.Clients[common.NotificationsClientName]; ok {
notificationsClient = notifications.NewNotificationsClient(
urlclient.New(
ctx,
wg,
registryClient,
clients.SupportNotificationsServiceKey,
clients.ApiNotificationRoute,
interval,
config.Clients[common.NotificationsClientName].Url()+clients.ApiNotificationRoute,
),
)
local.New(config.Clients[common.NotificationsClientName].Url() + clients.ApiNotificationRoute))
}

// Note that all the clients are optional so some or all these clients may be nil
Expand Down
28 changes: 3 additions & 25 deletions internal/bootstrap/handlers/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ import (

func TestClientsBootstrapHandler(t *testing.T) {
configuration := &common.ConfigurationStruct{
Service: common.ServiceInfo{
ClientMonitor: "5s",
},
Service: common.ServiceInfo{},
}

logger := logging.FactoryToStdout("clients-test")
Expand Down Expand Up @@ -79,46 +77,29 @@ func TestClientsBootstrapHandler(t *testing.T) {
CoreDataClientInfo *config.ClientInfo
CommandClientInfo *config.ClientInfo
NotificationsClientInfo *config.ClientInfo
MonitorDuration string
ExpectSuccess bool
}{
{
Name: "All Clients",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: &commandClientInfo,
NotificationsClientInfo: &notificationsClientInfo,
MonitorDuration: "5s",
ExpectSuccess: true,
},
{
Name: "No Clients",
CoreDataClientInfo: nil,
CommandClientInfo: nil,
NotificationsClientInfo: nil,
MonitorDuration: "5s",
ExpectSuccess: true,
},
{
Name: "Only Core Data Clients",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: nil,
NotificationsClientInfo: nil,
MonitorDuration: "5s",
ExpectSuccess: true,
},
{
Name: "Invalid MonitorDuration",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: nil,
NotificationsClientInfo: nil,
MonitorDuration: "bogus",
ExpectSuccess: true,
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
configuration.Service.ClientMonitor = test.MonitorDuration
configuration.Clients = make(map[string]config.ClientInfo)

if test.CoreDataClientInfo != nil {
Expand All @@ -139,11 +120,8 @@ func TestClientsBootstrapHandler(t *testing.T) {
},
})

actualSuccess := NewClients().BootstrapHandler(context.Background(), &sync.WaitGroup{}, startupTimer, dic)
require.Equal(t, test.ExpectSuccess, actualSuccess)
if actualSuccess == false {
return // Test is complete
}
success := NewClients().BootstrapHandler(context.Background(), &sync.WaitGroup{}, startupTimer, dic)
require.True(t, success)

eventClient := container.EventClientFrom(dic.Get)
valueDescriptorClient := container.ValueDescriptorClientFrom(dic.Get)
Expand Down
2 changes: 0 additions & 2 deletions internal/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ type ConfigurationStruct struct {
type ServiceInfo struct {
BootTimeout string
CheckInterval string
ClientMonitor string
Host string
HTTPSCert string
HTTPSKey string
Expand Down Expand Up @@ -186,7 +185,6 @@ func (c *ConfigurationStruct) transformToBootstrapServiceInfo() bootstrapConfig.
return bootstrapConfig.ServiceInfo{
BootTimeout: durationToMill(c.Service.BootTimeout),
CheckInterval: c.Service.CheckInterval,
ClientMonitor: durationToMill(c.Service.ClientMonitor),
Host: c.Service.Host,
Port: c.Service.Port,
Protocol: c.Service.Protocol,
Expand Down
2 changes: 1 addition & 1 deletion internal/webserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestConfigureAndConfigRoute(t *testing.T) {
rr := httptest.NewRecorder()
webserver.router.ServeHTTP(rr, req)

expected := `{"Writable":{"LogLevel":"","Pipeline":{"ExecutionOrder":"","UseTargetTypeOfByteArray":false,"Functions":null},"StoreAndForward":{"Enabled":false,"RetryInterval":"","MaxRetryCount":0},"InsecureSecrets":null},"Logging":{"EnableRemote":false,"File":""},"Registry":{"Host":"","Port":0,"Type":""},"Service":{"BootTimeout":"","CheckInterval":"","ClientMonitor":"","Host":"","HTTPSCert":"","HTTPSKey":"","Port":0,"Protocol":"","StartupMsg":"","ReadMaxLimit":0,"Timeout":""},"MessageBus":{"PublishHost":{"Host":"","Port":0,"Protocol":""},"SubscribeHost":{"Host":"","Port":0,"Protocol":""},"Type":"","Optional":null},"Binding":{"Type":"","SubscribeTopic":"","PublishTopic":""},"ApplicationSettings":null,"Clients":null,"Database":{"Type":"","Host":"","Port":0,"Timeout":"","Username":"","Password":"","MaxIdle":0,"BatchSize":0},"SecretStore":{"Host":"","Port":0,"Path":"","Protocol":"","Namespace":"","RootCaCertPath":"","ServerName":"","Authentication":{"AuthType":"","AuthToken":""},"AdditionalRetryAttempts":0,"RetryWaitPeriod":"","TokenFile":""},"SecretStoreExclusive":{"Host":"","Port":0,"Path":"","Protocol":"","Namespace":"","RootCaCertPath":"","ServerName":"","Authentication":{"AuthType":"","AuthToken":""},"AdditionalRetryAttempts":0,"RetryWaitPeriod":"","TokenFile":""},"Startup":{"Duration":0,"Interval":0}}` + "\n"
expected := `{"Writable":{"LogLevel":"","Pipeline":{"ExecutionOrder":"","UseTargetTypeOfByteArray":false,"Functions":null},"StoreAndForward":{"Enabled":false,"RetryInterval":"","MaxRetryCount":0},"InsecureSecrets":null},"Logging":{"EnableRemote":false,"File":""},"Registry":{"Host":"","Port":0,"Type":""},"Service":{"BootTimeout":"","CheckInterval":"","Host":"","HTTPSCert":"","HTTPSKey":"","Port":0,"Protocol":"","StartupMsg":"","ReadMaxLimit":0,"Timeout":""},"MessageBus":{"PublishHost":{"Host":"","Port":0,"Protocol":""},"SubscribeHost":{"Host":"","Port":0,"Protocol":""},"Type":"","Optional":null},"Binding":{"Type":"","SubscribeTopic":"","PublishTopic":""},"ApplicationSettings":null,"Clients":null,"Database":{"Type":"","Host":"","Port":0,"Timeout":"","Username":"","Password":"","MaxIdle":0,"BatchSize":0},"SecretStore":{"Host":"","Port":0,"Path":"","Protocol":"","Namespace":"","RootCaCertPath":"","ServerName":"","Authentication":{"AuthType":"","AuthToken":""},"AdditionalRetryAttempts":0,"RetryWaitPeriod":"","TokenFile":""},"SecretStoreExclusive":{"Host":"","Port":0,"Path":"","Protocol":"","Namespace":"","RootCaCertPath":"","ServerName":"","Authentication":{"AuthType":"","AuthToken":""},"AdditionalRetryAttempts":0,"RetryWaitPeriod":"","TokenFile":""},"Startup":{"Duration":0,"Interval":0}}` + "\n"

body := rr.Body.String()
assert.Equal(t, expected, body)
Expand Down
88 changes: 0 additions & 88 deletions pkg/endpoint/endpoint.go

This file was deleted.

10 changes: 4 additions & 6 deletions pkg/transforms/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ package transforms
import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient/local"
"github.com/stretchr/testify/require"

"github.com/edgexfoundry/go-mod-core-contracts/clients"
"github.com/edgexfoundry/go-mod-core-contracts/clients/coredata"
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
"github.com/stretchr/testify/assert"

"github.com/edgexfoundry/app-functions-sdk-go/appcontext"
"github.com/edgexfoundry/app-functions-sdk-go/pkg/urlclient"

"github.com/edgexfoundry/go-mod-core-contracts/models"

"github.com/edgexfoundry/app-functions-sdk-go/appcontext"
)

var context *appcontext.Context
Expand All @@ -42,9 +42,7 @@ const (

func init() {
lc := logger.NewClient("app_functions_sdk_go", false, "./test.log", "DEBUG")
eventClient := coredata.NewEventClient(
urlclient.New(nil, nil, nil, "", "", 0, "http://test"+clients.ApiEventRoute),
)
eventClient := coredata.NewEventClient(local.New("http://test" + clients.ApiEventRoute))
mockSP := newMockSecretProvider(lc, nil)

context = &appcontext.Context{
Expand Down
56 changes: 0 additions & 56 deletions pkg/urlclient/urlclient.go

This file was deleted.

0 comments on commit 0aa127b

Please sign in to comment.