|
| 1 | +package ox |
| 2 | + |
| 3 | +import org.scalatest.flatspec.AnyFlatSpec |
| 4 | +import org.scalatest.matchers.should.Matchers |
| 5 | +import ox.syntax.mapPar |
| 6 | +import ox.util.Trail |
| 7 | + |
| 8 | +import java.util.concurrent.atomic.AtomicInteger |
| 9 | +import scala.collection.IterableFactory |
| 10 | +import scala.collection.immutable.Iterable |
| 11 | +import scala.List |
| 12 | + |
| 13 | +class MapParTest extends AnyFlatSpec with Matchers { |
| 14 | + "mapPar" should "output the same type as input" in { |
| 15 | + val input = List(1, 2, 3) |
| 16 | + val result = input.mapPar(1)(identity) |
| 17 | + result shouldBe a[List[_]] |
| 18 | + } |
| 19 | + |
| 20 | + it should "run computations in parallel" in { |
| 21 | + val InputElements = 17 |
| 22 | + val TransformationMillis: Long = 100 |
| 23 | + |
| 24 | + val input = (0 to InputElements) |
| 25 | + def transformation(i: Int) = { |
| 26 | + Thread.sleep(TransformationMillis) |
| 27 | + i + 1 |
| 28 | + } |
| 29 | + |
| 30 | + val start = System.currentTimeMillis() |
| 31 | + val result = input.to(Iterable).mapPar(5)(transformation) |
| 32 | + val end = System.currentTimeMillis() |
| 33 | + |
| 34 | + result.toList should contain theSameElementsInOrderAs (input.map(_ + 1)) |
| 35 | + (end - start) should be < (InputElements * TransformationMillis) |
| 36 | + } |
| 37 | + |
| 38 | + it should "run not more computations than limit" in { |
| 39 | + val Parallelism = 5 |
| 40 | + |
| 41 | + val input = (1 to 158) |
| 42 | + |
| 43 | + class MaxCounter { |
| 44 | + val counter = new AtomicInteger(0) |
| 45 | + var max = 0 |
| 46 | + def increment() = { |
| 47 | + counter.updateAndGet { c => |
| 48 | + val inc = c + 1 |
| 49 | + max = if (inc > max) inc else max |
| 50 | + inc |
| 51 | + } |
| 52 | + } |
| 53 | + def decrement() = { |
| 54 | + counter.decrementAndGet() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + val maxCounter = new MaxCounter |
| 59 | + |
| 60 | + def transformation(i: Int) = { |
| 61 | + maxCounter.increment() |
| 62 | + Thread.sleep(10) |
| 63 | + maxCounter.decrement() |
| 64 | + } |
| 65 | + |
| 66 | + input.to(Iterable).mapPar(Parallelism)(transformation) |
| 67 | + |
| 68 | + maxCounter.max should be <= Parallelism |
| 69 | + } |
| 70 | + |
| 71 | + it should "interrupt other computations in one fails" in { |
| 72 | + val InputElements = 18 |
| 73 | + val TransformationMillis: Long = 100 |
| 74 | + val trail = Trail() |
| 75 | + |
| 76 | + val input = (0 to InputElements) |
| 77 | + |
| 78 | + def transformation(i: Int) = { |
| 79 | + if (i == 4) { |
| 80 | + trail.add("exception") |
| 81 | + throw new Exception("boom") |
| 82 | + } else { |
| 83 | + Thread.sleep(TransformationMillis) |
| 84 | + trail.add("transformation") |
| 85 | + i + 1 |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + input.to(Iterable).mapPar(5)(transformation) |
| 91 | + } catch { |
| 92 | + case e: Exception if e.getMessage == "boom" => trail.add("catch") |
| 93 | + } |
| 94 | + |
| 95 | + Thread.sleep(300) |
| 96 | + trail.add("all done") |
| 97 | + |
| 98 | + trail.get shouldBe Vector("exception", "catch", "all done") |
| 99 | + } |
| 100 | +} |
0 commit comments