Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
pulak-opti committed Nov 24, 2023
1 parent 0ebd517 commit a6eee8b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/handlers/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func RedisNotificationReceiver(conf config.SyncConfig) NotificationReceiverFunc
return nil, errors.New("sdk key not found")
}

redisSyncer, err := syncer.NewSyncedNotificationCenter(ctx, &zerolog.Logger{}, sdkKey, conf)
redisSyncer, err := syncer.NewSyncedNotificationCenter(ctx, sdkKey, conf)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/optimizely/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (

odpCachePkg "github.com/optimizely/go-sdk/pkg/odp/cache"
cmap "github.com/orcaman/concurrent-map"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -252,7 +251,7 @@ func defaultLoader(
}

if agentConf.Synchronization.Notification.Enable {
syncedNC, err := syncer.NewSyncedNotificationCenter(context.Background(), &zerolog.Logger{}, sdkKey, agentConf.Synchronization)
syncedNC, err := syncer.NewSyncedNotificationCenter(context.Background(), sdkKey, agentConf.Synchronization)
if err != nil {
log.Error().Err(err).Msgf("Failed to create SyncedNotificationCenter, reason: %s", err.Error())
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/syncer/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type PubSub interface {
Subscribe(ctx context.Context, channel string) (chan string, error)
}

func NewPubSub(conf config.SyncConfig) (PubSub, error) {
func newPubSub(conf config.SyncConfig) (PubSub, error) {
if conf.Notification.Default == PubSubRedis {
pubsubConf, found := conf.Pubsub[PubSubRedis]
if !found {
Expand Down
2 changes: 1 addition & 1 deletion pkg/syncer/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestNewPubSub(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewPubSub(tt.args.conf)
got, err := newPubSub(tt.args.conf)
if (err != nil) != tt.wantErr {
t.Errorf("NewPubSub() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
16 changes: 13 additions & 3 deletions pkg/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import (
"github.com/optimizely/agent/config"
"github.com/optimizely/go-sdk/pkg/notification"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

const (
LoggerCtxKey = "syncer-logger"
)

var (
Expand Down Expand Up @@ -57,15 +62,20 @@ type Event struct {
Message interface{} `json:"message"`
}

func NewSyncedNotificationCenter(ctx context.Context, logger *zerolog.Logger, sdkKey string, conf config.SyncConfig) (NotificationSyncer, error) {
func NewSyncedNotificationCenter(ctx context.Context, sdkKey string, conf config.SyncConfig) (NotificationSyncer, error) {
mutexLock.Lock()
defer mutexLock.Unlock()

if nc, ok := ncCache[sdkKey]; ok {
return nc, nil
}

pubsub, err := NewPubSub(conf)
logger, ok := ctx.Value(LoggerCtxKey).(*zerolog.Logger)
if !ok {
logger = &log.Logger
}

pubsub, err := newPubSub(conf)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -122,7 +132,7 @@ type DatafileSyncer struct {
}

func NewDatafileSyncer(conf config.SyncConfig) (*DatafileSyncer, error) {
pubsub, err := NewPubSub(conf)
pubsub, err := newPubSub(conf)
if err != nil {
return nil, err
}
Expand Down
115 changes: 115 additions & 0 deletions pkg/syncer/syncer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/****************************************************************************
* Copyright 2023 Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

// Package syncer provides synchronization across Agent nodes
package syncer

import (
"context"
"reflect"
"testing"

"github.com/optimizely/agent/config"
"github.com/rs/zerolog/log"
)

func TestNewSyncedNotificationCenter(t *testing.T) {
type args struct {
ctx context.Context
sdkKey string
conf config.SyncConfig
}
tests := []struct {
name string
args args
want NotificationSyncer
wantErr bool
}{
{
name: "Test with valid config",
args: args{
ctx: context.Background(),
sdkKey: "123",
conf: config.SyncConfig{
Pubsub: map[string]interface{}{
"redis": map[string]interface{}{
"host": "localhost:6379",
"password": "",
"database": 0,
},
},
Notification: config.FeatureSyncConfig{
Default: "redis",
Enable: true,
},
},
},
want: &SyncedNotificationCenter{
ctx: context.Background(),
logger: &log.Logger,
sdkKey: "123",
pubsub: &pubsubRedis{
host: "localhost:6379",
password: "",
database: 0,
},
},
wantErr: false,
},
{
name: "Test with invalid sync config",
args: args{
ctx: context.Background(),
sdkKey: "1234",
conf: config.SyncConfig{
Pubsub: map[string]interface{}{
"not-redis": map[string]interface{}{
"host": "invalid host",
},
},
Notification: config.FeatureSyncConfig{
Default: "redis",
Enable: true,
},
},
},
want: nil,
wantErr: true,
},
{
name: "Test with empty sync config",
args: args{
ctx: context.Background(),
sdkKey: "1234",
conf: config.SyncConfig{},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewSyncedNotificationCenter(tt.args.ctx, tt.args.sdkKey, tt.args.conf)
if (err != nil) != tt.wantErr {
t.Errorf("NewSyncedNotificationCenter() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewSyncedNotificationCenter() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit a6eee8b

Please sign in to comment.