mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-23 05:57:19 +02:00
pkg/storage: change backend interface to return error (#1131)
Since when storage backend like redis can be fault in many cases, the interface should return error for the caller to handle.
This commit is contained in:
parent
90d95b8c10
commit
aedfbc4c71
7 changed files with 108 additions and 69 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
|
@ -14,12 +15,15 @@ func TestDB(t *testing.T) {
|
|||
ctx := context.Background()
|
||||
db := NewDB("example", 2)
|
||||
t.Run("get missing record", func(t *testing.T) {
|
||||
assert.Nil(t, db.Get(ctx, "abcd"))
|
||||
record, err := db.Get(ctx, "abcd")
|
||||
require.Error(t, err)
|
||||
assert.Nil(t, record)
|
||||
})
|
||||
t.Run("get record", func(t *testing.T) {
|
||||
data := new(anypb.Any)
|
||||
assert.NoError(t, db.Put(ctx, "abcd", data))
|
||||
record := db.Get(ctx, "abcd")
|
||||
record, err := db.Get(ctx, "abcd")
|
||||
require.NoError(t, err)
|
||||
if assert.NotNil(t, record) {
|
||||
assert.NotNil(t, record.CreatedAt)
|
||||
assert.Equal(t, data, record.Data)
|
||||
|
@ -32,21 +36,26 @@ func TestDB(t *testing.T) {
|
|||
})
|
||||
t.Run("delete record", func(t *testing.T) {
|
||||
assert.NoError(t, db.Delete(ctx, "abcd"))
|
||||
record := db.Get(ctx, "abcd")
|
||||
record, err := db.Get(ctx, "abcd")
|
||||
require.NoError(t, err)
|
||||
if assert.NotNil(t, record) {
|
||||
assert.NotNil(t, record.DeletedAt)
|
||||
}
|
||||
})
|
||||
t.Run("clear deleted", func(t *testing.T) {
|
||||
db.ClearDeleted(ctx, time.Now().Add(time.Second))
|
||||
assert.Nil(t, db.Get(ctx, "abcd"))
|
||||
record, err := db.Get(ctx, "abcd")
|
||||
require.Error(t, err)
|
||||
assert.Nil(t, record)
|
||||
})
|
||||
t.Run("keep remaining", func(t *testing.T) {
|
||||
data := new(anypb.Any)
|
||||
assert.NoError(t, db.Put(ctx, "abcd", data))
|
||||
assert.NoError(t, db.Delete(ctx, "abcd"))
|
||||
db.ClearDeleted(ctx, time.Now().Add(-10*time.Second))
|
||||
assert.NotNil(t, db.Get(ctx, "abcd"))
|
||||
record, err := db.Get(ctx, "abcd")
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, record)
|
||||
db.ClearDeleted(ctx, time.Now().Add(time.Second))
|
||||
})
|
||||
t.Run("list", func(t *testing.T) {
|
||||
|
@ -55,8 +64,14 @@ func TestDB(t *testing.T) {
|
|||
assert.NoError(t, db.Put(ctx, fmt.Sprintf("%02d", i), data))
|
||||
}
|
||||
|
||||
assert.Len(t, db.List(ctx, ""), 10)
|
||||
assert.Len(t, db.List(ctx, "00000000000A"), 4)
|
||||
assert.Len(t, db.List(ctx, "00000000000F"), 0)
|
||||
records, err := db.List(ctx, "")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, records, 10)
|
||||
records, err = db.List(ctx, "00000000000A")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, records, 4)
|
||||
records, err = db.List(ctx, "00000000000F")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, records, 0)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue