File tree Expand file tree Collapse file tree 2 files changed +19
-6
lines changed Expand file tree Collapse file tree 2 files changed +19
-6
lines changed Original file line number Diff line number Diff line change 18
18
package registry
19
19
20
20
import (
21
+ "sync"
22
+
21
23
"github.com/optimizely/go-sdk/pkg/notification"
22
24
)
23
25
24
26
var notificationCenterCache = make (map [string ]notification.Center )
27
+ var notificationLock sync.Mutex
25
28
26
29
// GetNotificationCenter returns the notification center instance associated with the given SDK Key or creates a new one if not found
27
30
func GetNotificationCenter (sdkKey string ) notification.Center {
28
- var notificationCenter notification.Center
29
- var ok bool
30
- if notificationCenter , ok = notificationCenterCache [sdkKey ]; ! ok {
31
+ notificationLock .Lock ()
32
+ defer notificationLock .Unlock ()
33
+
34
+ notificationCenter , ok := notificationCenterCache [sdkKey ]
35
+ if ! ok {
31
36
notificationCenter = notification .NewNotificationCenter ()
32
37
notificationCenterCache [sdkKey ] = notificationCenter
33
38
}
Original file line number Diff line number Diff line change @@ -28,11 +28,19 @@ type ServiceRegistryTestSuite struct {
28
28
29
29
func (s * ServiceRegistryTestSuite ) TestGetNotificationCenter () {
30
30
// empty state, make sure we get a new notification center
31
- sdkKey := "sdk_key_1"
32
- notificationCenter := GetNotificationCenter (sdkKey )
31
+ sdkKey1 := "sdk_key_1"
32
+ sdkKey2 := "sdk_key_2"
33
+ notificationCenter := GetNotificationCenter (sdkKey1 )
33
34
s .NotNil (notificationCenter )
34
35
35
- notificationCenter2 := GetNotificationCenter (sdkKey )
36
+ notificationCenter2 := GetNotificationCenter (sdkKey1 )
37
+ s .Equal (notificationCenter , notificationCenter2 )
38
+
39
+ // make sure sdkKey2 does not cause race condition
40
+ notificationCenter = GetNotificationCenter (sdkKey2 )
41
+ s .NotNil (notificationCenter )
42
+
43
+ notificationCenter2 = GetNotificationCenter (sdkKey2 )
36
44
s .Equal (notificationCenter , notificationCenter2 )
37
45
}
38
46
You can’t perform that action at this time.
0 commit comments