Skip to content

Commit

Permalink
refactor: remove unused baseURI variables related to ServiceEndpoints
Browse files Browse the repository at this point in the history
configuration
  • Loading branch information
cwaldren-ld committed Mar 5, 2024
1 parent 4af62a4 commit cbf7edb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 25 deletions.
30 changes: 12 additions & 18 deletions internal/endpoints/configure_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ func getCustom(serviceEndpoints interfaces.ServiceEndpoints, serviceType Service
}

// IsCustom returns true if the service endpoint has been overridden with a non-default value.
func IsCustom(serviceEndpoints interfaces.ServiceEndpoints, serviceType ServiceType, overrideValue string) bool {
uri := overrideValue
if uri == "" {
uri = getCustom(serviceEndpoints, serviceType)
}
func IsCustom(serviceEndpoints interfaces.ServiceEndpoints, serviceType ServiceType) bool {
uri := getCustom(serviceEndpoints, serviceType)
return uri != "" && strings.TrimSuffix(uri, "/") != strings.TrimSuffix(DefaultBaseURI(serviceType), "/")
}

Expand All @@ -74,23 +71,20 @@ func DefaultBaseURI(serviceType ServiceType) string {
func SelectBaseURI(
serviceEndpoints interfaces.ServiceEndpoints,
serviceType ServiceType,
overrideValue string,
loggers ldlog.Loggers,
) string {
configuredBaseURI := overrideValue
if configuredBaseURI == "" {
if anyCustom(serviceEndpoints) {
configuredBaseURI = getCustom(serviceEndpoints, serviceType)
if configuredBaseURI == "" {
loggers.Errorf(
"You have set custom ServiceEndpoints without specifying the %s base URI; connections may not work properly",
serviceType,
)
configuredBaseURI = DefaultBaseURI(serviceType)
}
} else {
var configuredBaseURI string
if anyCustom(serviceEndpoints) {
configuredBaseURI = getCustom(serviceEndpoints, serviceType)
if configuredBaseURI == "" {
loggers.Errorf(
"You have set custom ServiceEndpoints without specifying the %s base URI; connections may not work properly",
serviceType,
)
configuredBaseURI = DefaultBaseURI(serviceType)
}
} else {
configuredBaseURI = DefaultBaseURI(serviceType)
}
return strings.TrimRight(configuredBaseURI, "/")
}
Expand Down
73 changes: 73 additions & 0 deletions internal/endpoints/configure_endpoints_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package endpoints

import (
"fmt"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-sdk-common/v3/ldlogtest"
"github.com/launchdarkly/go-server-sdk/v7/interfaces"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)

func TestDefaultURISelectedIfNoCustomURISpecified(t *testing.T) {
logger := ldlogtest.NewMockLog()
endpoints := interfaces.ServiceEndpoints{}
services := []ServiceType{StreamingService, PollingService, EventsService}
for _, service := range services {
assert.Equal(t, strings.TrimSuffix(DefaultBaseURI(service), "/"), SelectBaseURI(endpoints, service, logger.Loggers))
}
}

func TestSelectCustomURIs(t *testing.T) {
logger := ldlogtest.NewMockLog()
const customURI = "http://custom_uri"

cases := []struct {
endpoints interfaces.ServiceEndpoints
service ServiceType
}{
{interfaces.ServiceEndpoints{Polling: customURI}, PollingService},
{interfaces.ServiceEndpoints{Streaming: customURI}, StreamingService},
{interfaces.ServiceEndpoints{Events: customURI}, EventsService},
}

for _, c := range cases {
assert.Equal(t, customURI, SelectBaseURI(c.endpoints, c.service, logger.Loggers))
}

assert.Empty(t, logger.GetOutput(ldlog.Error))
}

func TestLogErrorIfAtLeastOneButNotAllCustomURISpecified(t *testing.T) {
logger := ldlogtest.NewMockLog()
const customURI = "http://custom_uri"

cases := []struct {
endpoints interfaces.ServiceEndpoints
service ServiceType
}{
{interfaces.ServiceEndpoints{Streaming: customURI}, PollingService},
{interfaces.ServiceEndpoints{Events: customURI}, PollingService},
{interfaces.ServiceEndpoints{Streaming: customURI, Events: customURI}, PollingService},

{interfaces.ServiceEndpoints{Polling: customURI}, StreamingService},
{interfaces.ServiceEndpoints{Events: customURI}, StreamingService},
{interfaces.ServiceEndpoints{Polling: customURI, Events: customURI}, StreamingService},

{interfaces.ServiceEndpoints{Streaming: customURI}, EventsService},
{interfaces.ServiceEndpoints{Polling: customURI}, EventsService},
{interfaces.ServiceEndpoints{Streaming: customURI, Polling: customURI}, EventsService},
}

// Even if the configuration is considered to be likely malformed, we should still return the proper default URI for
// the service that wasn't configured.
for _, c := range cases {
assert.Equal(t, strings.TrimSuffix(DefaultBaseURI(c.service), "/"), SelectBaseURI(c.endpoints, c.service, logger.Loggers))
}

// For each service that wasn't configured, we should see a log message indicating that.
for _, c := range cases {
logger.AssertMessageMatch(t, true, ldlog.Error, fmt.Sprintf("You have set custom ServiceEndpoints without specifying the %s base URI", c.service))
}
}
3 changes: 1 addition & 2 deletions ldcomponents/polling_data_source_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (b *PollingDataSourceBuilder) Build(context subsystems.ClientContext) (subs
configuredBaseURI := endpoints.SelectBaseURI(
context.GetServiceEndpoints(),
endpoints.PollingService,
b.baseURI,
context.GetLogging().Loggers,
)
cfg := datasource.PollingConfig{
Expand All @@ -107,7 +106,7 @@ func (b *PollingDataSourceBuilder) DescribeConfiguration(context subsystems.Clie
return ldvalue.ObjectBuild().
SetBool("streamingDisabled", true).
SetBool("customBaseURI",
endpoints.IsCustom(context.GetServiceEndpoints(), endpoints.PollingService, b.baseURI)).
endpoints.IsCustom(context.GetServiceEndpoints(), endpoints.PollingService)).
Set("pollingIntervalMillis", durationToMillisValue(b.pollInterval)).
SetBool("usingRelayDaemon", false).
Build()
Expand Down
3 changes: 1 addition & 2 deletions ldcomponents/send_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func (b *EventProcessorBuilder) Build(
configuredBaseURI := endpoints.SelectBaseURI(
context.GetServiceEndpoints(),
endpoints.EventsService,
b.baseURI,
loggers,
)

Expand Down Expand Up @@ -206,7 +205,7 @@ func (b *EventProcessorBuilder) DescribeConfiguration(context subsystems.ClientC
return ldvalue.ObjectBuild().
Set("allAttributesPrivate", ldvalue.Bool(b.allAttributesPrivate)).
Set("customEventsURI", ldvalue.Bool(
endpoints.IsCustom(context.GetServiceEndpoints(), endpoints.EventsService, b.baseURI))).
endpoints.IsCustom(context.GetServiceEndpoints(), endpoints.EventsService))).
Set("diagnosticRecordingIntervalMillis", durationToMillisValue(b.diagnosticRecordingInterval)).
Set("eventsCapacity", ldvalue.Int(b.capacity)).
Set("eventsFlushIntervalMillis", durationToMillisValue(b.flushInterval)).
Expand Down
4 changes: 1 addition & 3 deletions ldcomponents/streaming_data_source_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const DefaultInitialReconnectDelay = time.Second
//
// See StreamingDataSource for usage.
type StreamingDataSourceBuilder struct {
baseURI string
initialReconnectDelay time.Duration
filterKey ldvalue.OptionalString
}
Expand Down Expand Up @@ -81,7 +80,6 @@ func (b *StreamingDataSourceBuilder) Build(context subsystems.ClientContext) (su
configuredBaseURI := endpoints.SelectBaseURI(
context.GetServiceEndpoints(),
endpoints.StreamingService,
b.baseURI,
context.GetLogging().Loggers,
)
cfg := datasource.StreamConfig{
Expand All @@ -101,7 +99,7 @@ func (b *StreamingDataSourceBuilder) DescribeConfiguration(context subsystems.Cl
return ldvalue.ObjectBuild().
SetBool("streamingDisabled", false).
SetBool("customStreamURI",
endpoints.IsCustom(context.GetServiceEndpoints(), endpoints.StreamingService, b.baseURI)).
endpoints.IsCustom(context.GetServiceEndpoints(), endpoints.StreamingService)).
Set("reconnectTimeMillis", durationToMillisValue(b.initialReconnectDelay)).
SetBool("usingRelayDaemon", false).
Build()
Expand Down

0 comments on commit cbf7edb

Please sign in to comment.