-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug of calculating param size and amount in ParamFlowRequestDataW…
…riter of Sentinel cluster (#495) Signed-off-by: Eric Zhao <sczyh16@gmail.com>
- Loading branch information
Showing
3 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...va/com/alibaba/csp/sentinel/cluster/client/codec/data/ParamFlowRequestDataWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.alibaba.csp.sentinel.cluster.client.codec.data; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author Eric Zhao | ||
*/ | ||
public class ParamFlowRequestDataWriterTest { | ||
|
||
@Test | ||
public void testCalculateParamTransportSize() { | ||
ParamFlowRequestDataWriter writer = new ParamFlowRequestDataWriter(); | ||
// POJO (non-primitive type) should not be regarded as a valid parameter. | ||
assertEquals(0, writer.calculateParamTransportSize(new SomePojo().setParam1("abc"))); | ||
|
||
assertEquals(4 + 1, writer.calculateParamTransportSize(1)); | ||
assertEquals(1 + 1, writer.calculateParamTransportSize((byte) 1)); | ||
assertEquals(1 + 1, writer.calculateParamTransportSize(false)); | ||
assertEquals(8 + 1, writer.calculateParamTransportSize(2L)); | ||
assertEquals(8 + 1, writer.calculateParamTransportSize(4.0d)); | ||
final String paramStr = "Sentinel"; | ||
assertEquals(1 + 4 + paramStr.getBytes().length, writer.calculateParamTransportSize(paramStr)); | ||
} | ||
|
||
@Test | ||
public void testCalculateParamAmountExceedsMaxSize() { | ||
final int maxSize = 10; | ||
ParamFlowRequestDataWriter writer = new ParamFlowRequestDataWriter(maxSize); | ||
assertEquals(1, writer.calculateParamAmount(new ArrayList<Object>() {{ | ||
add(1); | ||
}})); | ||
assertEquals(2, writer.calculateParamAmount(new ArrayList<Object>() {{ | ||
add(1); add(64); | ||
}})); | ||
assertEquals(2, writer.calculateParamAmount(new ArrayList<Object>() {{ | ||
add(1); add(64); add(3); | ||
}})); | ||
} | ||
|
||
@Test | ||
public void testCalculateParamAmount() { | ||
ParamFlowRequestDataWriter writer = new ParamFlowRequestDataWriter(); | ||
assertEquals(6, writer.calculateParamAmount(new ArrayList<Object>() {{ | ||
add(1); add(1d); add(1f); add((byte) 1); add("123"); add(true); | ||
}})); | ||
// POJO (non-primitive type) should not be regarded as a valid parameter. | ||
assertEquals(0, writer.calculateParamAmount(new ArrayList<Object>() {{ | ||
add(new SomePojo()); | ||
}})); | ||
assertEquals(1, writer.calculateParamAmount(new ArrayList<Object>() {{ | ||
add(new Object()); add(1); | ||
}})); | ||
} | ||
|
||
private static class SomePojo { | ||
private String param1; | ||
|
||
public String getParam1() { | ||
return param1; | ||
} | ||
|
||
public SomePojo setParam1(String param1) { | ||
this.param1 = param1; | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SomePojo{" + | ||
"param1='" + param1 + '\'' + | ||
'}'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters