forked from karatelabs/karate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockUtils.java
executable file
·46 lines (37 loc) · 1.53 KB
/
MockUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package mock;
import com.intuit.karate.core.MockServer;
import com.intuit.karate.PerfContext;
import com.intuit.karate.Runner;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* @author pthomas3
*/
public class MockUtils {
public static void startServer() {
MockServer server = MockServer.feature("classpath:mock/mock.feature").build();
System.setProperty("mock.cats.url", "http://localhost:" + server.getPort() + "/cats");
}
private static final List<String> catNames = (List) Runner.runFeature("classpath:mock/feeder.feature", null, false).get("names");
private static final AtomicInteger counter = new AtomicInteger();
public static String getNextCatName() {
return catNames.get(counter.getAndIncrement() % catNames.size());
}
public static Map<String, Object> myRpc(Map<String, Object> map, PerfContext context) {
long startTime = System.currentTimeMillis();
// this is just an example, you can put any kind of code here
int sleepTime = (Integer) map.get("sleep");
try {
Thread.sleep(sleepTime);
} catch (Exception e) {
throw new RuntimeException(e);
}
long endTime = System.currentTimeMillis();
// and here is where you send the performance data to the reporting engine
context.capturePerfEvent("myRpc-" + sleepTime, startTime, endTime);
return Collections.singletonMap("success", true);
}
}