-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9727 from NthPortal/topic/TrieMap-fixes-2/PR
Fix TrieMap#{filterInPlace,mapValuesInPlace}
- Loading branch information
Showing
8 changed files
with
157 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
test/junit/scala/collection/concurrent/ConcurrentMapTestHelper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.collection.concurrent | ||
|
||
import scala.concurrent.duration.SECONDS | ||
|
||
object ConcurrentMapTestHelper { | ||
def genericTest_filterInPlace(newMap: => Map[String, Int]): Unit = { | ||
val tester = new ConcurrentMapTester(newMap += "k" -> 0) | ||
|
||
tester.runTasks(5, SECONDS)( | ||
_.filterInPlace((_, v) => { | ||
SECONDS.sleep(2) | ||
v > 0 | ||
}), | ||
map => { | ||
SECONDS.sleep(1) | ||
map("k") = 1 | ||
}, | ||
) | ||
|
||
tester.assertContainsEntry("k", 1) // can get `0` if incorrectly implemented | ||
} | ||
|
||
def genericTest_mapValuesInPlace(newMap: => Map[String, Int]): Unit = { | ||
val tester = new ConcurrentMapTester(newMap += "k" -> 0) | ||
tester.runTasks(5, SECONDS)( | ||
_.mapValuesInPlace((_, v) => { | ||
SECONDS.sleep(2) | ||
v + 5 | ||
}), | ||
map => { | ||
SECONDS.sleep(1) | ||
map("k") = 1 | ||
}, | ||
) | ||
|
||
tester.assertExistsEntry("k", x => x == 1 || x == 6) // can get `5` if incorrectly implemented | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
test/junit/scala/collection/concurrent/ConcurrentMapTester.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package scala.collection.concurrent | ||
|
||
import java.util.concurrent.Executors | ||
import scala.concurrent.duration.TimeUnit | ||
|
||
class ConcurrentMapTester[K, V](map: Map[K, V]) { | ||
def runTasks(executionTimeout: Long, unit: TimeUnit)(tasks: (Map[K, V] => Unit)*): Unit = { | ||
val exec = Executors.newCachedThreadPool() | ||
for (task <- tasks) exec.execute(() => task(map)) | ||
exec.shutdown() | ||
exec.awaitTermination(executionTimeout, unit) | ||
} | ||
|
||
@throws[AssertionError] | ||
def assertContainsEntry(k: K, v: V): Unit = { | ||
val value = map.get(k) | ||
assert(value.isDefined, s"map does not contain key '$k'") | ||
assert(value.contains(v), s"key '$k' is mapped to '${value.get}', not to '$v'") | ||
} | ||
|
||
@throws[AssertionError] | ||
def assertExistsEntry(k: K, p: V => Boolean): Unit = { | ||
val value = map.get(k) | ||
assert(value.isDefined, s"map does not contain key '$k'") | ||
assert(value.exists(p), s"key '$k' is mapped to '${value.get}', which does not match the predicate") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/junit/scala/collection/convert/JConcurrentMapWrapperTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package scala.collection.convert | ||
|
||
import org.junit.Test | ||
|
||
import java.util.concurrent.{ConcurrentHashMap, ConcurrentSkipListMap} | ||
|
||
import scala.collection.concurrent.ConcurrentMapTestHelper | ||
import scala.jdk.CollectionConverters._ | ||
|
||
class JConcurrentMapWrapperTest { | ||
@Test | ||
def CHM_filterInPlace(): Unit = { | ||
ConcurrentMapTestHelper.genericTest_filterInPlace(new ConcurrentHashMap[String, Int].asScala) | ||
} | ||
|
||
@Test | ||
def CHM_mapValuesInPlace(): Unit = { | ||
ConcurrentMapTestHelper.genericTest_mapValuesInPlace(new ConcurrentHashMap[String, Int].asScala) | ||
} | ||
|
||
@Test | ||
def CSLM_filterInPlace(): Unit = { | ||
ConcurrentMapTestHelper.genericTest_filterInPlace(new ConcurrentSkipListMap[String, Int].asScala) | ||
} | ||
|
||
@Test | ||
def CSLM_mapValuesInPlace(): Unit = { | ||
ConcurrentMapTestHelper.genericTest_mapValuesInPlace(new ConcurrentSkipListMap[String, Int].asScala) | ||
} | ||
} |