diff --git a/README.md b/README.md index 032b232..003d351 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ - [清空 Flow 数据](#清空-Flow-数据) - [获取指定频道的消息总线数据](#获取指定频道的消息总线数据) - [获取Socket对象进行事件监听](#获取Socket对象进行事件监听) + - [设置带宽限速](#设置带宽限速) - [应用场景](#应用场景) - [在UI自动化中校验请求参数是否符合预期](#在UI自动化中校验请求参数是否符合预期) - [在UI自动化中校验返回与客户端展示是否一致](#在UI自动化中校验返回与客户端展示是否一致) @@ -409,6 +410,23 @@ socket.connect(); socket.disconnect(); ``` +### 设置带宽限速 + +[Lyrebird 获取/设置网络带宽限速接口文档](https://meituan-dianping.github.io/lyrebird/guide/api.html#%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E7%BD%91%E7%BB%9C%E5%B8%A6%E5%AE%BD%E9%99%90%E9%80%9F) + +```java +// 设置带宽限速为2G +lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_2G); +// 设置带宽限速为2.5G +lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_2_5G); +// 设置带宽限速为3G +lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_3G); +// 设置带宽不限速 +lyrebird.setSpeedLimit(Bandwidth.UNLIMITED); +// 获取带宽速度 +int bandwidth = lyrebird.getSpeedLimit().getBandwidth(); +``` + ## 应用场景 在UI自动化中,可将移动设备通过代理的方式将请求数据接入Lyrebird,[操作指南](https://github.com/Meituan-Dianping/lyrebird#连接移动设备),在测试用例中通过调用Lyrebird API来校验网络请求参数是否符合预期。 diff --git a/pom.xml b/pom.xml index 6df377e..11907b9 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.meituan-dianping.lyrebird.sdk lyrebird-java-client - 1.1.1 + 1.1.2 lyrebird-java-client https://github.com/Meituan-Dianping/lyrebird-java-client diff --git a/src/main/java/com/meituan/lyrebird/Lyrebird.java b/src/main/java/com/meituan/lyrebird/Lyrebird.java index 58e9b75..98adb07 100644 --- a/src/main/java/com/meituan/lyrebird/Lyrebird.java +++ b/src/main/java/com/meituan/lyrebird/Lyrebird.java @@ -1,5 +1,7 @@ package com.meituan.lyrebird; +import com.meituan.lyrebird.client.api.bandwidth.Bandwidth; +import com.meituan.lyrebird.client.api.bandwidth.SpeedLimit; import io.socket.client.Socket; import java.lang.reflect.Method; @@ -153,6 +155,26 @@ public LBMockData getMockData(String dataId) throws LyrebirdClientException { return client.getMockData(dataId); } + /** + * set the speed limit + * + * @param bandwidth BANDWIDTH_2G对应2G;BANDWIDTH_2_5G对应2.5G;BANDWIDTH_3G对应3G;UNLIMITED对应无限制 + * @throws LyrebirdClientException + */ + public void setSpeedLimit(Bandwidth bandwidth) throws LyrebirdClientException { + client.setSpeedLimit(bandwidth); + } + + /** + * get the speed limit bandwidth + * + * @return + * @throws LyrebirdClientException + */ + public SpeedLimit getSpeedLimit() throws LyrebirdClientException { + return client.getSpeedLimit(); + } + /** * Get an object of socket io * diff --git a/src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java b/src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java index 430c575..a1adf2e 100644 --- a/src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java +++ b/src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java @@ -1,5 +1,8 @@ package com.meituan.lyrebird.client; +import com.meituan.lyrebird.client.api.bandwidth.Bandwidth; +import com.meituan.lyrebird.client.api.bandwidth.BandwidthTemplate; +import com.meituan.lyrebird.client.api.bandwidth.SpeedLimit; import com.meituan.lyrebird.client.exceptions.LyrebirdClientException; import com.meituan.lyrebird.client.api.*; import io.socket.client.IO; @@ -197,6 +200,42 @@ public LBMockData getMockData(String dataId) throws LyrebirdClientException { } } + /** + * set the speed limit + * + * @param bandwidth an enum of Bandwidth + * @throws LyrebirdClientException + */ + public void setSpeedLimit(Bandwidth bandwidth) throws LyrebirdClientException { + BandwidthTemplate bandwidthTemplate = new BandwidthTemplate(bandwidth); + BaseResponse resp; + try{ + resp = lyrebirdService.setSpeedLimit(bandwidthTemplate).execute().body(); + } catch (IOException e) { + throw new LyrebirdClientException("Catch exception while set the speed limit", e); + } + if (resp == null) { + throw new LyrebirdClientException("Got none response from the speed limit request"); + } + if (resp.getCode() != 1000) { + throw new LyrebirdClientException(resp.getMessage()); + } + } + + /** + * get the speed limit bandwidth + * + * @return + * @throws LyrebirdClientException + */ + public SpeedLimit getSpeedLimit() throws LyrebirdClientException { + try { + return lyrebirdService.getSpeedLimit().execute().body(); + } catch (IOException e) { + throw new LyrebirdClientException("Catch exception while get speed limit bandwidth.", e); + } + } + /** * Get an object of socket io * diff --git a/src/main/java/com/meituan/lyrebird/client/LyrebirdService.java b/src/main/java/com/meituan/lyrebird/client/LyrebirdService.java index a6b989a..68f3d7d 100644 --- a/src/main/java/com/meituan/lyrebird/client/LyrebirdService.java +++ b/src/main/java/com/meituan/lyrebird/client/LyrebirdService.java @@ -2,6 +2,8 @@ import com.meituan.lyrebird.client.api.*; +import com.meituan.lyrebird.client.api.bandwidth.BandwidthTemplate; +import com.meituan.lyrebird.client.api.bandwidth.SpeedLimit; import retrofit2.Call; import retrofit2.http.*; @@ -30,4 +32,11 @@ public interface LyrebirdService { @GET("api/data/{dataId}") Call getMockData(@Path("dataId") String dataId); + + @Headers("Content-Type: application/json") + @PUT("api/bandwidth") + Call setSpeedLimit(@Body BandwidthTemplate template); + + @GET("api/bandwidth") + Call getSpeedLimit(); } diff --git a/src/main/java/com/meituan/lyrebird/client/api/bandwidth/Bandwidth.java b/src/main/java/com/meituan/lyrebird/client/api/bandwidth/Bandwidth.java new file mode 100644 index 0000000..11d918b --- /dev/null +++ b/src/main/java/com/meituan/lyrebird/client/api/bandwidth/Bandwidth.java @@ -0,0 +1,8 @@ +package com.meituan.lyrebird.client.api.bandwidth; + +/** + * 网络带宽枚举 + */ +public enum Bandwidth { + BANDWIDTH_2G, BANDWIDTH_2_5G, BANDWIDTH_3G, UNLIMITED +} diff --git a/src/main/java/com/meituan/lyrebird/client/api/bandwidth/BandwidthTemplate.java b/src/main/java/com/meituan/lyrebird/client/api/bandwidth/BandwidthTemplate.java new file mode 100644 index 0000000..212a83a --- /dev/null +++ b/src/main/java/com/meituan/lyrebird/client/api/bandwidth/BandwidthTemplate.java @@ -0,0 +1,30 @@ +package com.meituan.lyrebird.client.api.bandwidth; + +public class BandwidthTemplate { + private String templateName; + + public BandwidthTemplate(Bandwidth bandwidth) { + switch (bandwidth) { + case BANDWIDTH_2G: + templateName = "2G"; + break; + case BANDWIDTH_2_5G: + templateName = "2.5G"; + break; + case BANDWIDTH_3G: + templateName = "3G"; + break; + default: + templateName = "UNLIMITED"; + break; + } + } + + public String getTemplateName() { + return templateName; + } + + public void setTemplateName(String templateName) { + this.templateName = templateName; + } +} diff --git a/src/main/java/com/meituan/lyrebird/client/api/bandwidth/SpeedLimit.java b/src/main/java/com/meituan/lyrebird/client/api/bandwidth/SpeedLimit.java new file mode 100644 index 0000000..0838194 --- /dev/null +++ b/src/main/java/com/meituan/lyrebird/client/api/bandwidth/SpeedLimit.java @@ -0,0 +1,15 @@ +package com.meituan.lyrebird.client.api.bandwidth; + +import com.meituan.lyrebird.client.api.BaseResponse; + +public class SpeedLimit extends BaseResponse { + private int bandwidth; + + public int getBandwidth() { + return bandwidth; + } + + public void setBandwidth(int bandwidth) { + this.bandwidth = bandwidth; + } +} diff --git a/src/test/java/com/meituan/lyrebird/test/TestFunctional.java b/src/test/java/com/meituan/lyrebird/test/TestFunctional.java index c1d67ed..beb6078 100644 --- a/src/test/java/com/meituan/lyrebird/test/TestFunctional.java +++ b/src/test/java/com/meituan/lyrebird/test/TestFunctional.java @@ -4,6 +4,7 @@ import com.jayway.jsonpath.JsonPath; import com.meituan.lyrebird.Lyrebird; import com.meituan.lyrebird.client.api.*; +import com.meituan.lyrebird.client.api.bandwidth.Bandwidth; import com.meituan.lyrebird.client.exceptions.LyrebirdClientException; import java.util.List; import okhttp3.mockwebserver.*; @@ -171,10 +172,10 @@ public void testEventList() throws LyrebirdClientException, InterruptedException @Test public void testLBMockData() throws LyrebirdClientException { - this.mockServer.enqueue( - new MockResponse() + this.mockServer.enqueue(new MockResponse() .setBody( - "{\"code\": 1000,\"data\": {\"id\": \"cfa0c589-8ef0-4885-b4f4-b9688c5af0d5\", \"name\": \"test-data\", \"response\": {\"data\": \"[{\\\"type\\\": \\\"scheme\\\", \\\"info\\\":{\\\"value\\\": \\\"test://www.lyrebird.java.sdk.com\\\"}, \\\"desc\\\": \\\"The scheme of target page\\\"}]\"}}, \"message\": \"success\"}")); + "{\"code\": 1000,\"data\": {\"id\": \"cfa0c589-8ef0-4885-b4f4-b9688c5af0d5\", \"name\": \"test-data\", \"response\": {\"data\": \"[{\\\"type\\\": \\\"scheme\\\", \\\"info\\\":{\\\"value\\\": \\\"test://www.lyrebird.java.sdk.com\\\"}, \\\"desc\\\": \\\"The scheme of target page\\\"}]\"}}, \"message\": \"success\"}" + )); LBMockData lbMockData = this.lyrebird.getMockData("cfa0c589-8ef0-4885-b4f4-b9688c5af0d5"); assertEquals("cfa0c589-8ef0-4885-b4f4-b9688c5af0d5", lbMockData.getId()); @@ -184,4 +185,68 @@ public void testLBMockData() throws LyrebirdClientException { assertEquals(1, urlScheme.size()); assertEquals("test://www.lyrebird.java.sdk.com", urlScheme.get(0)); } + + @Test + public void test2GSpeedLimit() throws LyrebirdClientException, InterruptedException { + this.mockServer.enqueue(new MockResponse() + .setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 10}" + )); + lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_2G); + RecordedRequest request = this.mockServer.takeRequest(); + assertEquals("{\"templateName\":\"2G\"}", request.getBody().readUtf8()); + + this.mockServer.enqueue(new MockResponse() + .setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 10}" + )); + int bandwidth = lyrebird.getSpeedLimit().getBandwidth(); + assertEquals(10, bandwidth); + } + + @Test + public void test25GSpeedLimit() throws LyrebirdClientException, InterruptedException { + this.mockServer.enqueue(new MockResponse() + .setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 35}" + )); + lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_2_5G); + RecordedRequest request = this.mockServer.takeRequest(); + assertEquals("{\"templateName\":\"2.5G\"}", request.getBody().readUtf8()); + + this.mockServer.enqueue(new MockResponse() + .setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 35}" + )); + int bandwidth = lyrebird.getSpeedLimit().getBandwidth(); + assertEquals(35, bandwidth); + } + + @Test + public void test3GSpeedLimit() throws LyrebirdClientException, InterruptedException { + this.mockServer.enqueue(new MockResponse() + .setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 120}" + )); + lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_3G); + RecordedRequest request = this.mockServer.takeRequest(); + assertEquals("{\"templateName\":\"3G\"}", request.getBody().readUtf8()); + + this.mockServer.enqueue(new MockResponse() + .setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 120}" + )); + int bandwidth = lyrebird.getSpeedLimit().getBandwidth(); + assertEquals(120, bandwidth); + } + + @Test + public void testUnlimitedSpeedLimit() throws LyrebirdClientException, InterruptedException { + this.mockServer.enqueue(new MockResponse() + .setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": -1}" + )); + lyrebird.setSpeedLimit(Bandwidth.UNLIMITED); + RecordedRequest request = this.mockServer.takeRequest(); + assertEquals("{\"templateName\":\"UNLIMITED\"}", request.getBody().readUtf8()); + + this.mockServer.enqueue(new MockResponse() + .setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": -1}" + )); + int bandwidth = lyrebird.getSpeedLimit().getBandwidth(); + assertEquals(-1, bandwidth); + } }