Skip to content

Commit

Permalink
refactor: remove unused baseURI variables related to ServiceEndpoints (
Browse files Browse the repository at this point in the history
…#109)

In the streaming/polling/event builders, there seems to be unused baseURI member variables that aren't being set anywhere. I've removed these, which in turn allowed me to remove an unused overrideValue parameter from some of the ServiceEndpoint utility functions.
  • Loading branch information
cwaldren-ld authored Mar 11, 2024
1 parent 4af62a4 commit 85b68a1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 27 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))
}
}
4 changes: 1 addition & 3 deletions ldcomponents/polling_data_source_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const DefaultPollInterval = 30 * time.Second
//
// See [PollingDataSource] for usage.
type PollingDataSourceBuilder struct {
baseURI string
pollInterval time.Duration
filterKey ldvalue.OptionalString
}
Expand Down Expand Up @@ -90,7 +89,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 +105,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
4 changes: 1 addition & 3 deletions ldcomponents/send_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const (
// See [SendEvents] for usage.
type EventProcessorBuilder struct {
allAttributesPrivate bool
baseURI string
capacity int
diagnosticRecordingInterval time.Duration
flushInterval time.Duration
Expand Down Expand Up @@ -74,7 +73,6 @@ func (b *EventProcessorBuilder) Build(
configuredBaseURI := endpoints.SelectBaseURI(
context.GetServiceEndpoints(),
endpoints.EventsService,
b.baseURI,
loggers,
)

Expand Down Expand Up @@ -206,7 +204,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 85b68a1

Please sign in to comment.