Add a new integration test framework

I have had this living in a separate branch for now, but we need this in the main branch sooner rather than later.

One big advantage of this is that integration tests can live in any package, not just hugolib.
This commit is contained in:
Bjørn Erik Pedersen 2022-02-09 13:41:04 +01:00
parent 9262719092
commit 64f75adcf6
6 changed files with 477 additions and 1 deletions

View file

@ -16,6 +16,7 @@ package lazy
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
@ -28,6 +29,9 @@ func New() *Init {
// Init holds a graph of lazily initialized dependencies.
type Init struct {
// Used in tests
initCount uint64
mu sync.Mutex
prev *Init
@ -47,6 +51,12 @@ func (ini *Init) Add(initFn func() (interface{}, error)) *Init {
return ini.add(false, initFn)
}
// InitCount gets the number of this this Init has been initialized.
func (ini *Init) InitCount() int {
i := atomic.LoadUint64(&ini.initCount)
return int(i)
}
// AddWithTimeout is same as Add, but with a timeout that aborts initialization.
func (ini *Init) AddWithTimeout(timeout time.Duration, f func(ctx context.Context) (interface{}, error)) *Init {
return ini.Add(func() (interface{}, error) {
@ -77,6 +87,7 @@ func (ini *Init) Do() (interface{}, error) {
}
ini.init.Do(func() {
atomic.AddUint64(&ini.initCount, 1)
prev := ini.prev
if prev != nil {
// A branch. Initialize the ancestors.