@@ -2,7 +2,7 @@ package th
2
2
3
3
import (
4
4
"context"
5
- "sync "
5
+ "github.com/rekby/safemutex "
6
6
"testing"
7
7
8
8
"github.com/maxatome/go-testdeep"
@@ -19,7 +19,10 @@ type Env struct {
19
19
func NewEnv (t * testing.T ) (env * Env , ctx context.Context , cancel func ()) {
20
20
td := testdeep .NewT (t )
21
21
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
+ }
23
26
env = & Env {
24
27
EnvT : fixenv .NewEnv (tWrap ),
25
28
Ctx : ctx ,
@@ -41,32 +44,40 @@ type TD struct {
41
44
type testWrapper struct {
42
45
* testdeep.T
43
46
44
- m sync.Mutex
47
+ m safemutex.Mutex [testWrapperSynced ]
48
+ }
49
+
50
+ type testWrapperSynced struct {
45
51
cleanups []func ()
46
52
cleanupsStarted bool
47
53
}
48
54
49
55
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
+ } )
54
60
}
55
61
56
62
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
+ })
63
74
64
75
if started {
65
76
return
66
77
}
67
78
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 ]
70
81
f ()
71
82
}
72
83
}
0 commit comments