Skip to content

Commit

Permalink
Merge pull request #19 from yury-awesome/feature/新增限速支持
Browse files Browse the repository at this point in the history
Feature/新增限速支持
  • Loading branch information
yury-awesome authored Mar 24, 2020
2 parents e2a3f3c + 00f8c78 commit 8811806
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [清空 Flow 数据](#清空-Flow-数据)
- [获取指定频道的消息总线数据](#获取指定频道的消息总线数据)
- [获取Socket对象进行事件监听](#获取Socket对象进行事件监听)
- [设置带宽限速](#设置带宽限速)
- [应用场景](#应用场景)
- [在UI自动化中校验请求参数是否符合预期](#在UI自动化中校验请求参数是否符合预期)
- [在UI自动化中校验返回与客户端展示是否一致](#在UI自动化中校验返回与客户端展示是否一致)
Expand Down Expand Up @@ -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来校验网络请求参数是否符合预期。
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.meituan-dianping.lyrebird.sdk</groupId>
<artifactId>lyrebird-java-client</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>

<name>lyrebird-java-client</name>
<url>https://github.com/Meituan-Dianping/lyrebird-java-client</url>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/meituan/lyrebird/Lyrebird.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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
*
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -30,4 +32,11 @@ public interface LyrebirdService {

@GET("api/data/{dataId}")
Call<LBMockData> getMockData(@Path("dataId") String dataId);

@Headers("Content-Type: application/json")
@PUT("api/bandwidth")
Call<SpeedLimit> setSpeedLimit(@Body BandwidthTemplate template);

@GET("api/bandwidth")
Call<SpeedLimit> getSpeedLimit();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.meituan.lyrebird.client.api.bandwidth;

/**
* 网络带宽枚举
*/
public enum Bandwidth {
BANDWIDTH_2G, BANDWIDTH_2_5G, BANDWIDTH_3G, UNLIMITED
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
71 changes: 68 additions & 3 deletions src/test/java/com/meituan/lyrebird/test/TestFunctional.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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());
Expand All @@ -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);
}
}

0 comments on commit 8811806

Please sign in to comment.