17
17
18
18
package org .apache .openwhisk .core .database .test
19
19
20
- import scala .collection .parallel ._
21
20
import scala .concurrent .duration .DurationInt
22
- import java .util .concurrent .ForkJoinPool
21
+ import java .util .concurrent .Executors
22
+
23
23
import org .junit .runner .RunWith
24
24
import org .scalatest .BeforeAndAfterEach
25
25
import org .scalatest .FlatSpec
@@ -32,9 +32,17 @@ import spray.json.JsString
32
32
import org .apache .openwhisk .common .TransactionId
33
33
import org .apache .openwhisk .utils .retry
34
34
35
- @ RunWith (classOf [JUnitRunner ])
36
- class CacheConcurrencyTests extends FlatSpec with WskTestHelpers with WskActorSystem with BeforeAndAfterEach {
35
+ import scala .concurrent .ExecutionContext
37
36
37
+ @ RunWith (classOf [JUnitRunner ])
38
+ class CacheConcurrencyTests
39
+ extends FlatSpec
40
+ with WskTestHelpers
41
+ with WskActorSystem
42
+ with BeforeAndAfterEach
43
+ with ConcurrencyHelpers {
44
+
45
+ val timeout = 5 .minutes
38
46
println(s " Running tests on # proc: ${Runtime .getRuntime.availableProcessors()}" )
39
47
40
48
implicit private val transId = TransactionId .testing
@@ -43,15 +51,14 @@ class CacheConcurrencyTests extends FlatSpec with WskTestHelpers with WskActorSy
43
51
44
52
val nExternalIters = 1
45
53
val nInternalIters = 5
46
- val nThreads = nInternalIters * 30
54
+ val nThreads = nInternalIters * 30 // The maximum number of tasks running in parallel at any given time
55
+ val parallelismExecutionContext = ExecutionContext .fromExecutor(Executors .newFixedThreadPool(nThreads))
47
56
48
- val parallel = (1 to nInternalIters).par
49
- parallel.tasksupport = new ForkJoinTaskSupport (new ForkJoinPool (nThreads))
50
-
51
- def run [W ](phase : String )(block : String => W ) = parallel.map { i =>
52
- val name = s " testy ${i}"
53
- withClue(s " $phase: failed for $name" ) { (name, block(name)) }
54
- }
57
+ def run [W ](phase : String )(block : String => W ) =
58
+ concurrently((1 to nInternalIters), timeout) { i =>
59
+ val name = s " testy ${i}"
60
+ withClue(s " $phase: failed for $name" ) { (name, block(name)) }
61
+ }(parallelismExecutionContext)
55
62
56
63
override def beforeEach () = {
57
64
run(" pre-test sanitize" ) { name =>
@@ -79,9 +86,7 @@ class CacheConcurrencyTests extends FlatSpec with WskTestHelpers with WskActorSy
79
86
80
87
run(" delete+get" ) { name =>
81
88
// run 30 operations in parallel: 15 get, 1 delete, 14 more get
82
- val para = (1 to 30 ).par
83
- para.tasksupport = new ForkJoinTaskSupport (new ForkJoinPool (nThreads))
84
- para.map { i =>
89
+ concurrently((1 to 30 ), timeout) { i =>
85
90
if (i != 16 ) {
86
91
val rr = wsk.action.get(name, expectedExitCode = DONTCARE_EXIT )
87
92
withClue(s " expecting get to either succeed or fail with not found: $rr" ) {
@@ -91,7 +96,7 @@ class CacheConcurrencyTests extends FlatSpec with WskTestHelpers with WskActorSy
91
96
} else {
92
97
wsk.action.delete(name)
93
98
}
94
- }
99
+ }(parallelismExecutionContext)
95
100
}
96
101
97
102
// Give some time to replicate the state between the controllers
@@ -117,9 +122,7 @@ class CacheConcurrencyTests extends FlatSpec with WskTestHelpers with WskActorSy
117
122
118
123
run(" update+get" ) { name =>
119
124
// run 30 operations in parallel: 15 get, 1 update, 14 more get
120
- val para = (1 to 30 ).par
121
- para.tasksupport = new ForkJoinTaskSupport (new ForkJoinPool (nThreads))
122
- para.map { i =>
125
+ concurrently((1 to 30 ), timeout) { i =>
123
126
if (i != 16 ) {
124
127
val rr = wsk.action.get(name, expectedExitCode = DONTCARE_EXIT )
125
128
withClue(s " expecting get to either succeed or fail with not found: $rr" ) {
@@ -129,7 +132,7 @@ class CacheConcurrencyTests extends FlatSpec with WskTestHelpers with WskActorSy
129
132
} else {
130
133
wsk.action.create(name, None , parameters = Map (" color" -> JsString (" blue" )), update = true )
131
134
}
132
- }
135
+ }(parallelismExecutionContext)
133
136
}
134
137
135
138
// All controllers should have the correct action
0 commit comments