...

Source file src/github.com/launchdarkly/go-server-sdk/v6/internal/sharedtest/mocks/mock_data_store.go

Documentation: github.com/launchdarkly/go-server-sdk/v6/internal/sharedtest/mocks

     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  // DataSourceFactoryWithData is a test implementation of ComponentConfigurer that will cause the data
     9  // source to provide a specific set of data when it starts.
    10  type DataSourceFactoryWithData struct {
    11  	Data []ldstoretypes.Collection
    12  }
    13  
    14  func (f DataSourceFactoryWithData) Build( //nolint:revive
    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  // DataSourceThatIsAlwaysInitialized returns a test component factory that produces a data source
    41  // that immediately reports success on startup, although it does not provide any data.
    42  func DataSourceThatIsAlwaysInitialized() subsystems.ComponentConfigurer[subsystems.DataSource] {
    43  	return SingleComponentConfigurer[subsystems.DataSource]{Instance: mockDataSource{Initialized: true}}
    44  }
    45  
    46  // DataSourceThatNeverInitializes returns a test component factory that produces a data source
    47  // that immediately starts up in a failed state and does not provide any data.
    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