Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add tests for polling initialization #268

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions sdktests/common_tests_stream_fdv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (c CommonStreamingTests) FDv2(t *ldtest.T) {

func (c CommonStreamingTests) StateTransitions(t *ldtest.T) {
t.Run("initializes from an empty state", c.InitializeFromEmptyState)
t.Run("initializes from polling initializer", c.InitializeFromPollingInitializer)
t.Run("initializes from polling initializer + streaming updates",
c.InitializeFromPollingInitializerWithStreamingUpdates)
t.Run("initializes from 2 polling initializers", c.InitializeFromTwoPollingInitializers)
t.Run("saves previously known state", c.SavesPreviouslyKnownState)
t.Run("replaces previously known state", c.ReplacesPreviouslyKnownState)
t.Run("updates previously known state", c.UpdatesPreviouslyKnownState)
Expand All @@ -51,6 +55,70 @@ func (c CommonStreamingTests) InitializeFromEmptyState(t *ldtest.T) {
validatePayloadReceived(t, dataSystem.PrimarySync().endpoint, client, "", expectedEvaluations)
}

func (c CommonStreamingTests) InitializeFromPollingInitializer(t *ldtest.T) {
dataBefore := mockld.NewServerSDKDataBuilder().Flag(c.makeServerSideFlag("flag-key", 1, initialValue)).Build()
dataAfter := mockld.NewServerSDKDataBuilder().IntentCode("xfer-none").IntentReason("up-to-date").Build()
dataSystem := NewSDKDataSystem(t, dataAfter, DataSystemOptionPollingInitializer(dataBefore))

client := NewSDKClient(t, dataSystem)

_, err := dataSystem.Initializers[0].Endpoint().AwaitConnection(time.Second)
require.NoError(t, err)

expectedEvaluations := map[string]ldvalue.Value{"flag-key": initialValue}
validatePayloadReceived(t, dataSystem.PrimarySync().Endpoint(), client, "initial", expectedEvaluations)
}

func (c CommonStreamingTests) InitializeFromPollingInitializerWithStreamingUpdates(t *ldtest.T) {
dataBefore := mockld.NewServerSDKDataBuilder().Flag(c.makeServerSideFlag("flag-key", 1, initialValue)).Build()
dataAfter := mockld.NewServerSDKDataBuilder().
IntentCode("xfer-changes").
IntentReason("stale").
Flag(c.makeServerSideFlag("flag-key", 2, updatedValue)).
Flag(c.makeServerSideFlag("new-flag-key", 1, newInitialValue)).
Build()
dataSystem := NewSDKDataSystem(t, dataBefore, DataSystemOptionPollingInitializer(dataBefore))
dataSystem.PrimarySync().streaming.SetInitialData(dataAfter)

client := NewSDKClient(t, dataSystem)

_, err := dataSystem.Initializers[0].Endpoint().AwaitConnection(time.Second)
require.NoError(t, err)

expectedEvaluations := map[string]ldvalue.Value{"flag-key": initialValue, "new-flag-key": newInitialValue}
validatePayloadReceived(t, dataSystem.PrimarySync().Endpoint(), client, "initial", expectedEvaluations)
}

func (c CommonStreamingTests) InitializeFromTwoPollingInitializers(t *ldtest.T) {
emptyPayload := mockld.NewServerSDKDataBuilder().
Build()
initialStatefulData := mockld.NewServerSDKDataBuilder().
IntentCode("xfer-full").
IntentReason("payload-missing").
State("expected-state").
Flag(c.makeServerSideFlag("flag-key", 2, updatedValue)).
Build()
streamingData := mockld.NewServerSDKDataBuilder().
IntentCode("none").
IntentReason("up-to-date").
State("expected-state").
Build()
dataSystem := NewSDKDataSystem(t, streamingData,
DataSystemOptionPollingInitializer(emptyPayload), DataSystemOptionPollingInitializer(initialStatefulData))

// Force the first endpoint to fail
dataSystem.Initializers[0].Endpoint().Close()

client := NewSDKClient(t, dataSystem)

// Verify the initializers fall over to the next initializer in line.
_, err := dataSystem.Initializers[1].Endpoint().AwaitConnection(time.Second)
require.NoError(t, err)

expectedEvaluations := map[string]ldvalue.Value{"flag-key": updatedValue}
validatePayloadReceived(t, dataSystem.PrimarySync().Endpoint(), client, "expected-state", expectedEvaluations)
}

func (c CommonStreamingTests) SavesPreviouslyKnownState(t *ldtest.T) {
dataBefore := c.makeSDKDataWithFlag(1, initialValue)
dataAfter := mockld.NewServerSDKDataBuilder().IntentCode("xfer-none").IntentReason("up-to-date").Build()
Expand Down
36 changes: 34 additions & 2 deletions sdktests/testapi_sdk_data_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ import (
)

type sdkDataSystemConfig struct {
polling o.Maybe[bool] // true, false, or "undefined, use the default"
polling o.Maybe[bool] // true, false, or "undefined, use the default"
pollingInitializers []mockld.FDv2SDKData
}

// SDKDataSystemOption is the interface for options to NewSDKDataSystem.
type SDKDataSystemOption helpers.ConfigOption[sdkDataSystemConfig]

// DataSystemOptionPollingInitializer adds support for a polling initializer
func DataSystemOptionPollingInitializer(data mockld.FDv2SDKData) SDKDataSystemOption {
return helpers.ConfigOptionFunc[sdkDataSystemConfig](func(c *sdkDataSystemConfig) error {
c.pollingInitializers = append(c.pollingInitializers, data)
return nil
})
}

// DataSystemOptionPolling makes an SDKDataSystem simulate the polling service.
func DataSystemOptionPolling() SDKDataSystemOption {
return helpers.ConfigOptionFunc[sdkDataSystemConfig](func(c *sdkDataSystemConfig) error {
Expand Down Expand Up @@ -212,6 +221,20 @@ func NewSDKDataSystem(
t *ldtest.T, data mockld.SDKData, options ...SDKDataSystemOption) *SDKDataSystem {
dataSystem := NewSDKDataSystemWithoutEndpoints(t, data, options...)

if dataSystem.Initializers != nil {
for i, initializer := range dataSystem.Initializers {
if initializer.pollingService == nil {
continue
}

initializer.endpoint =
requireContext(t).harness.NewMockEndpoint(initializer.pollingService, t.DebugLogger(),
harness.MockEndpointDescription("polling initializer"))

dataSystem.Initializers[i] = initializer
}
}

if dataSystem.Synchronizers != nil {
isPolling := dataSystem.PrimarySync().polling != nil
handler := helpers.IfElse[http.Handler](isPolling,
Expand Down Expand Up @@ -251,8 +274,17 @@ func NewSDKDataSystemWithoutEndpoints(
var config sdkDataSystemConfig
_ = helpers.ApplyOptions(&config, options...)

defaultIsPolling := sdkKind == mockld.JSClientSDK || sdkKind == mockld.PHPSDK
d := &SDKDataSystem{}
d.t = t

for _, initializer := range config.pollingInitializers {
d.Initializers = append(d.Initializers, DataInitializer{
pollingService: mockld.NewPollingService(initializer, sdkKind, t.DebugLogger()).
WithGzipCompression(t.Capabilities().Has(servicedef.CapabilityPollingGzip)),
})
}

defaultIsPolling := sdkKind == mockld.JSClientSDK || sdkKind == mockld.PHPSDK
if config.polling.Value() || (!config.polling.IsDefined() && defaultIsPolling) {
d.Synchronizers = &Synchronizers{
primary: Synchronizer{
Expand Down
Loading