@@ -10,126 +10,114 @@ import scala.util.Try
10
10
class RaceSpec extends AnyFlatSpec with Matchers {
11
11
" Racing two functions" should " return the result of the first one that completes and cancel the execution of the other" in {
12
12
val results = new ConcurrentLinkedQueue [String ]()
13
- val actual : Int | String = structured {
14
- race[Int , String ](
15
- {
16
- delay(1 .second)
17
- results.add(" job1" )
18
- throw new RuntimeException (" Error" )
19
- }, {
20
- delay(500 .millis)
21
- results.add(" job2" )
22
- " 42"
23
- }
24
- )
25
- }
13
+ val actual : Int | String = race[Int , String ](
14
+ {
15
+ delay(1 .second)
16
+ results.add(" job1" )
17
+ throw new RuntimeException (" Error" )
18
+ }, {
19
+ delay(500 .millis)
20
+ results.add(" job2" )
21
+ " 42"
22
+ }
23
+ )
26
24
27
25
actual should be(" 42" )
28
26
results.toArray should contain theSameElementsInOrderAs List (" job2" )
29
27
}
30
28
31
29
it should " return the result of the second one if the first one throws an exception" in {
32
30
val results = new ConcurrentLinkedQueue [String ]()
33
- val actual : Int | String = structured {
34
- race(
35
- {
36
- delay(1 .second)
37
- results.add(" job1" )
38
- 42
39
- }, {
40
- delay(500 .millis)
41
- results.add(" job2" )
42
- throw new RuntimeException (" Error" )
43
- }
44
- )
45
- }
31
+ val actual : Int | String = race(
32
+ {
33
+ delay(1 .second)
34
+ results.add(" job1" )
35
+ 42
36
+ }, {
37
+ delay(500 .millis)
38
+ results.add(" job2" )
39
+ throw new RuntimeException (" Error" )
40
+ }
41
+ )
46
42
47
43
actual should be(42 )
48
44
results.toArray should contain theSameElementsInOrderAs List (" job2" , " job1" )
49
45
}
50
46
51
47
it should " honor the structural concurrency and wait for all the jobs to complete" in {
52
48
val results = new ConcurrentLinkedQueue [String ]()
53
- val actual : Int | String = structured {
54
- race(
55
- {
56
- val job1 = fork {
57
- fork {
58
- delay(2 .second)
59
- results.add(" job3" )
60
- }
61
- delay(1 .second)
62
- results.add(" job1" )
49
+ val actual : Int | String = race(
50
+ {
51
+ val job1 = fork {
52
+ fork {
53
+ delay(2 .second)
54
+ results.add(" job3" )
63
55
}
64
- 42
65
- }, {
66
- delay(500 .millis)
67
- throw new RuntimeException (" Error" )
56
+ delay(1 .second)
57
+ results.add(" job1" )
68
58
}
69
- )
70
- }
59
+ 42
60
+ }, {
61
+ delay(500 .millis)
62
+ throw new RuntimeException (" Error" )
63
+ }
64
+ )
71
65
72
66
actual should be(42 )
73
67
results.toArray should contain theSameElementsInOrderAs List (" job1" , " job3" )
74
68
}
75
69
76
70
it should " throw the exception thrown by the first function both throw an exception" in {
77
71
val expectedResult = Try {
78
- structured {
79
- race(
80
- {
81
- delay(1 .second)
82
- throw new RuntimeException (" Error in job1" )
83
- }, {
84
- delay(500 .millis)
85
- throw new RuntimeException (" Error in job2" )
86
- }
87
- )
88
- }
72
+ race(
73
+ {
74
+ delay(1 .second)
75
+ throw new RuntimeException (" Error in job1" )
76
+ }, {
77
+ delay(500 .millis)
78
+ throw new RuntimeException (" Error in job2" )
79
+ }
80
+ )
89
81
}
90
82
91
83
expectedResult.failure.exception shouldBe a[RuntimeException ]
92
84
expectedResult.failure.exception.getMessage shouldBe " Error in job2"
93
85
}
94
86
95
87
it should " honor the structural concurrency and return the value of the second function if the first threw an exception" in {
96
- val actual : Int | String = structured {
97
- race(
98
- {
99
- val job1 = fork {
100
- delay(500 .millis)
101
- println(" job1" )
102
- throw new RuntimeException (" Error in job1" )
103
- }
104
- 42
105
- }, {
106
- delay(1 .second)
107
- println(" job2" )
108
- " 42"
88
+ val actual : Int | String = race(
89
+ {
90
+ val job1 = fork {
91
+ delay(500 .millis)
92
+ println(" job1" )
93
+ throw new RuntimeException (" Error in job1" )
109
94
}
110
- )
111
- }
95
+ 42
96
+ }, {
97
+ delay(1 .second)
98
+ println(" job2" )
99
+ " 42"
100
+ }
101
+ )
112
102
113
103
actual should be(" 42" )
114
104
}
115
105
116
106
it should " honor the structured concurrency and cancel all the children jobs" in {
117
107
val results = new ConcurrentLinkedQueue [String ]()
118
- val actual : Int | String = structured {
119
- race(
120
- {
121
- val job1 = fork {
122
- delay(1 .seconds)
123
- results.add(" job1" )
124
- }
125
- 42
126
- }, {
127
- delay(500 .millis)
128
- results.add(" job2" )
129
- " 42"
108
+ val actual : Int | String = race(
109
+ {
110
+ val job1 = fork {
111
+ delay(1 .seconds)
112
+ results.add(" job1" )
130
113
}
131
- )
132
- }
114
+ 42
115
+ }, {
116
+ delay(500 .millis)
117
+ results.add(" job2" )
118
+ " 42"
119
+ }
120
+ )
133
121
134
122
Thread .sleep(2000 )
135
123
0 commit comments