Skip to content

Commit

Permalink
feat: Add Cron Scheduler Clients in ClientsBootstrap Handler
Browse files Browse the repository at this point in the history
closes: #744

Signed-off-by: Jack Chen <jack@iotechsys.com>
  • Loading branch information
jackchenjc committed Aug 27, 2024
1 parent 4767b51 commit e78dd3b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 64 deletions.
25 changes: 25 additions & 0 deletions bootstrap/container/clients.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright (c) 2022 Intel Corporation
// Copyright (C) 2024 IOTech Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -179,3 +180,27 @@ func DeviceServiceCommandClientFrom(get di.Get) interfaces.DeviceServiceCommandC

return client
}

// ScheduleJobClientName contains the name of the ScheduleJobClient's implementation in the DIC.
var ScheduleJobClientName = di.TypeInstanceToName((*interfaces.ScheduleJobClient)(nil))

// ScheduleJobClientFrom helper function queries the DIC and returns the ScheduleJobClient's implementation.
func ScheduleJobClientFrom(get di.Get) interfaces.ScheduleJobClient {
if get(ScheduleJobClientName) == nil {
return nil
}

return get(ScheduleJobClientName).(interfaces.ScheduleJobClient)
}

// ScheduleActionRecordClientName contains the name of the ScheduleActionRecordClient's implementation in the DIC.
var ScheduleActionRecordClientName = di.TypeInstanceToName((*interfaces.ScheduleActionRecordClient)(nil))

// ScheduleActionRecordClientFrom helper function queries the DIC and returns the ScheduleActionRecordClient's implementation.
func ScheduleActionRecordClientFrom(get di.Get) interfaces.ScheduleActionRecordClient {
if get(ScheduleActionRecordClientName) == nil {
return nil
}

return get(ScheduleActionRecordClientName).(interfaces.ScheduleActionRecordClient)
}
11 changes: 11 additions & 0 deletions bootstrap/handlers/clients.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*******************************************************************************
* Copyright 2022 Intel Inc.
* Copyright (C) 2023 Intel Corporation
* Copyright (C) 2024 IOTech Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -175,6 +176,16 @@ func (cb *ClientsBootstrap) BootstrapHandler(
},
})

case common.SupportCronSchedulerServiceKey:
dic.Update(di.ServiceConstructorMap{
container.ScheduleJobClientName: func(get di.Get) interface{} {
return clients.NewScheduleJobClient(url, jwtSecretProvider, cfg.GetBootstrap().Service.EnableNameFieldEscape)
},
container.ScheduleActionRecordClientName: func(get di.Get) interface{} {
return clients.NewScheduleActionRecordClient(url, jwtSecretProvider, cfg.GetBootstrap().Service.EnableNameFieldEscape)
},
})

default:

}
Expand Down
158 changes: 94 additions & 64 deletions bootstrap/handlers/clients_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright (c) 2022 Intel Corporation
// Copyright (C) 2024 IOTech Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -77,97 +78,112 @@ func TestClientsBootstrapHandler(t *testing.T) {
Protocol: "http",
}

cronSchedulerClientInfo := config.ClientInfo{
Host: "localhost",
Port: 59863,
Protocol: "http",
}

registryMock := &registryMocks.Client{}
registryMock.On("GetServiceEndpoint", common.CoreDataServiceKey).Return(types.ServiceEndpoint{}, nil)
registryMock.On("GetServiceEndpoint", common.CoreMetaDataServiceKey).Return(types.ServiceEndpoint{}, nil)
registryMock.On("GetServiceEndpoint", common.CoreCommandServiceKey).Return(types.ServiceEndpoint{}, nil)
registryMock.On("GetServiceEndpoint", common.SupportNotificationsServiceKey).Return(types.ServiceEndpoint{}, nil)
registryMock.On("GetServiceEndpoint", common.SupportSchedulerServiceKey).Return(types.ServiceEndpoint{}, nil)
registryMock.On("GetServiceEndpoint", common.SupportCronSchedulerServiceKey).Return(types.ServiceEndpoint{}, nil)

registryErrorMock := &registryMocks.Client{}
registryErrorMock.On("GetServiceEndpoint", common.CoreDataServiceKey).Return(types.ServiceEndpoint{}, errors.New("some error"))

startupTimer := startup.NewTimer(1, 1)

