-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
- Loading branch information
1 parent
00326c6
commit 3c41fc8
Showing
4 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package service | ||
|
||
import ( | ||
"sync" | ||
|
||
iservice "github.com/open-feature/flagd/core/pkg/service" | ||
) | ||
|
||
// eventingConfiguration is a wrapper for notification subscriptions | ||
type eventingConfiguration struct { | ||
mu *sync.RWMutex | ||
subs map[interface{}]chan iservice.Notification | ||
} | ||
|
||
func (eventing *eventingConfiguration) subscribe(id interface{}, notifyChan chan iservice.Notification) { | ||
eventing.mu.Lock() | ||
defer eventing.mu.Unlock() | ||
|
||
eventing.subs[id] = notifyChan | ||
} | ||
|
||
func (eventing *eventingConfiguration) emitToAll(n iservice.Notification) { | ||
eventing.mu.RLock() | ||
defer eventing.mu.RUnlock() | ||
|
||
for _, send := range eventing.subs { | ||
send <- n | ||
} | ||
} | ||
|
||
func (eventing *eventingConfiguration) unSubscribe(id interface{}) { | ||
eventing.mu.Lock() | ||
defer eventing.mu.Unlock() | ||
|
||
delete(eventing.subs, id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package service | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
iservice "github.com/open-feature/flagd/core/pkg/service" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSubscribe(t *testing.T) { | ||
// given | ||
eventing := &eventingConfiguration{ | ||
subs: make(map[interface{}]chan iservice.Notification), | ||
mu: &sync.RWMutex{}, | ||
} | ||
|
||
idA := "a" | ||
chanA := make(chan iservice.Notification, 1) | ||
|
||
idB := "b" | ||
chanB := make(chan iservice.Notification, 1) | ||
|
||
// when | ||
eventing.subscribe(idA, chanA) | ||
eventing.subscribe(idB, chanB) | ||
|
||
// then | ||
require.Equal(t, chanA, eventing.subs[idA], "incorrect subscription association") | ||
require.Equal(t, chanB, eventing.subs[idB], "incorrect subscription association") | ||
} | ||
|
||
func TestUnsubscribe(t *testing.T) { | ||
// given | ||
eventing := &eventingConfiguration{ | ||
subs: make(map[interface{}]chan iservice.Notification), | ||
mu: &sync.RWMutex{}, | ||
} | ||
|
||
idA := "a" | ||
chanA := make(chan iservice.Notification, 1) | ||
idB := "b" | ||
chanB := make(chan iservice.Notification, 1) | ||
|
||
// when | ||
eventing.subscribe(idA, chanA) | ||
eventing.subscribe(idB, chanB) | ||
|
||
eventing.unSubscribe(idA) | ||
|
||
// then | ||
require.Empty(t, eventing.subs[idA], | ||
"expected subscription cleared, but value present: %v", eventing.subs[idA]) | ||
require.Equal(t, chanB, eventing.subs[idB], "incorrect subscription association") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters