Skip to content

Commit

Permalink
localcache tests
Browse files Browse the repository at this point in the history
test purge functionality

test it was added to the cache
  • Loading branch information
Jusshersmith committed Apr 30, 2019
1 parent bf0ca6a commit 154e82b
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions internal/pkg/groups/localcache_test.go
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")
}
}

0 comments on commit 154e82b

Please sign in to comment.