tests := []struct {
Name string
CoreDataClientInfo *config.ClientInfo
CommandClientInfo *config.ClientInfo
MetadataClientInfo *config.ClientInfo
NotificationClientInfo *config.ClientInfo
SchedulerClientInfo *config.ClientInfo
Registry registry.Client
ExpectedResult bool
Name string
CoreDataClientInfo *config.ClientInfo
CommandClientInfo *config.ClientInfo
MetadataClientInfo *config.ClientInfo
NotificationClientInfo *config.ClientInfo
SchedulerClientInfo *config.ClientInfo
CronSchedulerClientInfo *config.ClientInfo
Registry registry.Client
ExpectedResult bool
}{
{
Name: "All ClientsBootstrap",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: &commandHttpClientInfo,
MetadataClientInfo: &metadataClientInfo,
NotificationClientInfo: &notificationClientInfo,
SchedulerClientInfo: &subscriberClientInfo,
Registry: nil,
ExpectedResult: true,
Name: "All ClientsBootstrap",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: &commandHttpClientInfo,
MetadataClientInfo: &metadataClientInfo,
NotificationClientInfo: &notificationClientInfo,
SchedulerClientInfo: &subscriberClientInfo,
CronSchedulerClientInfo: &cronSchedulerClientInfo,
Registry: nil,
ExpectedResult: true,
},
{
Name: "All ClientsBootstrap using registry",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: &commandHttpClientInfo,
MetadataClientInfo: &metadataClientInfo,
NotificationClientInfo: &notificationClientInfo,
SchedulerClientInfo: &subscriberClientInfo,
Registry: registryMock,
ExpectedResult: true,
Name: "All ClientsBootstrap using registry",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: &commandHttpClientInfo,
MetadataClientInfo: &metadataClientInfo,
NotificationClientInfo: &notificationClientInfo,
SchedulerClientInfo: &subscriberClientInfo,
CronSchedulerClientInfo: &cronSchedulerClientInfo,
Registry: registryMock,
ExpectedResult: true,
},
{
Name: "Core Data Client using registry fails",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: nil,
MetadataClientInfo: nil,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
Registry: registryErrorMock,
ExpectedResult: false,
Name: "Core Data Client using registry fails",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: nil,
MetadataClientInfo: nil,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
CronSchedulerClientInfo: nil,
Registry: registryErrorMock,
ExpectedResult: false,
},
{
Name: "No ClientsBootstrap",
CoreDataClientInfo: nil,
CommandClientInfo: nil,
MetadataClientInfo: nil,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
Registry: nil,
ExpectedResult: true,
Name: "No ClientsBootstrap",
CoreDataClientInfo: nil,
CommandClientInfo: nil,
MetadataClientInfo: nil,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
CronSchedulerClientInfo: nil,
Registry: nil,
ExpectedResult: true,
},
{
Name: "Only Core Data ClientsBootstrap",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: nil,
MetadataClientInfo: nil,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
Registry: nil,
ExpectedResult: true,
Name: "Only Core Data ClientsBootstrap",
CoreDataClientInfo: &coreDataClientInfo,
CommandClientInfo: nil,
MetadataClientInfo: nil,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
CronSchedulerClientInfo: nil,
Registry: nil,
ExpectedResult: true,
},
{
Name: "Only Metadata ClientsBootstrap",
CoreDataClientInfo: nil,
CommandClientInfo: nil,
MetadataClientInfo: &metadataClientInfo,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
Registry: nil,
ExpectedResult: true,
Name: "Only Metadata ClientsBootstrap",
CoreDataClientInfo: nil,
CommandClientInfo: nil,
MetadataClientInfo: &metadataClientInfo,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
CronSchedulerClientInfo: nil,
Registry: nil,
ExpectedResult: true,
},
{
Name: "Only Messaging based Command ClientsBootstrap",
CoreDataClientInfo: nil,
CommandClientInfo: &commandMessagingClientInfo,
MetadataClientInfo: nil,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
Registry: nil,
ExpectedResult: true,
Name: "Only Messaging based Command ClientsBootstrap",
CoreDataClientInfo: nil,
CommandClientInfo: &commandMessagingClientInfo,
MetadataClientInfo: nil,
NotificationClientInfo: nil,
SchedulerClientInfo: nil,
CronSchedulerClientInfo: nil,
Registry: nil,
ExpectedResult: true,
},
}

Expand Down Expand Up @@ -195,6 +211,10 @@ func TestClientsBootstrapHandler(t *testing.T) {
clients[common.SupportSchedulerServiceKey] = test.SchedulerClientInfo
}

if test.CronSchedulerClientInfo != nil {
clients[common.SupportCronSchedulerServiceKey] = test.CronSchedulerClientInfo
}

bootstrapConfig := config.BootstrapConfiguration{
Service: &config.ServiceInfo{
RequestTimeout: "30s",
Expand Down Expand Up @@ -249,6 +269,8 @@ func TestClientsBootstrapHandler(t *testing.T) {
subscriptionClient := container.SubscriptionClientFrom(dic.Get)
intervalClient := container.IntervalClientFrom(dic.Get)
intervalActionClient := container.IntervalActionClientFrom(dic.Get)
scheduleJobClient := container.ScheduleJobClientFrom(dic.Get)
scheduleActionRecordClient := container.ScheduleActionRecordClientFrom(dic.Get)

if test.CoreDataClientInfo != nil {
assert.NotNil(t, eventClient)
Expand Down Expand Up @@ -292,6 +314,14 @@ func TestClientsBootstrapHandler(t *testing.T) {
assert.Nil(t, intervalActionClient)
}

if test.CronSchedulerClientInfo != nil {
assert.NotNil(t, scheduleJobClient)
assert.NotNil(t, scheduleActionRecordClient)
} else {
assert.Nil(t, scheduleJobClient)
assert.Nil(t, scheduleActionRecordClient)
}

if test.Registry != nil {
registryMock.AssertExpectations(t)
}
Expand Down

0 comments on commit e78dd3b

Please sign in to comment.