fix: api handlers. add tests
This commit is contained in:
2
pkg/cache/cache.go
vendored
2
pkg/cache/cache.go
vendored
@@ -139,7 +139,7 @@ func (c *Cache[K, V]) ContainsOrAdd(ctx context.Context, key K, value V) (ok, ev
|
||||
// PeekOrAdd checks if a key is in the cache without updating the
|
||||
// recent-ness or deleting it for being stale, and if not, adds the value.
|
||||
// Returns whether found and whether an eviction occurred.
|
||||
func (c *Cache[K, V]) PeekOrAdd(ctx context.Context, key K, value V) (previous interface{}, ok, evicted bool) {
|
||||
func (c *Cache[K, V]) PeekOrAdd(ctx context.Context, key K, value V) (previous *V, ok, evicted bool) {
|
||||
var k K
|
||||
var v V
|
||||
c.lock.Lock()
|
||||
|
||||
131
pkg/cache/cache_test.go
vendored
Normal file
131
pkg/cache/cache_test.go
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
package cache_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/matryer/is"
|
||||
"github.com/sour-is/ev/pkg/cache"
|
||||
)
|
||||
|
||||
func TestCache(t *testing.T) {
|
||||
is := is.New(t)
|
||||
ctx := context.Background()
|
||||
|
||||
c, err := cache.NewCache[string, int](1)
|
||||
is.NoErr(err)
|
||||
|
||||
evicted := c.Add(ctx, "one", 1)
|
||||
is.True(!evicted)
|
||||
|
||||
is.True(c.Contains("one"))
|
||||
_, ok := c.Peek("one")
|
||||
is.True(ok)
|
||||
|
||||
ok, evicted = c.ContainsOrAdd(ctx, "two", 2)
|
||||
is.True(!ok)
|
||||
is.True(evicted)
|
||||
|
||||
is.True(!c.Contains("one"))
|
||||
is.True(c.Contains("two"))
|
||||
|
||||
is.Equal(c.Len(), 1)
|
||||
is.Equal(c.Keys(), []string{"two"})
|
||||
|
||||
v, ok := c.Get("two")
|
||||
is.True(ok)
|
||||
is.Equal(*v, 2)
|
||||
|
||||
evictCount := c.Resize(ctx, 100)
|
||||
is.True(evictCount == 0)
|
||||
|
||||
c.Add(ctx, "one", 1)
|
||||
|
||||
prev, ok, evicted := c.PeekOrAdd(ctx, "three", 3)
|
||||
is.True(!ok)
|
||||
is.True(!evicted)
|
||||
is.Equal(prev, nil)
|
||||
|
||||
key, value, ok := c.GetOldest()
|
||||
is.True(ok)
|
||||
is.Equal(*key, "two")
|
||||
is.Equal(*value, 2)
|
||||
|
||||
key, value, ok = c.RemoveOldest(ctx)
|
||||
is.True(ok)
|
||||
is.Equal(*key, "two")
|
||||
is.Equal(*value, 2)
|
||||
|
||||
c.Remove(ctx, "one")
|
||||
|
||||
c.Purge(ctx)
|
||||
is.True(!c.Contains("three"))
|
||||
}
|
||||
|
||||
func TestCacheWithEvict(t *testing.T) {
|
||||
is := is.New(t)
|
||||
ctx := context.Background()
|
||||
|
||||
evictions := 0
|
||||
|
||||
c, err := cache.NewWithEvict(1, func(ctx context.Context, s string, i int) { evictions++ })
|
||||
is.NoErr(err)
|
||||
|
||||
key, value, ok := c.GetOldest()
|
||||
is.True(!ok)
|
||||
is.Equal(key, nil)
|
||||
is.Equal(value, nil)
|
||||
|
||||
key, value, ok = c.RemoveOldest(ctx)
|
||||
is.True(!ok)
|
||||
is.Equal(key, nil)
|
||||
is.Equal(value, nil)
|
||||
|
||||
evicted := c.Add(ctx, "one", 1)
|
||||
is.True(!evicted)
|
||||
|
||||
is.True(c.Contains("one"))
|
||||
_, ok = c.Peek("one")
|
||||
is.True(ok)
|
||||
|
||||
ok, evicted = c.ContainsOrAdd(ctx, "two", 2)
|
||||
is.True(!ok)
|
||||
is.True(evicted)
|
||||
|
||||
is.True(!c.Contains("one"))
|
||||
is.True(c.Contains("two"))
|
||||
|
||||
is.Equal(c.Len(), 1)
|
||||
is.Equal(c.Keys(), []string{"two"})
|
||||
|
||||
v, ok := c.Get("two")
|
||||
is.True(ok)
|
||||
is.Equal(*v, 2)
|
||||
|
||||
evictCount := c.Resize(ctx, 100)
|
||||
is.True(evictCount == 0)
|
||||
|
||||
c.Add(ctx, "one", 1)
|
||||
|
||||
prev, ok, evicted := c.PeekOrAdd(ctx, "three", 3)
|
||||
is.True(!ok)
|
||||
is.True(!evicted)
|
||||
is.Equal(prev, nil)
|
||||
|
||||
key, value, ok = c.GetOldest()
|
||||
is.True(ok)
|
||||
is.Equal(*key, "two")
|
||||
is.Equal(*value, 2)
|
||||
|
||||
key, value, ok = c.RemoveOldest(ctx)
|
||||
is.True(ok)
|
||||
is.Equal(*key, "two")
|
||||
is.Equal(*value, 2)
|
||||
|
||||
c.Resize(ctx, 1)
|
||||
|
||||
c.Purge(ctx)
|
||||
is.True(!c.Contains("three"))
|
||||
|
||||
is.Equal(evictions, 4)
|
||||
}
|
||||
29
pkg/es/driver/projecter/projector_test.go
Normal file
29
pkg/es/driver/projecter/projector_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package projecter_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/sour-is/ev/pkg/es/driver"
|
||||
)
|
||||
|
||||
type mockDriver struct {
|
||||
onOpen func()
|
||||
onEventLog func()
|
||||
}
|
||||
|
||||
// EventLog implements driver.Driver
|
||||
func (*mockDriver) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// Open implements driver.Driver
|
||||
func (*mockDriver) Open(ctx context.Context, dsn string) (driver.Driver, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
var _ driver.Driver = (*mockDriver)(nil)
|
||||
|
||||
func TestProjecter(t *testing.T) {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user