From dc235d6aad9ac82eefbca24e20e286ed3230db70 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 19 Feb 2018 02:42:23 -0600 Subject: [PATCH] add cache test --- app/rest/cache_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 app/rest/cache_test.go diff --git a/app/rest/cache_test.go b/app/rest/cache_test.go new file mode 100644 index 00000000..40328e71 --- /dev/null +++ b/app/rest/cache_test.go @@ -0,0 +1,54 @@ +package rest + +import ( + "context" + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/umputun/remark/app/store" +) + +func TestLoadingCache_Get(t *testing.T) { + var postFnCall, coldCalls int + lc := NewLoadingCache(1*time.Minute, 200*time.Millisecond, func() { + postFnCall++ + }) + + res, err := lc.Get("key", time.Minute, func() ([]byte, error) { + coldCalls++ + return []byte("result"), nil + }) + assert.Nil(t, err) + assert.Equal(t, "result", string(res)) + assert.Equal(t, 1, coldCalls) + assert.Equal(t, 0, postFnCall) + + res, err = lc.Get("key", time.Minute, func() ([]byte, error) { + coldCalls++ + return []byte("result"), nil + }) + assert.Nil(t, err) + assert.Equal(t, "result", string(res)) + assert.Equal(t, 1, coldCalls) + assert.Equal(t, 0, postFnCall) + + lc.Flush() + time.Sleep(100 * time.Millisecond) // let postFn to do its thing + assert.Equal(t, 1, postFnCall) +} + +func TestLoadingCache_URLKey(t *testing.T) { + r, err := http.NewRequest("GET", "http://blah/123", nil) + assert.Nil(t, err) + key := URLKey(r) + assert.Equal(t, "http://blah/123", key) + + ctx := context.Background() + user := store.User{Admin: true} + ctx = context.WithValue(ctx, ContextKey("user"), user) + r = r.WithContext(ctx) + key = URLKey(r) + assert.Equal(t, "admin!!http://blah/123", key) +}