Open
Description
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.