@@ -19,6 +19,7 @@ func TestFuture(t *testing.T) {
1919 time .Sleep (100 * time .Millisecond )
2020 p .Success (true )
2121 }()
22+
2223 res , err := p .Future ().Join ()
2324
2425 assert .Equal (t , true , res )
@@ -42,6 +43,7 @@ func TestFuture_Utils(t *testing.T) {
4243 time .Sleep (300 * time .Millisecond )
4344 p3 .Failure (err3 )
4445 }()
46+
4547 arr := []Future [* int ]{p1 .Future (), p2 .Future (), p3 .Future ()}
4648 res := []any {res1 , res2 , err3 }
4749 futRes , _ := FutureSeq (arr ).Join ()
@@ -55,6 +57,7 @@ func TestFuture_FirstCompleted(t *testing.T) {
5557 time .Sleep (100 * time .Millisecond )
5658 p .Success (util .Ptr (true ))
5759 }()
60+
5861 timeout := FutureTimer [* bool ](10 * time .Millisecond )
5962 futRes , futErr := FutureFirstCompletedOf (p .Future (), timeout ).Join ()
6063
@@ -68,6 +71,7 @@ func TestFuture_Transform(t *testing.T) {
6871 time .Sleep (100 * time .Millisecond )
6972 p1 .Success (util .Ptr (1 ))
7073 }()
74+
7175 future := p1 .Future ().Map (func (v * int ) (* int , error ) {
7276 inc := * v + 1
7377 return & inc , nil
@@ -96,6 +100,7 @@ func TestFuture_Recover(t *testing.T) {
96100 time .Sleep (10 * time .Millisecond )
97101 p2 .Failure (errors .New ("recover Future failure" ))
98102 }()
103+
99104 future := p1 .Future ().Map (func (_ int ) (int , error ) {
100105 return 0 , errors .New ("map error" )
101106 }).FlatMap (func (_ int ) (Future [int ], error ) {
@@ -116,17 +121,30 @@ func TestFuture_Recover(t *testing.T) {
116121}
117122
118123func TestFuture_Failure (t * testing.T ) {
119- p1 := NewPromise [* int ]()
120- p2 := NewPromise [* int ]()
124+ p1 := NewPromise [int ]()
125+ p2 := NewPromise [int ]()
126+ p3 := NewPromise [int ]()
127+ err := errors .New ("error" )
121128 go func () {
122- time .Sleep (10 * time .Millisecond )
123- p1 .Failure (errors .New ("Future error" ))
124- time .Sleep (20 * time .Millisecond )
125- p2 .Success (util .Ptr (2 ))
129+ time .Sleep (5 * time .Millisecond )
130+ p1 .Failure (err )
131+ time .Sleep (5 * time .Millisecond )
132+ p2 .Failure (err )
133+ time .Sleep (5 * time .Millisecond )
134+ p3 .Success (2 )
126135 }()
127- res , _ := p1 .Future ().RecoverWith (p2 .Future ()).Join ()
128136
129- assert .Equal (t , 2 , * res )
137+ res , _ := p1 .Future ().
138+ Map (func (_ int ) (int , error ) { return 0 , err }).
139+ FlatMap (func (_ int ) (Future [int ], error ) { return p1 .Future (), err }).
140+ RecoverWith (p2 .Future ()).
141+ RecoverWith (p3 .Future ()).
142+ FlatMap (func (_ int ) (Future [int ], error ) { return p2 .Future (), err }).
143+ RecoverWith (p3 .Future ()).
144+ RecoverWith (p3 .Future ()).
145+ Join ()
146+
147+ assert .Equal (t , 2 , res )
130148}
131149
132150func TestFuture_Timeout (t * testing.T ) {
@@ -135,6 +153,7 @@ func TestFuture_Timeout(t *testing.T) {
135153 time .Sleep (100 * time .Millisecond )
136154 p .Success (true )
137155 }()
156+
138157 future := p .Future ()
139158
140159 ctx , cancel := context .WithTimeout (context .Background (),
@@ -150,7 +169,6 @@ func TestFuture_Timeout(t *testing.T) {
150169
151170func TestFuture_GoroutineLeak (t * testing.T ) {
152171 var wg sync.WaitGroup
153-
154172 fmt .Println (runtime .NumGoroutine ())
155173
156174 numFuture := 100
@@ -176,6 +194,7 @@ func TestFuture_GoroutineLeak(t *testing.T) {
176194 time .Sleep (10 * time .Millisecond )
177195 numGoroutine := runtime .NumGoroutine ()
178196 fmt .Println (numGoroutine )
197+
179198 if numGoroutine > numFuture {
180199 t .Fatalf ("numGoroutine is %d" , numGoroutine )
181200 }
0 commit comments