Github user srinathshankar commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15043#discussion_r79105418
  
    --- Diff: 
core/src/test/scala/org/apache/spark/storage/PartiallySerializedBlockSuite.scala
 ---
    @@ -0,0 +1,215 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.storage
    +
    +import java.nio.ByteBuffer
    +
    +import scala.reflect.ClassTag
    +
    +import org.mockito.Mockito
    +import org.mockito.Mockito.atLeastOnce
    +import org.mockito.invocation.InvocationOnMock
    +import org.mockito.stubbing.Answer
    +import org.scalatest.{BeforeAndAfterEach, PrivateMethodTester}
    +
    +import org.apache.spark.{SparkConf, SparkFunSuite, TaskContext, 
TaskContextImpl}
    +import org.apache.spark.memory.MemoryMode
    +import org.apache.spark.serializer.{JavaSerializer, SerializationStream, 
SerializerManager}
    +import org.apache.spark.storage.memory.{MemoryStore, 
PartiallySerializedBlock, RedirectableOutputStream}
    +import org.apache.spark.util.{ByteBufferInputStream, 
ByteBufferOutputStream}
    +import org.apache.spark.util.io.{ChunkedByteBuffer, 
ChunkedByteBufferOutputStream}
    +
    +class PartiallySerializedBlockSuite
    +    extends SparkFunSuite
    +    with BeforeAndAfterEach
    +    with PrivateMethodTester {
    +
    +  private val blockId = new TestBlockId("test")
    +  private val conf = new SparkConf()
    +  private val memoryStore = Mockito.mock(classOf[MemoryStore], 
Mockito.RETURNS_SMART_NULLS)
    +  private val serializerManager = new SerializerManager(new 
JavaSerializer(conf), conf)
    +
    +  private val getSerializationStream = 
PrivateMethod[SerializationStream]('serializationStream)
    +  private val getRedirectableOutputStream =
    +    PrivateMethod[RedirectableOutputStream]('redirectableOutputStream)
    +
    +  override protected def beforeEach(): Unit = {
    +    super.beforeEach()
    +    Mockito.reset(memoryStore)
    +  }
    +
    +  private def partiallyUnroll[T: ClassTag](
    +      iter: Iterator[T],
    +      numItemsToBuffer: Int): PartiallySerializedBlock[T] = {
    +
    +    val bbos: ChunkedByteBufferOutputStream = {
    +      val spy = Mockito.spy(new ChunkedByteBufferOutputStream(128, 
ByteBuffer.allocate))
    +      Mockito.doAnswer(new Answer[ChunkedByteBuffer] {
    +        override def answer(invocationOnMock: InvocationOnMock): 
ChunkedByteBuffer = {
    +          
Mockito.spy(invocationOnMock.callRealMethod().asInstanceOf[ChunkedByteBuffer])
    +        }
    +      }).when(spy).toChunkedByteBuffer
    +      spy
    +    }
    +
    +    val serializer = 
serializerManager.getSerializer(implicitly[ClassTag[T]]).newInstance()
    +    val redirectableOutputStream = Mockito.spy(new 
RedirectableOutputStream)
    +    redirectableOutputStream.setOutputStream(bbos)
    +    val serializationStream = 
Mockito.spy(serializer.serializeStream(redirectableOutputStream))
    +
    +    (1 to numItemsToBuffer).foreach { _ =>
    +      assert(iter.hasNext)
    +      serializationStream.writeObject[T](iter.next())
    +    }
    +
    +    val unrollMemory = bbos.size
    +    new PartiallySerializedBlock[T](
    +      memoryStore,
    +      serializerManager,
    +      blockId,
    +      serializationStream = serializationStream,
    +      redirectableOutputStream,
    +      unrollMemory = unrollMemory,
    +      memoryMode = MemoryMode.ON_HEAP,
    +      bbos,
    +      rest = iter,
    +      classTag = implicitly[ClassTag[T]])
    +  }
    +
    +  test("valuesIterator() and finishWritingToStream() cannot be called 
after discard() is called") {
    +    val partiallySerializedBlock = partiallyUnroll((1 to 10).iterator, 2)
    +    partiallySerializedBlock.discard()
    +    intercept[IllegalStateException] {
    +      partiallySerializedBlock.finishWritingToStream(null)
    +    }
    +    intercept[IllegalStateException] {
    +      partiallySerializedBlock.valuesIterator
    +    }
    +  }
    +
    +  test("discard() can be called more than once") {
    +    val partiallySerializedBlock = partiallyUnroll((1 to 10).iterator, 2)
    +    partiallySerializedBlock.discard()
    +    partiallySerializedBlock.discard()
    +  }
    +
    +  test("cannot call valuesIterator() more than once") {
    +    val partiallySerializedBlock = partiallyUnroll((1 to 10).iterator, 2)
    +    partiallySerializedBlock.valuesIterator
    +    intercept[IllegalStateException] {
    +      partiallySerializedBlock.valuesIterator
    +    }
    +  }
    +
    +  test("cannot call finishWritingToStream() more than once") {
    +    val partiallySerializedBlock = partiallyUnroll((1 to 10).iterator, 2)
    +    partiallySerializedBlock.finishWritingToStream(new 
ByteBufferOutputStream())
    +    intercept[IllegalStateException] {
    +      partiallySerializedBlock.finishWritingToStream(new 
ByteBufferOutputStream())
    +    }
    +  }
    +
    +  test("cannot call finishWritingToStream() after valuesIterator()") {
    +    val partiallySerializedBlock = partiallyUnroll((1 to 10).iterator, 2)
    +    partiallySerializedBlock.valuesIterator
    +    intercept[IllegalStateException] {
    +      partiallySerializedBlock.finishWritingToStream(new 
ByteBufferOutputStream())
    +    }
    +  }
    +
    +  test("cannot call valuesIterator() after finishWritingToStream()") {
    --- End diff --
    
    Minor: You can probably combine 
    test("cannot call valuesIterator() more than once") and
    test("cannot call finishWritingToStream() after valuesIterator()") into one 
test
    Same for making calls after finishWritingToStream


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to