mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-24 14:37:12 +02:00
authorize: use query instead of sync for databroker data (#3377)
This commit is contained in:
parent
fd82cc7870
commit
f61e7efe73
24 changed files with 661 additions and 1008 deletions
73
pkg/storage/cache_test.go
Normal file
73
pkg/storage/cache_test.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLocalCache(t *testing.T) {
|
||||
ctx, clearTimeout := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer clearTimeout()
|
||||
|
||||
callCount := 0
|
||||
update := func(ctx context.Context) ([]byte, error) {
|
||||
callCount++
|
||||
return []byte("v1"), nil
|
||||
}
|
||||
c := NewLocalCache()
|
||||
v, err := c.GetOrUpdate(ctx, []byte("k1"), update)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("v1"), v)
|
||||
assert.Equal(t, 1, callCount)
|
||||
|
||||
v, err = c.GetOrUpdate(ctx, []byte("k1"), update)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("v1"), v)
|
||||
assert.Equal(t, 1, callCount)
|
||||
|
||||
c.Invalidate([]byte("k1"))
|
||||
|
||||
v, err = c.GetOrUpdate(ctx, []byte("k1"), update)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("v1"), v)
|
||||
assert.Equal(t, 2, callCount)
|
||||
}
|
||||
|
||||
func TestGlobalCache(t *testing.T) {
|
||||
ctx, clearTimeout := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer clearTimeout()
|
||||
|
||||
callCount := 0
|
||||
update := func(ctx context.Context) ([]byte, error) {
|
||||
callCount++
|
||||
return []byte("v1"), nil
|
||||
}
|
||||
c := NewGlobalCache(time.Millisecond * 100)
|
||||
v, err := c.GetOrUpdate(ctx, []byte("k1"), update)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("v1"), v)
|
||||
assert.Equal(t, 1, callCount)
|
||||
|
||||
v, err = c.GetOrUpdate(ctx, []byte("k1"), update)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("v1"), v)
|
||||
assert.Equal(t, 1, callCount)
|
||||
|
||||
c.Invalidate([]byte("k1"))
|
||||
|
||||
v, err = c.GetOrUpdate(ctx, []byte("k1"), update)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("v1"), v)
|
||||
assert.Equal(t, 2, callCount)
|
||||
|
||||
assert.Eventually(t, func() bool {
|
||||
_, err := c.GetOrUpdate(ctx, []byte("k1"), func(ctx context.Context) ([]byte, error) {
|
||||
return nil, fmt.Errorf("ERROR")
|
||||
})
|
||||
return err != nil
|
||||
}, time.Second, time.Millisecond*10, "should honor TTL")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue