Skip to content

Commit dc56970

Browse files
committed
fix: Trying to migrate some tests to use synctest
1 parent 7d28255 commit dc56970

2 files changed

Lines changed: 37 additions & 33 deletions

File tree

internal/cas/store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestStore_LockConcurrency(t *testing.T) {
148148
acquired <- true
149149

150150
// Hold lock briefly.
151-
//nolint:synctestcheck // TODO migrate to synctest
151+
//nolint:synctestcheck // sync.Mutex.Lock is not durably blocking under synctest, so a bubble would deadlock here
152152
time.Sleep(100 * time.Millisecond)
153153

154154
err = lock.Unlock()

internal/remotestate/backend/s3/counting_semaphore_test.go

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77
"sync/atomic"
88
"testing"
9+
"testing/synctest"
910
"time"
1011

1112
s3backend "github.com/gruntwork-io/terragrunt/internal/remotestate/backend/s3"
@@ -26,47 +27,50 @@ func TestAwsCountingSemaphoreHappyPath(t *testing.T) {
2627
func TestAwsCountingSemaphoreConcurrency(t *testing.T) {
2728
t.Parallel()
2829

29-
permits := 10
30-
goroutines := 100
31-
semaphore := s3backend.NewCountingSemaphore(permits)
30+
synctest.Test(t, func(t *testing.T) {
31+
permits := 10
32+
goroutines := 100
33+
semaphore := s3backend.NewCountingSemaphore(permits)
3234

33-
var (
34-
goRoutinesExecutingSimultaneously uint32
35-
waitForAllGoRoutinesToFinish sync.WaitGroup
36-
)
35+
var (
36+
goRoutinesExecutingSimultaneously uint32
37+
waitForAllGoRoutinesToFinish sync.WaitGroup
38+
)
3739

38-
endGoRoutine := func() {
39-
// Decrement the number of running goroutines. Note that decrementing an unsigned int is a bit odd.
40-
// This is copied from the docs: https://golang.org/pkg/sync/atomic/#AddUint32
41-
atomic.AddUint32(&goRoutinesExecutingSimultaneously, ^uint32(0))
40+
endGoRoutine := func() {
41+
// Decrement the number of running goroutines. Note that decrementing an unsigned int is a bit odd.
42+
// This is copied from the docs: https://golang.org/pkg/sync/atomic/#AddUint32
43+
atomic.AddUint32(&goRoutinesExecutingSimultaneously, ^uint32(0))
4244

43-
semaphore.Release()
44-
waitForAllGoRoutinesToFinish.Done()
45-
}
45+
semaphore.Release()
46+
waitForAllGoRoutinesToFinish.Done()
47+
}
4648

47-
runGoRoutine := func() {
48-
defer endGoRoutine()
49+
runGoRoutine := func() {
50+
defer endGoRoutine()
4951

50-
semaphore.Acquire()
52+
semaphore.Acquire()
5153

52-
// Increment the total number of running goroutines
53-
totalGoRoutinesExecutingSimultaneously := atomic.AddUint32(&goRoutinesExecutingSimultaneously, 1)
54+
// Increment the total number of running goroutines
55+
totalGoRoutinesExecutingSimultaneously := atomic.AddUint32(&goRoutinesExecutingSimultaneously, 1)
5456

55-
if totalGoRoutinesExecutingSimultaneously > uint32(permits) {
56-
t.Fatalf("The semaphore was only supposed to allow %d goroutines to run simultaneously, but has allowed %d", permits, totalGoRoutinesExecutingSimultaneously)
57-
}
57+
if totalGoRoutinesExecutingSimultaneously > uint32(permits) {
58+
t.Fatalf("The semaphore was only supposed to allow %d goroutines to run simultaneously, but has allowed %d", permits, totalGoRoutinesExecutingSimultaneously)
59+
}
5860

59-
// Sleep for a random amount of time to represent this goroutine doing work
60-
randomSleepTime := rand.Intn(100)
61-
time.Sleep(time.Duration(randomSleepTime) * time.Millisecond) //nolint:synctestcheck // TODO migrate to synctest
62-
}
61+
// Sleep for a random amount of time to represent this goroutine doing work.
62+
// Under synctest the clock is fake, so this is deterministic and free.
63+
randomSleepTime := rand.Intn(100)
64+
time.Sleep(time.Duration(randomSleepTime) * time.Millisecond)
65+
}
6366

64-
// Fire up a whole bunch of goroutines that will all try to acquire the semaphore at the same time
65-
for range goroutines {
66-
waitForAllGoRoutinesToFinish.Add(1)
67+
// Fire up a whole bunch of goroutines that will all try to acquire the semaphore at the same time
68+
for range goroutines {
69+
waitForAllGoRoutinesToFinish.Add(1)
6770

68-
go runGoRoutine()
69-
}
71+
go runGoRoutine()
72+
}
7073

71-
waitForAllGoRoutinesToFinish.Wait()
74+
waitForAllGoRoutinesToFinish.Wait()
75+
})
7276
}

0 commit comments

Comments
 (0)