@@ -47,8 +47,8 @@ public void testReadFramesResetMessageSize() throws TTransportException {
4747 .put (secondFrame )
4848 .flip ();
4949 TConfiguration configuration =
50- TConfiguration .custom ().setMaxMessageSize (10 ).setMaxFrameSize (10 ).build ();
51- TMemoryBuffer underlying = new TMemoryBuffer (configuration , 10 );
50+ TConfiguration .custom ().setMaxMessageSize (14 ).setMaxFrameSize (10 ).build ();
51+ TMemoryBuffer underlying = new BudgetConsumingMemoryBuffer (configuration , 14 );
5252 underlying .write (framedData .array ());
5353 TElasticFramedTransport transport =
5454 new TElasticFramedTransport (underlying , 4 , configuration .getMaxFrameSize (), false );
@@ -78,16 +78,17 @@ public void testReadCompressedFramesResetMessageSize() throws TTransportExceptio
7878 int firstCompressedSize = frameSizes .getInt ();
7979 frameSizes .position (Integer .BYTES + firstCompressedSize );
8080 int secondCompressedSize = frameSizes .getInt ();
81- int maxMessageSize = Integer . BYTES + Math .max (firstCompressedSize , secondCompressedSize );
81+ int maxFrameSize = Math .max (firstCompressedSize , secondCompressedSize );
8282 TConfiguration configuration =
8383 TConfiguration .custom ()
84- .setMaxMessageSize (maxMessageSize )
85- .setMaxFrameSize (maxMessageSize )
84+ .setMaxMessageSize (maxFrameSize )
85+ .setMaxFrameSize (maxFrameSize )
8686 .build ();
87- TMemoryBuffer underlying = new TMemoryBuffer (configuration , maxMessageSize );
87+ TMemoryBuffer underlying = new BudgetConsumingMemoryBuffer (configuration , maxFrameSize );
8888 underlying .write (framedData );
8989 TSnappyElasticFramedTransport input =
9090 new TSnappyElasticFramedTransport (underlying , 4 , configuration .getMaxFrameSize (), false );
91+ assertEquals (Integer .BYTES + maxFrameSize , configuration .getMaxMessageSize ());
9192
9293 byte [] actualFirstFrame = new byte [firstFrame .length ];
9394 input .readAll (actualFirstFrame , 0 , actualFirstFrame .length );
@@ -98,6 +99,39 @@ public void testReadCompressedFramesResetMessageSize() throws TTransportExceptio
9899 assertArrayEquals (secondFrame , actualSecondFrame );
99100 }
100101
102+ @ Test
103+ public void testFrameSizeAlignsUnderlyingMessageSize () throws TTransportException {
104+ byte [] frame = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
105+ ByteBuffer framedData =
106+ ByteBuffer .allocate (Integer .BYTES + frame .length ).putInt (frame .length ).put (frame ).flip ();
107+ TConfiguration configuration =
108+ TConfiguration .custom ().setMaxMessageSize (6 ).setMaxFrameSize (frame .length ).build ();
109+ TMemoryBuffer underlying = new BudgetConsumingMemoryBuffer (configuration , 6 );
110+ underlying .write (framedData .array ());
111+ TElasticFramedTransport transport =
112+ new TElasticFramedTransport (underlying , 4 , frame .length , false );
113+
114+ assertEquals (Integer .BYTES + frame .length , configuration .getMaxMessageSize ());
115+ byte [] actualFrame = new byte [frame .length ];
116+ transport .readAll (actualFrame , 0 , actualFrame .length );
117+ assertArrayEquals (frame , actualFrame );
118+ }
119+
120+ private static class BudgetConsumingMemoryBuffer extends TMemoryBuffer {
121+
122+ private BudgetConsumingMemoryBuffer (TConfiguration configuration , int size )
123+ throws TTransportException {
124+ super (configuration , size );
125+ }
126+
127+ @ Override
128+ public int read (byte [] buffer , int offset , int length ) throws TTransportException {
129+ int bytesRead = super .read (buffer , offset , length );
130+ countConsumedMessageBytes (bytesRead );
131+ return bytesRead ;
132+ }
133+ }
134+
101135 @ Test
102136 public void testSingularSize () {
103137
0 commit comments