Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
friedrichwilken committed Dec 3, 2021
1 parent c86ea41 commit 5c9cce1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions components/eventing-controller/testing/bebmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (m *BEBMock) Start() string {
_ = json.NewDecoder(r.Body).Decode(&subscription)
m.Requests[r] = subscription
key := r.URL.Path + "/" + subscription.Name
m.Subscriptions.PutSubscriptions(key, &subscription)
m.Subscriptions.PutSubscription(key, &subscription)
if m.CreateResponse != nil {
m.CreateResponse(w)
} else {
Expand All @@ -134,7 +134,7 @@ func (m *BEBMock) Start() string {
if m.GetResponse != nil {
m.GetResponse(w, key)
} else {
subscriptionSaved := m.Subscriptions.GetSubscriptions(key)
subscriptionSaved := m.Subscriptions.GetSubscription(key)
if subscriptionSaved != nil {
subscriptionSaved.SubscriptionStatus = bebtypes.SubscriptionStatusActive
w.WriteHeader(http.StatusOK)
Expand Down
28 changes: 14 additions & 14 deletions components/eventing-controller/testing/safesubscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@ import (

// SafeSubscriptions encapsulates Subscriptions to provide mutual exclusion.
type SafeSubscriptions struct {
mutex *sync.RWMutex
sync.RWMutex
subscriptions map[string]*bebtypes.Subscription
}

// NewSafeSubscriptions returns a new SafeSubscriptions.
func NewSafeSubscriptions() *SafeSubscriptions {
return &SafeSubscriptions{
&sync.RWMutex{}, make(map[string]*bebtypes.Subscription),
sync.RWMutex{}, make(map[string]*bebtypes.Subscription),
}
}

// GetSubscriptions returns a Subscription via the corresponding the key.
func (s *SafeSubscriptions) GetSubscriptions(key string) *bebtypes.Subscription {
s.mutex.RLock()
defer s.mutex.RUnlock()
// GetSubscription returns a Subscription via the corresponding the key.
func (s *SafeSubscriptions) GetSubscription(key string) *bebtypes.Subscription {
s.RLock()
defer s.RUnlock()
return s.subscriptions[key]
}

// DeleteSubscription deletes a Subscription via the corresponding key.
func (s *SafeSubscriptions) DeleteSubscription(key string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Lock()
defer s.Unlock()
delete(s.subscriptions, key)
}

// DeleteSubscriptionsByName deletes all Subscriptions that contain the substring 'name' in their own name.
func (s *SafeSubscriptions) DeleteSubscriptionsByName(name string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Lock()
defer s.Unlock()
for k := range s.subscriptions {
if strings.Contains(k, name) {
delete(s.subscriptions, k)
}
}
}

// PutSubscriptions sets a Subscription of SafeSubscriptions via the corresponding key.
func (s *SafeSubscriptions) PutSubscriptions(key string, subscription *bebtypes.Subscription) {
s.mutex.Lock()
defer s.mutex.Unlock()
// PutSubscription adds a Subscription and it's corresponding key to SafeSubscriptions.
func (s *SafeSubscriptions) PutSubscription(key string, subscription *bebtypes.Subscription) {
s.Lock()
defer s.Unlock()
s.subscriptions[key] = subscription
}

0 comments on commit 5c9cce1

Please sign in to comment.