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

Cannot test FlagTracker using a file data source #156

Open
constant-mihai opened this issue Jun 17, 2024 · 1 comment
Open

Cannot test FlagTracker using a file data source #156

constant-mihai opened this issue Jun 17, 2024 · 1 comment
Labels
bug Something isn't working server-sdk Issues affecting the Go server SDK

Comments

@constant-mihai
Copy link

Is this a support request?
No.

Describe the bug
I'm trying to write unit tests for some code which uses FlagTracker.
I have successfully used test data sources for testing flag queries but cannot seem to do so for the flag listener, nor for the flag value listener.
After reading the docs for the FlagTracker interface, I've learned that:

	// Change events only work if the SDK is actually connecting to LaunchDarkly (or using the file data source).
	// If the SDK is only reading flags from a database (ldcomponents.ExternalUpdatesOnly) then it cannot
	// know when there is a change, because flags are read on an as-needed basis.

File data sources do not seem to work either.

To reproduce
Run the following program:

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"

	"gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
	sdk "gopkg.in/launchdarkly/go-server-sdk.v5"
	"gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"
	"gopkg.in/launchdarkly/go-server-sdk.v5/ldfiledata"
	"gopkg.in/launchdarkly/go-server-sdk.v5/ldfilewatch"
)

func main() {
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	ldClient, _ := sdk.MakeCustomClient("sdk-key-123abc",
		sdk.Config{
			Events: ldcomponents.NoEvents(),
			DataSource: ldfiledata.DataSource().
				FilePaths("./ld-simple.yaml").
				Reloader(ldfilewatch.WatchFiles),
		}, 0)

	// Not sure how MakeCustomClient works, so I'm putting a sleep here.
	time.Sleep(3 * time.Second)
	context := lduser.NewUserBuilder("user-key-1").Build()
	optIn, err := ldClient.BoolVariation("opt-in", context, false)
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		return
	}
	fmt.Printf("opt-in: %v\n", optIn)

	updateCh := ldClient.GetFlagTracker().AddFlagChangeListener()
	defer func() {
		ldClient.GetFlagTracker().RemoveFlagChangeListener(updateCh)
	}()

	for {
		select {
		case <-sigs:
			return
		case event, ok := <-updateCh:
			if !ok {
				// channel closed
				return
			}
			fmt.Printf("event: %v\n", event)
		}
	}
}

ld-simple.yaml

flagValues:
  opt-in: true

Program prints:

[LaunchDarkly] 2024/06/17 10:25:33 INFO: Starting LaunchDarkly client 5.10.1
[LaunchDarkly] 2024/06/17 10:25:33 INFO: FileDataSource: Reloading flag data after detecting a change
opt-in: true

Expected behavior
Expected the program to print:

[LaunchDarkly] 2024/06/17 10:25:33 INFO: Starting LaunchDarkly client 5.10.1
[LaunchDarkly] 2024/06/17 10:25:33 INFO: FileDataSource: Reloading flag data after detecting a change
opt-in: true
event: <some event value>

Logs
See above.

SDK version
5.10.1

Language version, developer tools
go 1.22.1

OS/platform
Ubuntu 22.04

Additional context
N/A.

@constant-mihai constant-mihai added bug Something isn't working server-sdk Issues affecting the Go server SDK labels Jun 17, 2024
@cwaldren-ld
Copy link
Contributor

cwaldren-ld commented Jun 17, 2024

Hi @constant-mihai , I will take a look at this.

As I mentioned on your other issue, the 5.10 version went end-of-life on December 7th of 2023, and is not supported by LaunchDarkly. If this turns out to need a bugfix, the fix would not be backported to v5.

Filed internally as 247611.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working server-sdk Issues affecting the Go server SDK
Projects
None yet
Development

No branches or pull requests

2 participants