databroker: add options for maximum capacity (#2095)

* databroker: add options

* implement redis

* add trace for enforce options
This commit is contained in:
Caleb Doxsey 2021-04-26 17:14:54 -06:00 committed by GitHub
parent b3216ae854
commit 636b3d6846
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 1085 additions and 419 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
@ -150,3 +151,32 @@ func TestStream(t *testing.T) {
})
require.NoError(t, eg.Wait())
}
func TestCapacity(t *testing.T) {
ctx := context.Background()
backend := New()
defer func() { _ = backend.Close() }()
err := backend.SetOptions(ctx, "EXAMPLE", &databroker.Options{
Capacity: proto.Uint64(3),
})
require.NoError(t, err)
for i := 0; i < 10; i++ {
err = backend.Put(ctx, &databroker.Record{
Type: "EXAMPLE",
Id: fmt.Sprint(i),
})
require.NoError(t, err)
}
records, _, err := backend.GetAll(ctx)
require.NoError(t, err)
assert.Len(t, records, 3)
var ids []string
for _, r := range records {
ids = append(ids, r.GetId())
}
assert.Equal(t, []string{"7", "8", "9"}, ids, "should contain recent records")
}