chore: add files cache for reading template tar archives from db (#17141)

This commit is contained in:
ケイラ
2025-04-02 15:42:16 -07:00
committed by GitHub
parent c06294235f
commit ac7ea08873
5 changed files with 308 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
package lazy
type ValueWithError[T any] struct {
inner Value[result[T]]
}
type result[T any] struct {
value T
err error
}
// NewWithError allows you to provide a lazy initializer that can fail.
func NewWithError[T any](fn func() (T, error)) *ValueWithError[T] {
return &ValueWithError[T]{
inner: Value[result[T]]{fn: func() result[T] {
value, err := fn()
return result[T]{value: value, err: err}
}},
}
}
func (v *ValueWithError[T]) Load() (T, error) {
result := v.inner.Load()
return result.value, result.err
}
+52
View File
@@ -0,0 +1,52 @@
package lazy_test
import (
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/util/lazy"
)
func TestLazyWithErrorOK(t *testing.T) {
t.Parallel()
l := lazy.NewWithError(func() (int, error) {
return 1, nil
})
i, err := l.Load()
require.NoError(t, err)
require.Equal(t, 1, i)
}
func TestLazyWithErrorErr(t *testing.T) {
t.Parallel()
l := lazy.NewWithError(func() (int, error) {
return 0, xerrors.New("oh no! everything that could went horribly wrong!")
})
i, err := l.Load()
require.Error(t, err)
require.Equal(t, 0, i)
}
func TestLazyWithErrorPointers(t *testing.T) {
t.Parallel()
a := 1
l := lazy.NewWithError(func() (*int, error) {
return &a, nil
})
b, err := l.Load()
require.NoError(t, err)
c, err := l.Load()
require.NoError(t, err)
*b++
*c++
require.Equal(t, 3, a)
}