azagrebin commented on a change in pull request #8194: 
[FLINK-12216][Runtime]Respect the number of bytes from input parameters in 
HybridMemorySegment
URL: https://github.com/apache/flink/pull/8194#discussion_r341011729
 
 

 ##########
 File path: 
flink-core/src/test/java/org/apache/flink/core/memory/HybridOnHeapMemorySegmentTest.java
 ##########
 @@ -77,4 +82,54 @@ public void testHybridHeapSegmentSpecifics() {
                assertEquals(3, buf2.position());
                assertEquals(7, buf2.limit());
        }
+
+       @Test
+       public void testReadOnlyByteBufferPut() {
+               final byte[] buffer = new byte[100];
+               HybridMemorySegment seg = new HybridMemorySegment(buffer, null);
+
+               String content = "hello world";
+               ByteBuffer bb = ByteBuffer.allocate(20);
+               bb.put(content.getBytes());
+               bb.rewind();
+
+               int offset = 10;
+               int numBytes = 5;
+
+               ByteBuffer readOnlyBuf = bb.asReadOnlyBuffer();
+               assertFalse(readOnlyBuf.isDirect());
+               assertFalse(readOnlyBuf.hasArray());
+
+               seg.put(offset, readOnlyBuf, numBytes);
+
+               // verify the area before the written region.
+               for (int i = 0; i < offset; i++) {
+                       assertEquals(0, buffer[i]);
+               }
+
+               // verify the region that is written.
+               assertEquals("hello", new String(buffer, offset, numBytes));
+
+               // verify the area after the written region.
+               for (int i = offset + numBytes; i < buffer.length; i++) {
+                       assertEquals(0, buffer[i]);
+               }
+       }
+
+       @Test(expected = IllegalArgumentException.class)
+       public void testReadOnlyByteBufferGet() {
 
 Review comment:
   Yes, I do not see any value in it.
   
   Btw, just a side thought. We could keep normal handling of this weird case 
(non-direct and non-array-based target buffer). The problem is that to test it 
we would have to implement a testing buffer extending `ByteBuffer`. The testing 
buffer would have a lot of methods to implement because we cannot easily extend 
any of internal `ByteBuffer` implementations. I think the effort is not worth 
while spending time atm but we can still adopt it in future if needed.

----------------------------------------------------------------
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]


With regards,
Apache Git Services

Reply via email to