forked from flyteorg/flytestdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support DeleteDelayed operation in AutoRefresh cache (flyteorg#98)
Signed-off-by: Haytham Abuelfutuh <[email protected]>
- Loading branch information
Showing
3 changed files
with
109 additions
and
1 deletion.
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,40 @@ | ||
package cache | ||
|
||
import "sync" | ||
|
||
var emptyVal = struct{}{} | ||
|
||
// syncSet is a thread-safe Set. | ||
type syncSet struct { | ||
underlying sync.Map | ||
} | ||
|
||
// Contains checks if the key is present in the set. | ||
func (s *syncSet) Contains(key interface{}) bool { | ||
_, found := s.underlying.Load(key) | ||
return found | ||
} | ||
|
||
// Insert adds a new key to the set if it doesn't already exist. | ||
func (s *syncSet) Insert(key interface{}) { | ||
s.underlying.Store(key, emptyVal) | ||
} | ||
|
||
// Remove deletes a key from the set. | ||
func (s *syncSet) Remove(key interface{}) { | ||
s.underlying.Delete(key) | ||
} | ||
|
||
// Range allows iterating over the set. Deleting the key while iterating is a supported operation. | ||
func (s *syncSet) Range(callback func(key interface{}) bool) { | ||
s.underlying.Range(func(key, value interface{}) bool { | ||
return callback(key) | ||
}) | ||
} | ||
|
||
// newSyncSet initializes a new thread-safe set. | ||
func newSyncSet() *syncSet { | ||
return &syncSet{ | ||
underlying: sync.Map{}, | ||
} | ||
} |
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,49 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func rangeAndRemove(tb testing.TB, s *syncSet, count int) { | ||
for i := 0; i < count; i++ { | ||
s.Insert(i) | ||
} | ||
|
||
s.Range(func(key interface{}) bool { | ||
s.Remove(key) | ||
return true | ||
}) | ||
|
||
for i := 0; i < count; i++ { | ||
assert.False(tb, s.Contains(i)) | ||
} | ||
} | ||
|
||
func TestSyncSet_Range(t *testing.T) { | ||
s := newSyncSet() | ||
rangeAndRemove(t, s, 1000) | ||
} | ||
|
||
func BenchmarkSyncSet_Range(b *testing.B) { | ||
s := newSyncSet() | ||
rangeAndRemove(b, s, b.N) | ||
} | ||
|
||
func TestSyncSet_Contains(t *testing.T) { | ||
s := newSyncSet() | ||
count := 1000 | ||
for i := 0; i < count; i++ { | ||
s.Insert(i) | ||
} | ||
|
||
for i := 0; i < count; i++ { | ||
assert.True(t, s.Contains(i)) | ||
s.Remove(i) | ||
} | ||
|
||
for i := 0; i < count; i++ { | ||
assert.False(t, s.Contains(i)) | ||
} | ||
} |