Skip to content

Commit 281589c

Browse files
committed
Add a test case to BlockFetcherIteratorSuite.scala for fetching block from remote from successfully
1 parent 0654128 commit 281589c

File tree

1 file changed

+64
-9
lines changed

1 file changed

+64
-9
lines changed

core/src/test/scala/org/apache/spark/storage/BlockFetcherIteratorSuite.scala

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import org.mockito.Matchers.any
2424

2525
import java.nio.ByteBuffer
2626

27+
import scala.collection.mutable.ArrayBuffer
2728
import scala.concurrent.future
2829
import scala.concurrent.ExecutionContext.Implicits.global
30+
2931
import org.apache.spark._
3032
import org.apache.spark.storage.BlockFetcherIterator._
3133
import org.apache.spark.network.{ConnectionManager, ConnectionManagerId,
@@ -34,30 +36,29 @@ import org.apache.spark.network.{ConnectionManager, ConnectionManagerId,
3436
class BlockFetcherIteratorSuite extends FunSuite with Matchers {
3537

3638
test("block fetch from remote fails using BasicBlockFetcherIterator") {
37-
val conf = new SparkConf
3839
val blockManager = mock(classOf[BlockManager])
3940
val connManager = mock(classOf[ConnectionManager])
40-
val message = Message.createBufferMessage(0)
41-
message.hasError = true
42-
val someMessage = Some(message)
41+
when(blockManager.connectionManager).thenReturn(connManager)
4342

4443
val f = future {
44+
val message = Message.createBufferMessage(0)
45+
message.hasError = true
46+
val someMessage = Some(message)
4547
someMessage
4648
}
47-
when(blockManager.connectionManager).thenReturn(connManager)
4849
when(connManager.sendMessageReliably(any(),
4950
any())).thenReturn(f)
5051
when(blockManager.futureExecContext).thenReturn(global)
52+
5153
when(blockManager.blockManagerId).thenReturn(
5254
BlockManagerId("test-client", "test-client", 1, 0))
5355
when(blockManager.maxBytesInFlight).thenReturn(48 * 1024 * 1024)
5456

55-
val dummyBlId1 = ShuffleBlockId(0,0,0)
56-
val dummyBlId2 = ShuffleBlockId(0,1,0)
57+
val blId1 = ShuffleBlockId(0,0,0)
58+
val blId2 = ShuffleBlockId(0,1,0)
5759
val bmId = BlockManagerId("test-server", "test-server",1 , 0)
5860
val blocksByAddress = Seq[(BlockManagerId, Seq[(BlockId, Long)])](
59-
(bmId, Seq((dummyBlId1, 1))),
60-
(bmId, Seq((dummyBlId2, 1)))
61+
(bmId, Seq((blId1, 1), (blId2, 1)))
6162
)
6263

6364
val iterator = new BasicBlockFetcherIterator(blockManager,
@@ -71,4 +72,58 @@ class BlockFetcherIteratorSuite extends FunSuite with Matchers {
7172
}
7273
}
7374

75+
test("block fetch from remote succeed using BasicBlockFetcherIterator") {
76+
val blockManager = mock(classOf[BlockManager])
77+
val connManager = mock(classOf[ConnectionManager])
78+
when(blockManager.connectionManager).thenReturn(connManager)
79+
80+
val blId1 = ShuffleBlockId(0,0,0)
81+
val blId2 = ShuffleBlockId(0,1,0)
82+
val buf1 = ByteBuffer.allocate(4)
83+
val buf2 = ByteBuffer.allocate(4)
84+
buf1.putInt(1)
85+
buf1.flip()
86+
buf2.putInt(1)
87+
buf2.flip()
88+
val blockMessage1 = BlockMessage.fromGotBlock(GotBlock(blId1, buf1))
89+
val blockMessage2 = BlockMessage.fromGotBlock(GotBlock(blId2, buf2))
90+
val blockMessageArray = new BlockMessageArray(
91+
Seq(blockMessage1, blockMessage2))
92+
93+
val bufferMessage = blockMessageArray.toBufferMessage
94+
val buffer = ByteBuffer.allocate(bufferMessage.size)
95+
val arrayBuffer = new ArrayBuffer[ByteBuffer]
96+
bufferMessage.buffers.foreach{ b =>
97+
buffer.put(b)
98+
}
99+
buffer.flip
100+
arrayBuffer += buffer
101+
102+
val someMessage = Some(Message.createBufferMessage(arrayBuffer))
103+
104+
val f = future {
105+
someMessage
106+
}
107+
when(connManager.sendMessageReliably(any(),
108+
any())).thenReturn(f)
109+
when(blockManager.futureExecContext).thenReturn(global)
110+
111+
when(blockManager.blockManagerId).thenReturn(
112+
BlockManagerId("test-client", "test-client", 1, 0))
113+
when(blockManager.maxBytesInFlight).thenReturn(48 * 1024 * 1024)
114+
115+
val bmId = BlockManagerId("test-server", "test-server",1 , 0)
116+
val blocksByAddress = Seq[(BlockManagerId, Seq[(BlockId, Long)])](
117+
(bmId, Seq((blId1, 1), (blId2, 1)))
118+
)
119+
120+
val iterator = new BasicBlockFetcherIterator(blockManager,
121+
blocksByAddress, null)
122+
iterator.initialize()
123+
iterator.foreach{
124+
case (_, r) => {
125+
(r.isDefined) should be(true)
126+
}
127+
}
128+
}
74129
}

0 commit comments

Comments
 (0)