Skip to content

Commit

Permalink
chore: Re-introduce polling and streaming unit tests (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 authored Feb 3, 2025
1 parent c2d1814 commit 7bea3e3
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 33 deletions.
7 changes: 0 additions & 7 deletions internal/datasource/data_source_test_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ import (

"github.com/launchdarkly/go-server-sdk/v7/internal/datastore"
"github.com/launchdarkly/go-server-sdk/v7/internal/sharedtest"
"github.com/launchdarkly/go-server-sdk/v7/subsystems"

th "github.com/launchdarkly/go-test-helpers/v3"
)

const testSDKKey = "test-sdk-key"

func basicClientContext() subsystems.ClientContext {
return sharedtest.NewSimpleTestContext(testSDKKey)
}

func withMockDataSourceUpdates(action func(*mocks.MockDataSourceUpdates)) {
d := mocks.NewMockDataSourceUpdates(datastore.NewInMemoryDataStore(sharedtest.NewTestLoggers()))
// currently don't need to defer any cleanup actions
Expand Down
12 changes: 6 additions & 6 deletions internal/datasource/polling_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestPollingProcessorClosingItShouldNotBlock(t *testing.T) {
r.RequestAllRespCh <- mocks.RequestAllResponse{}

withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
p := newPollingProcessor(basicClientContext(), dataSourceUpdates, r, time.Minute)
p := newPollingProcessor(sharedtest.BasicClientContext(), dataSourceUpdates, r, time.Minute)

p.Close()

Expand All @@ -49,7 +49,7 @@ func TestPollingProcessorInitialization(t *testing.T) {
r.RequestAllRespCh <- resp

withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
p := newPollingProcessor(basicClientContext(), dataSourceUpdates, r, time.Millisecond*10)
p := newPollingProcessor(sharedtest.BasicClientContext(), dataSourceUpdates, r, time.Millisecond*10)
defer p.Close()

closeWhenReady := make(chan struct{})
Expand Down Expand Up @@ -116,7 +116,7 @@ func testPollingProcessorRecoverableError(t *testing.T, err error, verifyError f
req.RequestAllRespCh <- mocks.RequestAllResponse{Err: err}

withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
p := newPollingProcessor(basicClientContext(), dataSourceUpdates, req, time.Millisecond*10)
p := newPollingProcessor(sharedtest.BasicClientContext(), dataSourceUpdates, req, time.Millisecond*10)
defer p.Close()
closeWhenReady := make(chan struct{})
p.Start(closeWhenReady)
Expand Down Expand Up @@ -168,7 +168,7 @@ func testPollingProcessorUnrecoverableError(
req.RequestAllRespCh <- mocks.RequestAllResponse{} // we shouldn't get a second request, but just in case

withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
p := newPollingProcessor(basicClientContext(), dataSourceUpdates, req, time.Millisecond*10)
p := newPollingProcessor(sharedtest.BasicClientContext(), dataSourceUpdates, req, time.Millisecond*10)
defer p.Close()
closeWhenReady := make(chan struct{})
p.Start(closeWhenReady)
Expand All @@ -191,7 +191,7 @@ func TestPollingProcessorUsesHTTPClientFactory(t *testing.T) {
withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
httpClientFactory := urlAppendingHTTPClientFactory("/transformed")
httpConfig := subsystems.HTTPConfiguration{CreateHTTPClient: httpClientFactory}
context := sharedtest.NewTestContext(testSDKKey, &httpConfig, nil)
context := sharedtest.NewTestContext(sharedtest.TestSDKKey, &httpConfig, nil)

p := NewPollingProcessor(context, dataSourceUpdates, PollingConfig{
BaseURI: ts.URL,
Expand All @@ -216,7 +216,7 @@ func TestPollingProcessorAppendsFilterParameter(t *testing.T) {
pollHandler, requestsCh := httphelpers.RecordingHandler(ldservices.ServerSidePollingServiceHandler(data))
httphelpers.WithServer(pollHandler, func(ts *httptest.Server) {
withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
p := NewPollingProcessor(basicClientContext(), dataSourceUpdates, PollingConfig{
p := NewPollingProcessor(sharedtest.BasicClientContext(), dataSourceUpdates, PollingConfig{
BaseURI: ts.URL,
PollInterval: time.Minute * 30,
FilterKey: filter.key,
Expand Down
20 changes: 10 additions & 10 deletions internal/datasource/polling_http_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestRequestorImplRequestAll(t *testing.T) {
ldservices.ServerSidePollingServiceHandler(expectedData.ToServerSDKData()),
)
httphelpers.WithServer(handler, func(ts *httptest.Server) {
r := newPollingRequester(basicClientContext(), nil, ts.URL, filter.key)
r := newPollingRequester(sharedtest.BasicClientContext(), nil, ts.URL, filter.key)

data, cached, err := r.Request()

Expand All @@ -47,7 +47,7 @@ func TestRequestorImplRequestAll(t *testing.T) {
t.Run("HTTP error response", func(t *testing.T) {
handler := httphelpers.HandlerWithStatus(500)
httphelpers.WithServer(handler, func(ts *httptest.Server) {
r := newPollingRequester(basicClientContext(), nil, ts.URL, filter.key)
r := newPollingRequester(sharedtest.BasicClientContext(), nil, ts.URL, filter.key)

data, cached, err := r.Request()

Expand All @@ -67,7 +67,7 @@ func TestRequestorImplRequestAll(t *testing.T) {
httphelpers.WithServer(handler, func(ts *httptest.Server) {
closedServerURL = ts.URL
})
r := newPollingRequester(basicClientContext(), nil, closedServerURL, filter.key)
r := newPollingRequester(sharedtest.BasicClientContext(), nil, closedServerURL, filter.key)

data, cached, err := r.Request()

Expand All @@ -79,7 +79,7 @@ func TestRequestorImplRequestAll(t *testing.T) {
t.Run("malformed data", func(t *testing.T) {
handler := httphelpers.HandlerWithResponse(200, nil, []byte("{"))
httphelpers.WithServer(handler, func(ts *httptest.Server) {
r := newPollingRequester(basicClientContext(), nil, ts.URL, filter.key)
r := newPollingRequester(sharedtest.BasicClientContext(), nil, ts.URL, filter.key)

data, cached, err := r.Request()

Expand All @@ -92,7 +92,7 @@ func TestRequestorImplRequestAll(t *testing.T) {
})

t.Run("malformed base URI", func(t *testing.T) {
r := newPollingRequester(basicClientContext(), nil, "::::", filter.key)
r := newPollingRequester(sharedtest.BasicClientContext(), nil, "::::", filter.key)

data, cached, err := r.Request()

Expand All @@ -109,7 +109,7 @@ func TestRequestorImplRequestAll(t *testing.T) {
httphelpers.HandlerWithJSONResponse(ldservices.NewServerSDKData(), nil),
)
httpConfig := subsystems.HTTPConfiguration{DefaultHeaders: headers}
context := sharedtest.NewTestContext(testSDKKey, &httpConfig, nil)
context := sharedtest.NewTestContext(sharedtest.TestSDKKey, &httpConfig, nil)

httphelpers.WithServer(handler, func(ts *httptest.Server) {
r := newPollingRequester(context, nil, ts.URL, filter.key)
Expand All @@ -125,7 +125,7 @@ func TestRequestorImplRequestAll(t *testing.T) {
t.Run("logs debug message", func(t *testing.T) {
mockLog := ldlogtest.NewMockLog()
mockLog.Loggers.SetMinLevel(ldlog.Debug)
context := sharedtest.NewTestContext(testSDKKey, nil, &subsystems.LoggingConfiguration{Loggers: mockLog.Loggers})
context := sharedtest.NewTestContext(sharedtest.TestSDKKey, nil, &subsystems.LoggingConfiguration{Loggers: mockLog.Loggers})
handler := httphelpers.HandlerWithJSONResponse(ldservices.NewServerSDKData(), nil)

httphelpers.WithServer(handler, func(ts *httptest.Server) {
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestRequestorImplCaching(t *testing.T) {
),
)
httphelpers.WithServer(handler, func(ts *httptest.Server) {
r := newPollingRequester(basicClientContext(), nil, ts.URL, filter.key)
r := newPollingRequester(sharedtest.BasicClientContext(), nil, ts.URL, filter.key)

data1, cached1, err1 := r.Request()

Expand Down Expand Up @@ -192,7 +192,7 @@ func TestRequestorImplCanUseCustomHTTPClientFactory(t *testing.T) {
pollHandler, requestsCh := httphelpers.RecordingHandler(ldservices.ServerSidePollingServiceHandler(data))
httpClientFactory := urlAppendingHTTPClientFactory("/transformed")
httpConfig := subsystems.HTTPConfiguration{CreateHTTPClient: httpClientFactory}
context := sharedtest.NewTestContext(testSDKKey, &httpConfig, nil)
context := sharedtest.NewTestContext(sharedtest.TestSDKKey, &httpConfig, nil)

httphelpers.WithServer(pollHandler, func(ts *httptest.Server) {
r := newPollingRequester(context, nil, ts.URL, "")
Expand All @@ -212,7 +212,7 @@ func TestRequestorImplCanAppendsFilterParameter(t *testing.T) {

testWithFilters(t, func(t *testing.T, filter filterTest) {
httphelpers.WithServer(pollHandler, func(ts *httptest.Server) {
r := newPollingRequester(basicClientContext(), nil, ts.URL, filter.key)
r := newPollingRequester(sharedtest.BasicClientContext(), nil, ts.URL, filter.key)

_, _, _ = r.Request()

Expand Down
18 changes: 9 additions & 9 deletions internal/datasource/streaming_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,11 @@ func testStreamProcessorUnrecoverableHTTPError(t *testing.T, statusCode int) {
defer mockLog.DumpIfTestFailed(t)
httphelpers.WithServer(httphelpers.HandlerWithStatus(statusCode), func(ts *httptest.Server) {
withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
id := ldevents.NewDiagnosticID(testSDKKey)
id := ldevents.NewDiagnosticID(sharedtest.TestSDKKey)
diagnosticsManager := ldevents.NewDiagnosticsManager(id, ldvalue.Null(), ldvalue.Null(), time.Now(), nil)
context := &internal.ClientContextImpl{
BasicClientContext: subsystems.BasicClientContext{
SDKKey: testSDKKey,
SDKKey: sharedtest.TestSDKKey,
Logging: subsystems.LoggingConfiguration{Loggers: mockLog.Loggers},
},
DiagnosticsManager: diagnosticsManager,
Expand Down Expand Up @@ -424,11 +424,11 @@ func testStreamProcessorRecoverableHTTPError(t *testing.T, statusCode int) {
defer mockLog.DumpIfTestFailed(t)
httphelpers.WithServer(sequentialHandler, func(ts *httptest.Server) {
withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
id := ldevents.NewDiagnosticID(testSDKKey)
id := ldevents.NewDiagnosticID(sharedtest.TestSDKKey)
diagnosticsManager := ldevents.NewDiagnosticsManager(id, ldvalue.Null(), ldvalue.Null(), time.Now(), nil)
context := &internal.ClientContextImpl{
BasicClientContext: subsystems.BasicClientContext{
SDKKey: testSDKKey,
SDKKey: sharedtest.TestSDKKey,
Logging: subsystems.LoggingConfiguration{Loggers: mockLog.Loggers},
},
DiagnosticsManager: diagnosticsManager,
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestStreamProcessorUsesHTTPClientFactory(t *testing.T) {
withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {
httpClientFactory := urlAppendingHTTPClientFactory("/transformed")
httpConfig := subsystems.HTTPConfiguration{CreateHTTPClient: httpClientFactory}
context := sharedtest.NewTestContext(testSDKKey, &httpConfig, nil)
context := sharedtest.NewTestContext(sharedtest.TestSDKKey, &httpConfig, nil)

sp := NewStreamProcessor(context, dataSourceUpdates, StreamConfig{
URI: ts.URL,
Expand Down Expand Up @@ -495,7 +495,7 @@ func TestStreamProcessorDoesNotUseConfiguredTimeoutAsReadTimeout(t *testing.T) {
return &c
}
httpConfig := subsystems.HTTPConfiguration{CreateHTTPClient: httpClientFactory}
context := sharedtest.NewTestContext(testSDKKey, &httpConfig, nil)
context := sharedtest.NewTestContext(sharedtest.TestSDKKey, &httpConfig, nil)

sp := NewStreamProcessor(context, dataSourceUpdates, StreamConfig{URI: ts.URL, InitialReconnectDelay: briefDelay})
defer sp.Close()
Expand All @@ -517,7 +517,7 @@ func TestStreamProcessorRestartsStreamIfStoreNeedsRefresh(t *testing.T) {

httphelpers.WithServer(streamHandler, func(ts *httptest.Server) {
withMockDataSourceUpdates(func(updates *mocks.MockDataSourceUpdates) {
sp := NewStreamProcessor(basicClientContext(), updates, StreamConfig{URI: ts.URL, InitialReconnectDelay: briefDelay})
sp := NewStreamProcessor(sharedtest.BasicClientContext(), updates, StreamConfig{URI: ts.URL, InitialReconnectDelay: briefDelay})
defer sp.Close()

closeWhenReady := make(chan struct{})
Expand All @@ -541,7 +541,7 @@ func TestMalformedStreamBaseURI(t *testing.T) {
defer mockLog.DumpIfTestFailed(t)
clientContext := &internal.ClientContextImpl{
BasicClientContext: subsystems.BasicClientContext{
SDKKey: testSDKKey,
SDKKey: sharedtest.TestSDKKey,
Logging: subsystems.LoggingConfiguration{Loggers: mockLog.Loggers},
},
}
Expand Down Expand Up @@ -570,7 +570,7 @@ func TestStreamProcessorAppendsFilterParameter(t *testing.T) {
httphelpers.WithServer(handler, func(ts *httptest.Server) {
withMockDataSourceUpdates(func(dataSourceUpdates *mocks.MockDataSourceUpdates) {

sp := NewStreamProcessor(basicClientContext(), dataSourceUpdates, StreamConfig{
sp := NewStreamProcessor(sharedtest.BasicClientContext(), dataSourceUpdates, StreamConfig{
URI: ts.URL,
InitialReconnectDelay: briefDelay,
FilterKey: filter.key,
Expand Down
Loading

0 comments on commit 7bea3e3

Please sign in to comment.