Skip to content

Commit 9ca0912

Browse files
committed
fixenv
1 parent d1abf29 commit 9ca0912

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

internal/th/fixenv.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package th
22

33
import (
44
"context"
5-
"sync"
5+
"github.com/rekby/safemutex"
66
"testing"
77

88
"github.com/maxatome/go-testdeep"
@@ -19,7 +19,10 @@ type Env struct {
1919
func NewEnv(t *testing.T) (env *Env, ctx context.Context, cancel func()) {
2020
td := testdeep.NewT(t)
2121
ctx, ctxCancel := TestContext(td)
22-
tWrap := &testWrapper{T: td}
22+
tWrap := &testWrapper{
23+
T: td,
24+
m: safemutex.NewWithOptions(testWrapperSynced{}, safemutex.MutexOptions{AllowPointers: true}),
25+
}
2326
env = &Env{
2427
EnvT: fixenv.NewEnv(tWrap),
2528
Ctx: ctx,
@@ -41,32 +44,40 @@ type TD struct {
4144
type testWrapper struct {
4245
*testdeep.T
4346

44-
m sync.Mutex
47+
m safemutex.Mutex[testWrapperSynced]
48+
}
49+
50+
type testWrapperSynced struct {
4551
cleanups []func()
4652
cleanupsStarted bool
4753
}
4854

4955
func (t *testWrapper) Cleanup(f func()) {
50-
t.m.Lock()
51-
defer t.m.Unlock()
52-
53-
t.cleanups = append(t.cleanups, f)
56+
t.m.Lock(func(synced testWrapperSynced) testWrapperSynced {
57+
synced.cleanups = append(synced.cleanups, f)
58+
return synced
59+
})
5460
}
5561

5662
func (t *testWrapper) startCleanups() {
57-
t.m.Lock()
58-
started := t.cleanupsStarted
59-
if !started {
60-
t.cleanupsStarted = true
61-
}
62-
t.m.Unlock()
63+
var started bool
64+
var cleanups []func()
65+
66+
t.m.Lock(func(synced testWrapperSynced) testWrapperSynced {
67+
started := synced.cleanupsStarted
68+
if !started {
69+
synced.cleanupsStarted = true
70+
}
71+
cleanups = synced.cleanups
72+
return synced
73+
})
6374

6475
if started {
6576
return
6677
}
6778

68-
for i := len(t.cleanups) - 1; i >= 0; i-- {
69-
f := t.cleanups[i]
79+
for i := len(cleanups) - 1; i >= 0; i-- {
80+
f := cleanups[i]
7081
f()
7182
}
7283
}

0 commit comments

Comments
 (0)