From 16e854a437547db7b4d36f0256856427be259af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 12 May 2026 22:37:51 +0200 Subject: [PATCH] testing: Use synctest where relevant --- cache/filecache/filecache_pruner_test.go | 117 ++++++++++++----------- common/para/para_test.go | 54 ++++++----- 2 files changed, 88 insertions(+), 83 deletions(-) diff --git a/cache/filecache/filecache_pruner_test.go b/cache/filecache/filecache_pruner_test.go index 9cb58e053..117ff3430 100644 --- a/cache/filecache/filecache_pruner_test.go +++ b/cache/filecache/filecache_pruner_test.go @@ -16,6 +16,7 @@ package filecache_test import ( "fmt" "testing" + "testing/synctest" "time" "github.com/gohugoio/hugo/cache/filecache" @@ -27,9 +28,10 @@ import ( func TestPrune(t *testing.T) { t.Parallel() - c := qt.New(t) + synctest.Test(t, func(t *testing.T) { + c := qt.New(t) - configStr := ` + configStr := ` resourceDir = "myresources" contentDir = "content" dataDir = "data" @@ -50,63 +52,64 @@ maxAge = "200ms" dir = ":resourceDir/_gen" ` - for _, name := range []string{filecache.CacheKeyAssets, filecache.CacheKeyImages} { - msg := qt.Commentf("cache: %s", name) - fs := afero.NewMemMapFs() - p := newPathsSpec(t, fs, configStr) - fileCachConfig := p.Cfg.GetConfigSection("caches").(filecache.Configs) - caches, err := filecache.NewCaches(fileCachConfig, fs) - c.Assert(err, qt.IsNil) - caches.SetResourceFs(fs) - cache := caches[name] - for i := range 10 { - id := fmt.Sprintf("i%d", i) - cache.GetOrCreateBytes(id, func() ([]byte, error) { + for _, name := range []string{filecache.CacheKeyAssets, filecache.CacheKeyImages} { + msg := qt.Commentf("cache: %s", name) + fs := afero.NewMemMapFs() + p := newPathsSpec(t, fs, configStr) + fileCachConfig := p.Cfg.GetConfigSection("caches").(filecache.Configs) + caches, err := filecache.NewCaches(fileCachConfig, fs) + c.Assert(err, qt.IsNil) + caches.SetResourceFs(fs) + cache := caches[name] + for i := range 10 { + id := fmt.Sprintf("i%d", i) + cache.GetOrCreateBytes(id, func() ([]byte, error) { + return []byte("abc"), nil + }) + if i == 4 { + // This will expire the first 5 + time.Sleep(201 * time.Millisecond) + } + } + + count, err := caches.Prune() + c.Assert(err, qt.IsNil) + c.Assert(count, qt.Equals, 5, msg) + + for i := range 10 { + id := fmt.Sprintf("i%d", i) + v := cache.GetString(id) + if i < 5 { + c.Assert(v, qt.Equals, "") + } else { + c.Assert(v, qt.Equals, "abc") + } + } + + caches, err = filecache.NewCaches(fileCachConfig, fs) + c.Assert(err, qt.IsNil) + caches.SetResourceFs(fs) + cache = caches[name] + // Touch one and then prune. + cache.GetOrCreateBytes("i5", func() ([]byte, error) { return []byte("abc"), nil }) - if i == 4 { - // This will expire the first 5 - time.Sleep(201 * time.Millisecond) + + count, err = caches.Prune() + c.Assert(err, qt.IsNil) + c.Assert(count, qt.Equals, 4) + + // Now only the i5 should be left. + for i := range 10 { + id := fmt.Sprintf("i%d", i) + v := cache.GetString(id) + if i != 5 { + c.Assert(v, qt.Equals, "") + } else { + c.Assert(v, qt.Equals, "abc") + } } + } - - count, err := caches.Prune() - c.Assert(err, qt.IsNil) - c.Assert(count, qt.Equals, 5, msg) - - for i := range 10 { - id := fmt.Sprintf("i%d", i) - v := cache.GetString(id) - if i < 5 { - c.Assert(v, qt.Equals, "") - } else { - c.Assert(v, qt.Equals, "abc") - } - } - - caches, err = filecache.NewCaches(fileCachConfig, fs) - c.Assert(err, qt.IsNil) - caches.SetResourceFs(fs) - cache = caches[name] - // Touch one and then prune. - cache.GetOrCreateBytes("i5", func() ([]byte, error) { - return []byte("abc"), nil - }) - - count, err = caches.Prune() - c.Assert(err, qt.IsNil) - c.Assert(count, qt.Equals, 4) - - // Now only the i5 should be left. - for i := range 10 { - id := fmt.Sprintf("i%d", i) - v := cache.GetString(id) - if i != 5 { - c.Assert(v, qt.Equals, "") - } else { - c.Assert(v, qt.Equals, "abc") - } - } - - } + }) } diff --git a/common/para/para_test.go b/common/para/para_test.go index 3d28a017d..f80c51fbc 100644 --- a/common/para/para_test.go +++ b/common/para/para_test.go @@ -20,6 +20,7 @@ import ( "sync" "sync/atomic" "testing" + "testing/synctest" "time" "github.com/gohugoio/hugo/htesting" @@ -28,18 +29,16 @@ import ( ) func TestPara(t *testing.T) { - if runtime.NumCPU() < 4 { - t.Skipf("skip para test, CPU count is %d", runtime.NumCPU()) - } - - // TODO(bep) - if htesting.IsCI() { - t.Skip("skip para test when running on CI") - } - c := qt.New(t) c.Run("Order", func(c *qt.C) { + if runtime.NumCPU() < 4 { + c.Skipf("skip Order subtest, CPU count is %d", runtime.NumCPU()) + } + if htesting.IsCI() { + c.Skip("skip Order subtest when running on CI") + } + n := 500 ints := make([]int, n) for i := range n { @@ -68,28 +67,31 @@ func TestPara(t *testing.T) { }) c.Run("Time", func(c *qt.C) { - const n = 100 + synctest.Test(c.TB.(*testing.T), func(t *testing.T) { + c := qt.New(t) + const n = 100 - p := New(5) - r, _ := p.Start(context.Background()) + p := New(5) + r, _ := p.Start(context.Background()) - start := time.Now() + start := time.Now() - var counter int64 + var counter int64 - for range n { - r.Run(func() error { - atomic.AddInt64(&counter, 1) - time.Sleep(1 * time.Millisecond) - return nil - }) - } + for range n { + r.Run(func() error { + atomic.AddInt64(&counter, 1) + time.Sleep(1 * time.Millisecond) + return nil + }) + } - c.Assert(r.Wait(), qt.IsNil) - c.Assert(counter, qt.Equals, int64(n)) + c.Assert(r.Wait(), qt.IsNil) + c.Assert(counter, qt.Equals, int64(n)) - since := time.Since(start) - limit := n / 2 * time.Millisecond - c.Assert(since < limit, qt.Equals, true, qt.Commentf("%s >= %s", since, limit)) + since := time.Since(start) + limit := n / 2 * time.Millisecond + c.Assert(since < limit, qt.Equals, true, qt.Commentf("%s >= %s", since, limit)) + }) }) }