Skip to content

Commit

Permalink
[rewrite] #1281 karate-gatling fixed, even the mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Nov 11, 2020
1 parent 485eaa6 commit e7b1f05
Show file tree
Hide file tree
Showing 23 changed files with 210 additions and 100 deletions.
8 changes: 3 additions & 5 deletions karate-core/src/main/java/com/intuit/karate/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void onComplete() {
public Iterator<CompletableFuture> process(Feature feature) {
CompletableFuture future = new CompletableFuture();
try {
FeatureRuntime fr = new FeatureRuntime(suite, feature);
FeatureRuntime fr = new FeatureRuntime(suite, feature, null);
fr.setNext(() -> {
featureResults.add(fr.result);
onFeatureDone(fr, ++index, count);
Expand Down Expand Up @@ -172,9 +172,8 @@ private static void onFeatureDone(FeatureRuntime fr, int index, int count) {

public static Map<String, Object> runFeature(Feature feature, Map<String, Object> vars, boolean evalKarateConfig) {
SuiteRuntime suite = new SuiteRuntime();
FeatureRuntime featureRuntime = new FeatureRuntime(suite, feature);
FeatureRuntime featureRuntime = new FeatureRuntime(suite, feature, vars);
featureRuntime.caller.setKarateConfigDisabled(!evalKarateConfig);
featureRuntime.setCallArg(vars);
featureRuntime.run();
FeatureResult result = featureRuntime.result;
if (result.isFailed()) {
Expand Down Expand Up @@ -204,10 +203,9 @@ public static void callAsync(String path, List<String> tags, Map<String, Object>
builder.tags = tags;
SuiteRuntime suite = new SuiteRuntime(builder); // sets tag selector
Feature feature = FileUtils.parseFeatureAndCallTag(path);
FeatureRuntime featureRuntime = new FeatureRuntime(suite, feature);
FeatureRuntime featureRuntime = new FeatureRuntime(suite, feature, arg);
featureRuntime.setPerfRuntime(perf);
featureRuntime.setNext(() -> perf.afterFeature());
featureRuntime.setCallArg(arg);
perf.submit(featureRuntime);
}

Expand Down
29 changes: 20 additions & 9 deletions karate-core/src/main/java/com/intuit/karate/core/PerfEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
* @author pthomas3
*/
public class PerfEvent {

private final String name;
private final long startTime;
private final long startTime;
private final long endTime;
private final int statusCode;
private boolean failed;

private boolean failed;
private String message;

public PerfEvent(long startTime, long endTime, String name, int statusCode) {
this.name = name;
this.startTime = startTime;
Expand All @@ -50,15 +50,15 @@ public String getName() {

public long getStartTime() {
return startTime;
}
}

public long getEndTime() {
return endTime;
}

public int getStatusCode() {
return statusCode;
}
}

public boolean isFailed() {
return failed;
Expand All @@ -75,5 +75,16 @@ public String getMessage() {
public void setMessage(String message) {
this.message = message;
}


@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[name: ").append(name);
sb.append(", startTime: ").append(startTime);
sb.append(", endTime: ").append(endTime);
sb.append(", statusCode: ").append(statusCode);
sb.append("]");
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@
import com.intuit.karate.driver.Target;
import com.intuit.karate.http.HttpLogModifier;
import com.intuit.karate.server.Cookies;
import com.intuit.karate.server.HttpClientFactory;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author pthomas3
*/
public class Config {

private static final Logger logger = LoggerFactory.getLogger(Config.class);

public static final int DEFAULT_RETRY_INTERVAL = 3000;
public static final int DEFAULT_RETRY_COUNT = 3;
Expand All @@ -65,7 +68,7 @@ public class Config {
private String localAddress;
private int responseDelay;
private boolean lowerCaseResponseHeaders = false;
private boolean corsEnabled = false;
private boolean corsEnabled = false; // TODO deprecate
private boolean logPrettyRequest;
private boolean logPrettyResponse;
private boolean printEnabled = true;
Expand Down Expand Up @@ -125,6 +128,7 @@ public boolean configure(String key, Variable value) { // TODO use enum
lowerCaseResponseHeaders = value.isTrue();
return false;
case "cors":
logger.warn("'cors' is deprecated, use server start api instead");
corsEnabled = value.isTrue();
return false;
case "logPrettyResponse":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,27 @@ public PerfHook getPerfRuntime() {
return perfRuntime;
}

public void setCallArg(Map<String, Object> arg) {
if (arg != null) {
caller.setArg(new Variable(arg));
}
}

public void setNext(Runnable next) {
this.next = next;
}

public FeatureRuntime(SuiteRuntime suite, Feature feature) {
this(suite, feature, ScenarioCall.none());
public FeatureRuntime(SuiteRuntime suite, Feature feature, Map<String, Object> arg) {
this(suite, feature, ScenarioCall.none(arg));
}

public FeatureRuntime(ScenarioCall call) {
this(call.parentRuntime.featureRuntime.suite, call.feature, call);
Variable arg = call.getArg();
result.setLoopIndex(call.getLoopIndex());
if (arg != null) {
result.setCallArg(arg.getValue());
if (call.arg != null && !call.arg.isNull()) {
result.setCallArg(call.arg.getValue());
}
}

private FeatureRuntime(SuiteRuntime suite, Feature feature, ScenarioCall parentCall) {
private FeatureRuntime(SuiteRuntime suite, Feature feature, ScenarioCall caller) {
this.suite = suite;
this.feature = feature;
this.caller = parentCall;
this.rootFeature = parentCall.isNone() ? this : parentCall.parentRuntime.featureRuntime;
this.caller = caller;
this.rootFeature = caller.isNone() ? this : caller.parentRuntime.featureRuntime;
result = new FeatureResult(suite.results, feature);
scenarios = new ScenarioGenerator(this, feature.getSections().iterator());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.intuit.karate.server.ResourceType;
import com.intuit.karate.server.Response;
import com.intuit.karate.server.ServerHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class MockHandler implements ServerHandler {
private final Feature feature;
private final String featureName;
private final ScenarioRuntime runtime; // holds global config and vars
private final Map<String, Variable> globals;

protected static final ThreadLocal<Request> LOCAL_REQUEST = new ThreadLocal<Request>();

Expand All @@ -82,8 +84,7 @@ public MockHandler(Feature feature) {
public MockHandler(Feature feature, Map<String, Object> args) {
this.feature = feature;
featureName = feature.getPath().toFile().getName();
FeatureRuntime featureRuntime = new FeatureRuntime(SuiteRuntime.forMock(), feature);
featureRuntime.setCallArg(args);
FeatureRuntime featureRuntime = new FeatureRuntime(SuiteRuntime.forMock(), feature, args);
FeatureSection section = new FeatureSection();
section.setIndex(-1); // TODO util for creating dummy scenario
Scenario dummy = new Scenario(feature, section, -1);
Expand All @@ -110,6 +111,7 @@ public MockHandler(Feature feature, Map<String, Object> args) {
}
}
runtime.logger.info("mock server initialized: {}", featureName);
globals = runtime.engine.detachVariables();
}

private static final Result PASSED = Result.passed(0);
Expand All @@ -119,7 +121,7 @@ public Response handle(Request req) {
Thread.currentThread().setContextClassLoader(runtime.featureRuntime.suite.classLoader);
LOCAL_REQUEST.set(req);
req.processBody();
ScenarioEngine engine = new ScenarioEngine(runtime);
ScenarioEngine engine = new ScenarioEngine(runtime, new HashMap(globals));
engine.setVariable(ScenarioEngine.REQUEST_URL_BASE, req.getUrlBase());
engine.setVariable(ScenarioEngine.REQUEST_URI, req.getPath());
engine.setVariable(ScenarioEngine.REQUEST_METHOD, req.getMethod());
Expand Down Expand Up @@ -160,7 +162,7 @@ public Response handle(Request req) {
break;
}
}
Map<String, Variable> vars = runtime.engine.vars;
Map<String, Variable> vars = engine.vars;
response = vars.remove(ScenarioEngine.RESPONSE);
responseStatus = vars.remove(ScenarioEngine.RESPONSE_STATUS);
responseHeaders = vars.remove(ScenarioEngine.RESPONSE_HEADERS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
import com.intuit.karate.core.Feature;
import com.intuit.karate.core.FeatureParser;
import com.intuit.karate.server.HttpServer;
import com.intuit.karate.server.ServerHandler;
import com.intuit.karate.server.HttpServerHandler;
import com.intuit.karate.server.SslContextFactory;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.server.HttpService;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.cors.CorsService;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -41,8 +42,8 @@
*/
public class MockServer extends HttpServer {

private MockServer(ServerBuilder sb, ServerHandler handler) {
super(sb, handler);
private MockServer(ServerBuilder sb) {
super(sb);
}

public static class Builder {
Expand All @@ -54,6 +55,7 @@ public static class Builder {
final Feature feature;
int port;
boolean ssl;
boolean corsEnabled;
File certFile;
File keyFile;
Map<String, Object> args;
Expand Down Expand Up @@ -92,6 +94,11 @@ public Builder arg(String name, Object value) {
return this;
}

public Builder corsEnabled() {
corsEnabled = true;
return this;
}

public MockServer build() {
ServerBuilder sb = Server.builder();
if (ssl) {
Expand All @@ -105,7 +112,12 @@ public MockServer build() {
sb.http(port);
}
MockHandler mockHandler = new MockHandler(feature, args);
return new MockServer(sb, mockHandler);
HttpService service = new HttpServerHandler(mockHandler);
if (corsEnabled) {
service = service.decorate(CorsService.builderForAnyOrigin().newDecorator());
}
sb.service("prefix:/", service);
return new MockServer(sb);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@

import com.intuit.karate.FileUtils;
import com.intuit.karate.PerfContext;
import com.intuit.karate.Resource;
import com.intuit.karate.StringUtils;
import com.intuit.karate.XmlUtils;
import com.intuit.karate.core.Feature;
import com.intuit.karate.core.PerfEvent;
import com.intuit.karate.core.Scenario;
import com.intuit.karate.data.Json;
Expand All @@ -49,7 +47,6 @@
import com.intuit.karate.shell.Command;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -723,8 +720,7 @@ public void stop(int port) {
}

public String toAbsolutePath(String relativePath) {
Resource resource = getEngine().fileReader.toResource(relativePath);
return resource.getPath().normalize().toAbsolutePath().toString();
return getEngine().fileReader.toAbsolutePath(relativePath);
}

public Object toBean(Object o, String className) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public class ScenarioCall {
public final ScenarioRuntime parentRuntime;
public final int depth;
public final Feature feature;
public final Variable arg;

private boolean callonce;
private Variable arg;

private boolean sharedScope;
private boolean karateConfigDisabled;
private int loopIndex = -1;
Expand All @@ -62,14 +63,6 @@ public boolean isSharedScope() {
return sharedScope;
}

public void setArg(Variable arg) {
this.arg = arg;
}

public Variable getArg() {
return arg;
}

public boolean isCallonce() {
return callonce;
}
Expand All @@ -85,19 +78,20 @@ public void setKarateConfigDisabled(boolean karateConfigDisabled) {
public boolean isKarateConfigDisabled() {
return karateConfigDisabled;
}
public static ScenarioCall none() {
return new ScenarioCall(null, null);

public static ScenarioCall none(Map<String, Object> arg) {
return new ScenarioCall(null, null, arg == null ? null : new Variable(arg));
}

public ScenarioCall(ScenarioRuntime parentRuntime, Feature feature) {
public ScenarioCall(ScenarioRuntime parentRuntime, Feature feature, Variable arg) {
this.parentRuntime = parentRuntime;
this.feature = feature;
if (parentRuntime == null) {
depth = 0;
} else {
depth = parentRuntime.caller.depth + 1;
}
this.arg = arg;
}

public static class Result {
Expand Down
Loading

0 comments on commit e7b1f05

Please sign in to comment.