aboutsummaryrefslogtreecommitdiffhomepage
path: root/lazy
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2022-02-09 13:41:04 +0100
committerBjørn Erik Pedersen <[email protected]>2022-02-09 15:41:32 +0100
commit64f75adcf6cf44fafeea7e4d00d8d05d951931e6 (patch)
tree4681023c300b7ea309301fc1f34db3a207c60f47 /lazy
parent9262719092d6ec8e034c7a097575defe8611dadb (diff)
downloadhugo-64f75adcf6cf44fafeea7e4d00d8d05d951931e6.tar.gz
hugo-64f75adcf6cf44fafeea7e4d00d8d05d951931e6.zip
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.
Diffstat (limited to 'lazy')
-rw-r--r--lazy/init.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/lazy/init.go b/lazy/init.go
index 6dff0c98c..fc64b2a7d 100644
--- a/lazy/init.go
+++ b/lazy/init.go
@@ -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.