Fix the numeric overflow bug of ping response data in the cluster module (#844)
- Change type of cluster ping data response from byte to int. This change is compatible with old versions Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
56e463e1fe
commit
7997cf65fc
|
|
@ -53,5 +53,10 @@
|
|||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -27,9 +27,14 @@ public class PingResponseDataDecoder implements EntityDecoder<ByteBuf, Integer>
|
|||
|
||||
@Override
|
||||
public Integer decode(ByteBuf source) {
|
||||
if (source.readableBytes() >= 1) {
|
||||
int size = source.readableBytes();
|
||||
if (size == 1) {
|
||||
// Compatible with old version (< 1.7.0).
|
||||
return (int) source.readByte();
|
||||
}
|
||||
if (size >= 4) {
|
||||
return source.readInt();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed 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 com.alibaba.csp.sentinel.cluster.client.codec.data;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Eric Zhao
|
||||
*/
|
||||
public class PingResponseDataDecoderTest {
|
||||
|
||||
@Test
|
||||
public void testDecodePingResponseData() {
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
PingResponseDataDecoder decoder = new PingResponseDataDecoder();
|
||||
|
||||
int big = Integer.MAX_VALUE;
|
||||
buf.writeInt(big);
|
||||
assertThat(decoder.decode(buf)).isEqualTo(big);
|
||||
|
||||
byte small = 12;
|
||||
buf.writeByte(small);
|
||||
assertThat(decoder.decode(buf)).isEqualTo(small);
|
||||
|
||||
buf.release();
|
||||
}
|
||||
}
|
||||
|
|
@ -52,5 +52,10 @@
|
|||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -31,6 +31,6 @@ public class PingResponseDataWriter implements EntityWriter<Integer, ByteBuf> {
|
|||
if (entity == null || target == null) {
|
||||
return;
|
||||
}
|
||||
target.writeByte(entity);
|
||||
target.writeInt(entity);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright 1999-2019 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed 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 com.alibaba.csp.sentinel.cluster.server.codec.data;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test cases for {@link PingResponseDataWriter}.
|
||||
*
|
||||
* @author Eric Zhao
|
||||
*/
|
||||
public class PingResponseDataWriterTest {
|
||||
|
||||
@Test
|
||||
public void testWritePingResponseAndParse() {
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
PingResponseDataWriter writer = new PingResponseDataWriter();
|
||||
|
||||
int small = 120;
|
||||
writer.writeTo(small, buf);
|
||||
assertThat(buf.readableBytes()).isGreaterThanOrEqualTo(4);
|
||||
assertThat(buf.readInt()).isEqualTo(small);
|
||||
|
||||
int big = Integer.MAX_VALUE;
|
||||
writer.writeTo(big, buf);
|
||||
assertThat(buf.readableBytes()).isGreaterThanOrEqualTo(4);
|
||||
assertThat(buf.readInt()).isEqualTo(big);
|
||||
|
||||
buf.release();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue