4
4
"context"
5
5
"errors"
6
6
"iter"
7
- "maps"
8
7
"reflect"
8
+ "sync"
9
9
"testing"
10
10
"time"
11
11
)
@@ -36,6 +36,19 @@ func TestSleep(t *testing.T) {
36
36
})
37
37
}
38
38
39
+ func TestIsDone (t * testing.T ) {
40
+ ctx , cancel := context .WithCancel (context .Background ())
41
+ if IsDone (ctx ) {
42
+ t .Fatal ("expected false, got true" )
43
+ }
44
+
45
+ cancel ()
46
+
47
+ if ! IsDone (ctx ) {
48
+ t .Fatal ("expected true, got false" )
49
+ }
50
+ }
51
+
39
52
func TestAll (t * testing.T ) {
40
53
t .Run ("NoError" , func (t * testing.T ) {
41
54
jobs := []Job [int ]{
@@ -58,9 +71,15 @@ func TestAll(t *testing.T) {
58
71
t .Run ("JobError" , func (t * testing.T ) {
59
72
expectedErr := errors .New ("test error" )
60
73
jobs := []Job [int ]{
61
- func (ctx context.Context ) (int , error ) { return 1 , nil },
62
- func (ctx context.Context ) (int , error ) { return 0 , expectedErr },
63
- func (ctx context.Context ) (int , error ) { return 3 , nil },
74
+ func (ctx context.Context ) (int , error ) {
75
+ return 1 , nil
76
+ },
77
+ func (ctx context.Context ) (int , error ) {
78
+ return 0 , expectedErr
79
+ },
80
+ func (ctx context.Context ) (int , error ) {
81
+ return 3 , nil
82
+ },
64
83
}
65
84
66
85
_ , err := All (jobs )
@@ -171,7 +190,7 @@ func TestRange(t *testing.T) {
171
190
func TestRange2 (t * testing.T ) {
172
191
t .Run ("NoJobError" , func (t * testing.T ) {
173
192
items := map [string ]int {"a" : 1 , "b" : 2 , "c" : 3 }
174
- processed := make ( map [ string ] bool )
193
+ processed := sync. Map {}
175
194
176
195
seq := iter.Seq2 [string , int ](
177
196
func (yield func (string , int ) bool ) {
@@ -186,7 +205,7 @@ func TestRange2(t *testing.T) {
186
205
err := Range2 (
187
206
seq ,
188
207
func (ctx context.Context , k string , v int ) error {
189
- processed [ k ] = true
208
+ processed . Store ( k , true )
190
209
return nil
191
210
},
192
211
)
@@ -195,9 +214,10 @@ func TestRange2(t *testing.T) {
195
214
t .Errorf ("unexpected error: %v" , err )
196
215
}
197
216
198
- expected := map [string ]bool {"a" : true , "b" : true , "c" : true }
199
- if ! reflect .DeepEqual (processed , expected ) {
200
- t .Errorf ("got %v, want %v" , processed , expected )
217
+ for _ , k := range []string {"a" , "b" , "c" } {
218
+ if b , ok := processed .Load (k ); ! ok || b .(bool ) == false {
219
+ t .Errorf ("key %v not processed" , k )
220
+ }
201
221
}
202
222
})
203
223
@@ -351,50 +371,3 @@ func TestMap2(t *testing.T) {
351
371
}
352
372
})
353
373
}
354
-
355
- func TestMap2InPlace (t * testing.T ) {
356
- t .Run ("NoJobError" , func (t * testing.T ) {
357
- input := map [string ]int {"a" : 1 , "b" : 2 , "c" : 3 }
358
- originalInput := maps .Clone (input )
359
-
360
- results , err := Map2InPlace (
361
- input ,
362
- func (ctx context.Context , k string , v int ) (string , int , error ) {
363
- return k , v * 2 , nil
364
- },
365
- )
366
-
367
- if err != nil {
368
- t .Errorf ("unexpected error: %v" , err )
369
- }
370
-
371
- expected := map [string ]int {"a" : 2 , "b" : 4 , "c" : 6 }
372
- if ! reflect .DeepEqual (results , expected ) {
373
- t .Errorf ("got %v, want %v" , results , expected )
374
- }
375
-
376
- // Verify the input map was modified
377
- if reflect .DeepEqual (input , originalInput ) {
378
- t .Error ("Map2InPlace did not modify the input map" )
379
- }
380
- if ! reflect .DeepEqual (input , expected ) {
381
- t .Error ("Map2InPlace did not correctly modify the input map" )
382
- }
383
- })
384
-
385
- t .Run ("JobError" , func (t * testing.T ) {
386
- expectedErr := errors .New ("test error" )
387
- input := map [string ]int {"a" : 1 , "b" : 2 , "c" : 3 }
388
-
389
- _ , err := Map2InPlace (
390
- input ,
391
- func (ctx context.Context , k string , v int ) (string , int , error ) {
392
- return k , v , expectedErr
393
- },
394
- )
395
-
396
- if err == nil {
397
- t .Error ("expected error, got nil" )
398
- }
399
- })
400
- }
0 commit comments