Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c9cce1
commit 24252ad
Showing
4 changed files
with
152 additions
and
83 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
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,64 @@ | ||
package testing | ||
|
||
import ( | ||
"github.com/kyma-project/kyma/components/eventing-controller/pkg/ems/api/events/types" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
// SafeRequests encapsulates Requests to provide mutual exclusion. | ||
type SafeRequests struct { | ||
sync.RWMutex | ||
requests map[*http.Request]interface{} | ||
} | ||
|
||
// NewSafeRequests returns a new SafeRequests. | ||
func NewSafeRequests() *SafeRequests { | ||
return &SafeRequests{ | ||
sync.RWMutex{}, | ||
make(map[*http.Request]interface{}), | ||
} | ||
} | ||
|
||
// StoreRequest adds a request to requests and sets it's corresponding subscription to nil. | ||
func (r *SafeRequests) StoreRequest(request *http.Request) { | ||
r.Lock() | ||
defer r.Unlock() | ||
r.requests[request] = nil | ||
} | ||
|
||
// PutSubscription sets a the subscription of a request. | ||
func (r *SafeRequests) PutSubscription(request *http.Request, subscription types.Subscription) { | ||
r.Lock() | ||
defer r.Unlock() | ||
r.requests[request] = subscription | ||
} | ||
|
||
// Len returns the length of requests. | ||
func (r *SafeRequests) Len() int { | ||
r.RLock() | ||
defer r.RUnlock() | ||
return len(r.requests) | ||
} | ||
|
||
// CheckIfAny iterates over requests and checks if a given func f is true at any iteration's request and payload. | ||
// CheckIfAny is only read-save; f must be a read-only operation. | ||
func (r *SafeRequests) CheckIfAny(f func(request *http.Request, payload interface{}) bool) bool { | ||
r.RLock() | ||
defer r.RUnlock() | ||
for req, payload := range r.requests { | ||
if f(req, payload) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// ReadEach iterates over requests and executes a given func f with the iteration's request and payload. | ||
func (r *SafeRequests) ReadEach(f func(request *http.Request, payload interface{})) { | ||
r.RLock() | ||
defer r.RUnlock() | ||
for req, payload := range r.requests { | ||
f(req, payload) | ||
} | ||
} |
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