-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test purge functionality test it was added to the cache
- Loading branch information
1 parent
bf0ca6a
commit 154e82b
Showing
1 changed file
with
72 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package groups | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
//"github.com/cactus/go-statsd-client/statsd/statsdtest" | ||
"github.com/datadog/datadog-go/statsd" | ||
//"github.com/cactus/go-statsd-client/statsd" | ||
) | ||
|
||
func TestNotAvailableAfterTTL(t *testing.T) { | ||
// Create a cache with a 10 millisecond TTL | ||
statsdClient, _ := statsd.New("127.0.0.1:8125") | ||
cache := NewLocalCache(time.Millisecond*10, statsdClient, []string{"test_case"}) | ||
|
||
// Create a cache Entry and insert it into the cache | ||
testData := Entry{ | ||
Key: "testkey", | ||
UserGroupData: UserGroupData{ | ||
AllowedGroups: []string{"testGroup"}, | ||
MatchedGroups: []string{"testGroup"}, | ||
}, | ||
} | ||
_, err := cache.Set(testData) | ||
if err != nil { | ||
t.Fatalf("did not expect an error, got %s", err) | ||
} | ||
|
||
// Check the cached entry can be retrieved from the cache. | ||
if data, _ := cache.Get(testData.Key); !reflect.DeepEqual(data, testData) { | ||
t.Errorf("expected data to be '%+v', was '%+v'", testData, data) | ||
} | ||
|
||
// If we wait 10ms (or lets say, 50 for good luck), it will have been removed | ||
time.Sleep(time.Millisecond * 50) | ||
|
||
if _, found := cache.get(testData.Key); found { | ||
t.Errorf("expected key not to be have been found after the TTL expired") | ||
} | ||
} | ||
|
||
func TestNotAvailableAfterPurge(t *testing.T) { | ||
statsdClient, _ := statsd.New("127.0.0.1:8125") | ||
cache := NewLocalCache(time.Duration(10)*time.Second, statsdClient, []string{"test_case"}) | ||
|
||
// Create a cache Entry and insert it into the cache | ||
testData := Entry{ | ||
Key: "testkey", | ||
UserGroupData: UserGroupData{ | ||
AllowedGroups: []string{"testGroup"}, | ||
MatchedGroups: []string{"testGroup"}, | ||
}, | ||
} | ||
_, err := cache.Set(testData) | ||
if err != nil { | ||
t.Fatalf("did not expect an error, got %s", err) | ||
} | ||
|
||
// Check the cached entry can be retrieved from the cache. | ||
if data, _ := cache.Get(testData.Key); !reflect.DeepEqual(data, testData) { | ||
t.Errorf("expected data to be '%+v', was '%+v'", testData, data) | ||
} | ||
|
||
cache.Purge([]string{testData.Key}) | ||
|
||
// Purge should have removed the entry, despite being within the cache TTL | ||
if _, found := cache.get(testData.Key); found { | ||
t.Errorf("expected key not to be have been found after purging") | ||
} | ||
} |