...
1 package mocks
2
3 import (
4 "github.com/launchdarkly/go-server-sdk/v6/subsystems"
5 "github.com/launchdarkly/go-server-sdk/v6/subsystems/ldstoretypes"
6 )
7
8
9
10 type DataSourceFactoryWithData struct {
11 Data []ldstoretypes.Collection
12 }
13
14 func (f DataSourceFactoryWithData) Build(
15 context subsystems.ClientContext,
16 ) (subsystems.DataSource, error) {
17 return &dataSourceWithData{f.Data, context.GetDataSourceUpdateSink(), false}, nil
18 }
19
20 type dataSourceWithData struct {
21 data []ldstoretypes.Collection
22 dataSourceUpdates subsystems.DataSourceUpdateSink
23 inited bool
24 }
25
26 func (d *dataSourceWithData) IsInitialized() bool {
27 return d.inited
28 }
29
30 func (d *dataSourceWithData) Close() error {
31 return nil
32 }
33
34 func (d *dataSourceWithData) Start(closeWhenReady chan<- struct{}) {
35 d.dataSourceUpdates.Init(d.data)
36 d.inited = true
37 close(closeWhenReady)
38 }
39
40
41
42 func DataSourceThatIsAlwaysInitialized() subsystems.ComponentConfigurer[subsystems.DataSource] {
43 return SingleComponentConfigurer[subsystems.DataSource]{Instance: mockDataSource{Initialized: true}}
44 }
45
46
47
48 func DataSourceThatNeverInitializes() subsystems.ComponentConfigurer[subsystems.DataSource] {
49 return SingleComponentConfigurer[subsystems.DataSource]{Instance: mockDataSource{Initialized: false}}
50 }
51
52 type mockDataSource struct {
53 Initialized bool
54 CloseFn func() error
55 StartFn func(chan<- struct{})
56 }
57
58 func (u mockDataSource) IsInitialized() bool {
59 return u.Initialized
60 }
61
62 func (u mockDataSource) Close() error {
63 if u.CloseFn == nil {
64 return nil
65 }
66 return u.CloseFn()
67 }
68
69 func (u mockDataSource) Start(closeWhenReady chan<- struct{}) {
70 if u.StartFn == nil {
71 close(closeWhenReady)
72 } else {
73 u.StartFn(closeWhenReady)
74 }
75 }
76
View as plain text