Skip to content

Commit 8811806

Browse files
authored
Merge pull request #19 from yury-awesome/feature/新增限速支持
Feature/新增限速支持
2 parents e2a3f3c + 00f8c78 commit 8811806

File tree

9 files changed

+210
-4
lines changed

9 files changed

+210
-4
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [清空 Flow 数据](#清空-Flow-数据)
3333
- [获取指定频道的消息总线数据](#获取指定频道的消息总线数据)
3434
- [获取Socket对象进行事件监听](#获取Socket对象进行事件监听)
35+
- [设置带宽限速](#设置带宽限速)
3536
- [应用场景](#应用场景)
3637
- [在UI自动化中校验请求参数是否符合预期](#在UI自动化中校验请求参数是否符合预期)
3738
- [在UI自动化中校验返回与客户端展示是否一致](#在UI自动化中校验返回与客户端展示是否一致)
@@ -409,6 +410,23 @@ socket.connect();
409410
socket.disconnect();
410411
```
411412

413+
### 设置带宽限速
414+
415+
[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)
416+
417+
```java
418+
// 设置带宽限速为2G
419+
lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_2G);
420+
// 设置带宽限速为2.5G
421+
lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_2_5G);
422+
// 设置带宽限速为3G
423+
lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_3G);
424+
// 设置带宽不限速
425+
lyrebird.setSpeedLimit(Bandwidth.UNLIMITED);
426+
// 获取带宽速度
427+
int bandwidth = lyrebird.getSpeedLimit().getBandwidth();
428+
```
429+
412430
## 应用场景
413431

414432
在UI自动化中,可将移动设备通过代理的方式将请求数据接入Lyrebird,[操作指南](https://github.com/Meituan-Dianping/lyrebird#连接移动设备),在测试用例中通过调用Lyrebird API来校验网络请求参数是否符合预期。

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.meituan-dianping.lyrebird.sdk</groupId>
77
<artifactId>lyrebird-java-client</artifactId>
8-
<version>1.1.1</version>
8+
<version>1.1.2</version>
99

1010
<name>lyrebird-java-client</name>
1111
<url>https://github.com/Meituan-Dianping/lyrebird-java-client</url>

src/main/java/com/meituan/lyrebird/Lyrebird.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.meituan.lyrebird;
22

3+
import com.meituan.lyrebird.client.api.bandwidth.Bandwidth;
4+
import com.meituan.lyrebird.client.api.bandwidth.SpeedLimit;
35
import io.socket.client.Socket;
46
import java.lang.reflect.Method;
57

@@ -153,6 +155,26 @@ public LBMockData getMockData(String dataId) throws LyrebirdClientException {
153155
return client.getMockData(dataId);
154156
}
155157

158+
/**
159+
* set the speed limit
160+
*
161+
* @param bandwidth BANDWIDTH_2G对应2G;BANDWIDTH_2_5G对应2.5G;BANDWIDTH_3G对应3G;UNLIMITED对应无限制
162+
* @throws LyrebirdClientException
163+
*/
164+
public void setSpeedLimit(Bandwidth bandwidth) throws LyrebirdClientException {
165+
client.setSpeedLimit(bandwidth);
166+
}
167+
168+
/**
169+
* get the speed limit bandwidth
170+
*
171+
* @return
172+
* @throws LyrebirdClientException
173+
*/
174+
public SpeedLimit getSpeedLimit() throws LyrebirdClientException {
175+
return client.getSpeedLimit();
176+
}
177+
156178
/**
157179
* Get an object of socket io
158180
*

src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java

+39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.meituan.lyrebird.client;
22

3+
import com.meituan.lyrebird.client.api.bandwidth.Bandwidth;
4+
import com.meituan.lyrebird.client.api.bandwidth.BandwidthTemplate;
5+
import com.meituan.lyrebird.client.api.bandwidth.SpeedLimit;
36
import com.meituan.lyrebird.client.exceptions.LyrebirdClientException;
47
import com.meituan.lyrebird.client.api.*;
58
import io.socket.client.IO;
@@ -197,6 +200,42 @@ public LBMockData getMockData(String dataId) throws LyrebirdClientException {
197200
}
198201
}
199202

203+
/**
204+
* set the speed limit
205+
*
206+
* @param bandwidth an enum of Bandwidth
207+
* @throws LyrebirdClientException
208+
*/
209+
public void setSpeedLimit(Bandwidth bandwidth) throws LyrebirdClientException {
210+
BandwidthTemplate bandwidthTemplate = new BandwidthTemplate(bandwidth);
211+
BaseResponse resp;
212+
try{
213+
resp = lyrebirdService.setSpeedLimit(bandwidthTemplate).execute().body();
214+
} catch (IOException e) {
215+
throw new LyrebirdClientException("Catch exception while set the speed limit", e);
216+
}
217+
if (resp == null) {
218+
throw new LyrebirdClientException("Got none response from the speed limit request");
219+
}
220+
if (resp.getCode() != 1000) {
221+
throw new LyrebirdClientException(resp.getMessage());
222+
}
223+
}
224+
225+
/**
226+
* get the speed limit bandwidth
227+
*
228+
* @return
229+
* @throws LyrebirdClientException
230+
*/
231+
public SpeedLimit getSpeedLimit() throws LyrebirdClientException {
232+
try {
233+
return lyrebirdService.getSpeedLimit().execute().body();
234+
} catch (IOException e) {
235+
throw new LyrebirdClientException("Catch exception while get speed limit bandwidth.", e);
236+
}
237+
}
238+
200239
/**
201240
* Get an object of socket io
202241
*

src/main/java/com/meituan/lyrebird/client/LyrebirdService.java

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.meituan.lyrebird.client.api.*;
44

5+
import com.meituan.lyrebird.client.api.bandwidth.BandwidthTemplate;
6+
import com.meituan.lyrebird.client.api.bandwidth.SpeedLimit;
57
import retrofit2.Call;
68
import retrofit2.http.*;
79

@@ -30,4 +32,11 @@ public interface LyrebirdService {
3032

3133
@GET("api/data/{dataId}")
3234
Call<LBMockData> getMockData(@Path("dataId") String dataId);
35+
36+
@Headers("Content-Type: application/json")
37+
@PUT("api/bandwidth")
38+
Call<SpeedLimit> setSpeedLimit(@Body BandwidthTemplate template);
39+
40+
@GET("api/bandwidth")
41+
Call<SpeedLimit> getSpeedLimit();
3342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.meituan.lyrebird.client.api.bandwidth;
2+
3+
/**
4+
* 网络带宽枚举
5+
*/
6+
public enum Bandwidth {
7+
BANDWIDTH_2G, BANDWIDTH_2_5G, BANDWIDTH_3G, UNLIMITED
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.meituan.lyrebird.client.api.bandwidth;
2+
3+
public class BandwidthTemplate {
4+
private String templateName;
5+
6+
public BandwidthTemplate(Bandwidth bandwidth) {
7+
switch (bandwidth) {
8+
case BANDWIDTH_2G:
9+
templateName = "2G";
10+
break;
11+
case BANDWIDTH_2_5G:
12+
templateName = "2.5G";
13+
break;
14+
case BANDWIDTH_3G:
15+
templateName = "3G";
16+
break;
17+
default:
18+
templateName = "UNLIMITED";
19+
break;
20+
}
21+
}
22+
23+
public String getTemplateName() {
24+
return templateName;
25+
}
26+
27+
public void setTemplateName(String templateName) {
28+
this.templateName = templateName;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.meituan.lyrebird.client.api.bandwidth;
2+
3+
import com.meituan.lyrebird.client.api.BaseResponse;
4+
5+
public class SpeedLimit extends BaseResponse {
6+
private int bandwidth;
7+
8+
public int getBandwidth() {
9+
return bandwidth;
10+
}
11+
12+
public void setBandwidth(int bandwidth) {
13+
this.bandwidth = bandwidth;
14+
}
15+
}

src/test/java/com/meituan/lyrebird/test/TestFunctional.java

+68-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jayway.jsonpath.JsonPath;
55
import com.meituan.lyrebird.Lyrebird;
66
import com.meituan.lyrebird.client.api.*;
7+
import com.meituan.lyrebird.client.api.bandwidth.Bandwidth;
78
import com.meituan.lyrebird.client.exceptions.LyrebirdClientException;
89
import java.util.List;
910
import okhttp3.mockwebserver.*;
@@ -171,10 +172,10 @@ public void testEventList() throws LyrebirdClientException, InterruptedException
171172

172173
@Test
173174
public void testLBMockData() throws LyrebirdClientException {
174-
this.mockServer.enqueue(
175-
new MockResponse()
175+
this.mockServer.enqueue(new MockResponse()
176176
.setBody(
177-
"{\"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\"}"));
177+
"{\"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\"}"
178+
));
178179

179180
LBMockData lbMockData = this.lyrebird.getMockData("cfa0c589-8ef0-4885-b4f4-b9688c5af0d5");
180181
assertEquals("cfa0c589-8ef0-4885-b4f4-b9688c5af0d5", lbMockData.getId());
@@ -184,4 +185,68 @@ public void testLBMockData() throws LyrebirdClientException {
184185
assertEquals(1, urlScheme.size());
185186
assertEquals("test://www.lyrebird.java.sdk.com", urlScheme.get(0));
186187
}
188+
189+
@Test
190+
public void test2GSpeedLimit() throws LyrebirdClientException, InterruptedException {
191+
this.mockServer.enqueue(new MockResponse()
192+
.setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 10}"
193+
));
194+
lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_2G);
195+
RecordedRequest request = this.mockServer.takeRequest();
196+
assertEquals("{\"templateName\":\"2G\"}", request.getBody().readUtf8());
197+
198+
this.mockServer.enqueue(new MockResponse()
199+
.setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 10}"
200+
));
201+
int bandwidth = lyrebird.getSpeedLimit().getBandwidth();
202+
assertEquals(10, bandwidth);
203+
}
204+
205+
@Test
206+
public void test25GSpeedLimit() throws LyrebirdClientException, InterruptedException {
207+
this.mockServer.enqueue(new MockResponse()
208+
.setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 35}"
209+
));
210+
lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_2_5G);
211+
RecordedRequest request = this.mockServer.takeRequest();
212+
assertEquals("{\"templateName\":\"2.5G\"}", request.getBody().readUtf8());
213+
214+
this.mockServer.enqueue(new MockResponse()
215+
.setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 35}"
216+
));
217+
int bandwidth = lyrebird.getSpeedLimit().getBandwidth();
218+
assertEquals(35, bandwidth);
219+
}
220+
221+
@Test
222+
public void test3GSpeedLimit() throws LyrebirdClientException, InterruptedException {
223+
this.mockServer.enqueue(new MockResponse()
224+
.setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 120}"
225+
));
226+
lyrebird.setSpeedLimit(Bandwidth.BANDWIDTH_3G);
227+
RecordedRequest request = this.mockServer.takeRequest();
228+
assertEquals("{\"templateName\":\"3G\"}", request.getBody().readUtf8());
229+
230+
this.mockServer.enqueue(new MockResponse()
231+
.setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": 120}"
232+
));
233+
int bandwidth = lyrebird.getSpeedLimit().getBandwidth();
234+
assertEquals(120, bandwidth);
235+
}
236+
237+
@Test
238+
public void testUnlimitedSpeedLimit() throws LyrebirdClientException, InterruptedException {
239+
this.mockServer.enqueue(new MockResponse()
240+
.setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": -1}"
241+
));
242+
lyrebird.setSpeedLimit(Bandwidth.UNLIMITED);
243+
RecordedRequest request = this.mockServer.takeRequest();
244+
assertEquals("{\"templateName\":\"UNLIMITED\"}", request.getBody().readUtf8());
245+
246+
this.mockServer.enqueue(new MockResponse()
247+
.setBody("{\"code\": 1000, \"message\": \"success\", \"bandwidth\": -1}"
248+
));
249+
int bandwidth = lyrebird.getSpeedLimit().getBandwidth();
250+
assertEquals(-1, bandwidth);
251+
}
187252
}

0 commit comments

Comments
 (0)