Skip to content

Commit

Permalink
[FSSDK-9632] Add helper function for notification center (#381)
Browse files Browse the repository at this point in the history
* add WithNotificationCenter helper method

* add get notification center method

* run source clear in each PR

* use latest go version for source clear

* add setup action

* fix linter error

* fix linter error

* add unit tests

* remove unnecessary replace directive

* add steps name in CI

* skip gocyclo linter for Client
  • Loading branch information
pulak-opti authored Sep 19, 2023
1 parent f8a4b27 commit c20465b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 9 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/source_clear_cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: Source clear
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
# Runs "weekly"
- cron: '0 0 * * 0'
Expand All @@ -11,8 +13,16 @@ jobs:
source_clear:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: '1.21.0'
check-latest: true
- name: Source clear scan
env:
SRCCLR_API_TOKEN: ${{ secrets.SRCCLR_API_TOKEN }}
run: curl -sSL https://download.sourceclear.com/ci.sh | bash -s - scan
run: |
go mod tidy
curl -sSL https://download.sourceclear.com/ci.sh | bash -s - scan
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cover: ## run unit tests with coverage
GO111MODULE=$(GO111MODULE) $(GOTEST) -race ./pkg/... -coverprofile=profile.cov

install: ## installs dev and ci dependencies
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.52.2
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.54.2

lint: ## runs `golangci-lint` linters defined in `.golangci.yml` file
$(GOLINT) run --out-format=tab --tests=false pkg/...
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// Work around issue with git.apache.org/thrift.git
replace git.apache.org/thrift.git => github.com/apache/thrift v0.12.0
5 changes: 5 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,11 @@ func (o *OptimizelyClient) GetOptimizelyConfig() (optimizelyConfig *config.Optim
return o.ConfigManager.GetOptimizelyConfig()
}

// GetNotificationCenter returns Optimizely Notification Center interface
func (o *OptimizelyClient) GetNotificationCenter() notification.Center {
return o.notificationCenter
}

// Close closes the Optimizely instance and stops any ongoing tasks from its children components.
func (o *OptimizelyClient) Close() {
o.execGroup.TerminateAndWait()
Expand Down
9 changes: 9 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,15 @@ func TestGetOptimizelyConfig(t *testing.T) {
assert.Equal(t, &config.OptimizelyConfig{Revision: "232"}, optimizelyConfig)
}

func TestGetNotificationCenter(t *testing.T) {
nc := &MockNotificationCenter{}
client := OptimizelyClient{
notificationCenter: nc,
}

assert.Equal(t, client.GetNotificationCenter(), nc)
}

func TestGetFeatureDecisionValid(t *testing.T) {
testFeatureKey := "test_feature_key"
testVariableKey := "test_feature_flag_key"
Expand Down
17 changes: 16 additions & 1 deletion pkg/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type OptimizelyFactory struct {
metricsRegistry metrics.Registry
overrideStore decision.ExperimentOverrideStore
userProfileService decision.UserProfileService
notificationCenter notification.Center

// ODP
segmentsCacheSize int
Expand All @@ -62,6 +63,8 @@ type OptimizelyFactory struct {
type OptionFunc func(*OptimizelyFactory)

// Client instantiates a new OptimizelyClient with the given options.
//
//nolint:gocyclo // exceeds gocyclo cyclomatic complexity
func (f *OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClient, error) {
// Default values for odp cache
f.segmentsCacheSize = pkgUtils.DefaultSegmentsCacheSize
Expand Down Expand Up @@ -101,10 +104,15 @@ func (f *OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClie
appClient := &OptimizelyClient{
defaultDecideOptions: decideOptions,
execGroup: eg,
notificationCenter: registry.GetNotificationCenter(f.SDKKey),
logger: logging.GetLogger(f.SDKKey, "OptimizelyClient"),
}

if f.notificationCenter != nil {
appClient.notificationCenter = f.notificationCenter
} else {
appClient.notificationCenter = registry.GetNotificationCenter(f.SDKKey)
}

if f.configManager != nil {
appClient.ConfigManager = f.configManager
} else {
Expand Down Expand Up @@ -285,6 +293,13 @@ func WithMetricsRegistry(metricsRegistry metrics.Registry) OptionFunc {
}
}

// WithNotificationCenter allows user to pass in their own implementation of the notification.Center interface
func WithNotificationCenter(nc notification.Center) OptionFunc {
return func(f *OptimizelyFactory) {
f.notificationCenter = nc
}
}

// StaticClient returns a client initialized with a static project config.
func (f *OptimizelyFactory) StaticClient() (optlyClient *OptimizelyClient, err error) {

Expand Down
8 changes: 8 additions & 0 deletions pkg/client/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ func TestClientWithDefaultSDKSettings(t *testing.T) {
assert.NotNil(t, optimizelyClient.OdpManager)
}

func TestClientWithNotificationCenterInOptions(t *testing.T) {
factory := OptimizelyFactory{SDKKey: "1212"}
nc := &MockNotificationCenter{}
optimizelyClient, err := factory.Client(WithNotificationCenter(nc))
assert.NoError(t, err)
assert.Equal(t, nc, optimizelyClient.notificationCenter)
}

func TestDummy(t *testing.T) {
factory := OptimizelyFactory{}
configManager := config.NewPollingProjectConfigManager("123")
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/datafileprojectconfig/mappers/condition_trees.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func buildConditionTree(conditions interface{}) (conditionTree *entities.TreeNod

parsedConditions, retErr := parseConditions(conditions)
if retErr != nil {
return
return nil, nil, retErr
}
odpSegments = []string{}
value := reflect.ValueOf(parsedConditions)
Expand Down Expand Up @@ -101,7 +101,7 @@ func buildConditionTree(conditions interface{}) (conditionTree *entities.TreeNod
n := &entities.TreeNode{}
if err := createLeafCondition(v, n); err != nil {
retErr = err
return
return nil, nil, retErr
}
// Extract odp segment from leaf node if applicable
extractSegment(&odpSegments, n)
Expand Down

0 comments on commit c20465b

Please sign in to comment.