mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-12 21:32:10 +00:00
Add test for indexer with multiple values
Kubernetes-commit: 56159f258ca380600b0bc08b2e99cbc745db3560
This commit is contained in:
committed by
Kubernetes Publisher
parent
b1e85f6f00
commit
1cab68940f
73
tools/cache/thread_safe_store_test.go
vendored
73
tools/cache/thread_safe_store_test.go
vendored
@@ -18,7 +18,11 @@ package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestThreadSafeStoreDeleteRemovesEmptySetsFromIndex(t *testing.T) {
|
||||
@@ -92,6 +96,75 @@ func TestThreadSafeStoreAddKeepsNonEmptySetPostDeleteFromIndex(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestThreadSafeStoreIndexingFunctionsWithMultipleValues(t *testing.T) {
|
||||
testIndexer := "testIndexer"
|
||||
|
||||
indexers := Indexers{
|
||||
testIndexer: func(obj interface{}) ([]string, error) {
|
||||
return strings.Split(obj.(string), ","), nil
|
||||
},
|
||||
}
|
||||
|
||||
indices := Indices{}
|
||||
store := NewThreadSafeStore(indexers, indices).(*threadSafeMap)
|
||||
|
||||
store.Add("key1", "foo")
|
||||
store.Add("key2", "bar")
|
||||
|
||||
assert := assert.New(t)
|
||||
|
||||
compare := func(key string, expected []string) error {
|
||||
values := store.indices[testIndexer][key].List()
|
||||
if cmp.Equal(values, expected) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unexpected index for key %s, diff=%s", key, cmp.Diff(values, expected))
|
||||
}
|
||||
|
||||
assert.NoError(compare("foo", []string{"key1"}))
|
||||
assert.NoError(compare("bar", []string{"key2"}))
|
||||
|
||||
store.Update("key2", "foo,bar")
|
||||
|
||||
assert.NoError(compare("foo", []string{"key1", "key2"}))
|
||||
assert.NoError(compare("bar", []string{"key2"}))
|
||||
|
||||
store.Update("key1", "foo,bar")
|
||||
|
||||
assert.NoError(compare("foo", []string{"key1", "key2"}))
|
||||
assert.NoError(compare("bar", []string{"key1", "key2"}))
|
||||
|
||||
store.Add("key3", "foo,bar,baz")
|
||||
|
||||
assert.NoError(compare("foo", []string{"key1", "key2", "key3"}))
|
||||
assert.NoError(compare("bar", []string{"key1", "key2", "key3"}))
|
||||
assert.NoError(compare("baz", []string{"key3"}))
|
||||
|
||||
store.Update("key1", "foo")
|
||||
|
||||
assert.NoError(compare("foo", []string{"key1", "key2", "key3"}))
|
||||
assert.NoError(compare("bar", []string{"key2", "key3"}))
|
||||
assert.NoError(compare("baz", []string{"key3"}))
|
||||
|
||||
store.Update("key2", "bar")
|
||||
|
||||
assert.NoError(compare("foo", []string{"key1", "key3"}))
|
||||
assert.NoError(compare("bar", []string{"key2", "key3"}))
|
||||
assert.NoError(compare("baz", []string{"key3"}))
|
||||
|
||||
store.Delete("key1")
|
||||
|
||||
assert.NoError(compare("foo", []string{"key3"}))
|
||||
assert.NoError(compare("bar", []string{"key2", "key3"}))
|
||||
assert.NoError(compare("baz", []string{"key3"}))
|
||||
|
||||
store.Delete("key3")
|
||||
|
||||
assert.NoError(compare("foo", []string{}))
|
||||
assert.NoError(compare("bar", []string{"key2"}))
|
||||
assert.NoError(compare("baz", []string{}))
|
||||
}
|
||||
|
||||
func BenchmarkIndexer(b *testing.B) {
|
||||
testIndexer := "testIndexer"
|
||||
|
||||
|
Reference in New Issue
Block a user