attilapiros commented on a change in pull request #32287:
URL: https://github.com/apache/spark/pull/32287#discussion_r620235033



##########
File path: 
core/src/test/scala/org/apache/spark/storage/ShuffleBlockFetcherIteratorSuite.scala
##########
@@ -48,15 +49,22 @@ class ShuffleBlockFetcherIteratorSuite extends 
SparkFunSuite with PrivateMethodT
   // in the presence of faults.
 
   /** Creates a mock [[BlockTransferService]] that returns data from the given 
map. */
-  private def createMockTransfer(data: Map[BlockId, ManagedBuffer]): 
BlockTransferService = {
+  private def createMockTransfer(
+      data: Map[BlockId, ManagedBuffer],
+      throwNettyOOM: Boolean = false): BlockTransferService = {
     val transfer = mock(classOf[BlockTransferService])
+    var hasThrowOOM = false
     when(transfer.fetchBlocks(any(), any(), any(), any(), any(), 
any())).thenAnswer(
       (invocation: InvocationOnMock) => {
         val blocks = invocation.getArguments()(3).asInstanceOf[Array[String]]
         val listener = 
invocation.getArguments()(4).asInstanceOf[BlockFetchingListener]
+        val OOMBlockIndex = new 
Random(System.currentTimeMillis()).nextInt(blocks.length)
 
-        for (blockId <- blocks) {
-          if (data.contains(BlockId(blockId))) {
+        for ((blockId, i) <- blocks.zipWithIndex) {
+          if (throwNettyOOM && !hasThrowOOM && i == OOMBlockIndex) {
+            hasThrowOOM = true
+            listener.onBlockFetchFailure(blockId, new 
TestNettyOutOfMemoryError())

Review comment:
       With two lines of code (+ 1 extra line import) you can remove the 
`TestNettyOutOfMemoryError.scala` file and keep the production code clean from 
the test implementation detail:
   
   ```suggestion
               val ctor = 
classOf[OutOfDirectMemoryError].getDeclaredConstructor(classOf[java.lang.String])
               ctor.setAccessible(true)
               listener.onBlockFetchFailure(blockId, ctor.newInstance("failed 
to allocate memory"))
   ```
   
   The pattern (using `setAccessible`) is already used in Spark.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to