Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactore/session context #163

Merged
merged 5 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

package eu.tsystems.mms.tic.testframework.events;

import eu.tsystems.mms.tic.testframework.report.model.context.SynchronizableContext;
import eu.tsystems.mms.tic.testframework.report.model.context.AbstractContext;

/**
* This event gets fired whenever a context should be updated.
Expand All @@ -33,13 +33,13 @@ public interface Listener {
void onContextUpdate(ContextUpdateEvent event);
}

private SynchronizableContext context;
private AbstractContext context;

public SynchronizableContext getContext() {
public AbstractContext getContext() {
return context;
}

public ContextUpdateEvent setContext(SynchronizableContext context) {
public ContextUpdateEvent setContext(AbstractContext context) {
this.context = context;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class MethodEndWorker implements MethodEndEvent.Listener, Loggable {
@Subscribe
@Override
public void onMethodEnd(MethodEndEvent event) {
ExecutionContextController.clearCurrentTestResult();

ITestResult testResult = event.getTestResult();
ITestNGMethod testMethod = event.getTestMethod();
MethodContext methodContext = event.getMethodContext();
Expand All @@ -70,5 +68,7 @@ public void onMethodEnd(MethodEndEvent event) {
if (methodContext.getStatus() != Status.FAILED) {
TesterraListener.getEventBus().post(new TestStatusUpdateEvent(methodContext));
}

ExecutionContextController.clearCurrentTestResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package eu.tsystems.mms.tic.testframework.execution.testng.worker.start;

import com.google.common.eventbus.Subscribe;
import eu.tsystems.mms.tic.testframework.annotations.NoRetry;
import eu.tsystems.mms.tic.testframework.events.MethodStartEvent;
import eu.tsystems.mms.tic.testframework.execution.testng.RetryAnalyzer;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
Expand All @@ -41,8 +40,6 @@ public class MethodStartWorker implements Loggable, MethodStartEvent.Listener {
@Override
@Subscribe
public void onMethodStart(MethodStartEvent event) {
event.getMethodContext().setThreadName();

ITestNGMethod testMethod = event.getTestMethod();
if (testMethod.isTest()) {
addRetryAnalyzer(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public void format(LogEvent event, StringBuilder toAppendTo ) {
}

// enhance with method context id
SessionContext currentSessionContext = ExecutionContextController.getCurrentSessionContext();
if (currentSessionContext != null) {
toAppendTo.append("[SCID:").append(currentSessionContext.getId()).append("]");
}
ExecutionContextController.getCurrentSessionContext().flatMap(SessionContext::getRemoteSessionId).ifPresent(s -> {
toAppendTo.append("[SCID:").append(s).append("]");
});
}

public static ContextIdsPatternConverter newInstance(String[] options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

public abstract class AbstractContext implements SynchronizableContext, Loggable {
protected String name;
public abstract class AbstractContext implements Loggable {
private String name;
private final String id = IDUtils.getB64encXID();
protected AbstractContext parentContext;
private AbstractContext parentContext;
private final Date startTime = new Date();
private Date endTime;
private Map<String, Object> metaData;
Expand Down Expand Up @@ -71,6 +71,14 @@ public String getId() {
return this.id;
}

protected void setName(String name) {
this.name = name;
}

protected void setParentContext(AbstractContext context) {
this.parentContext = context;
}

/**
* Gets an context for a specified name.
* If it not exists, it will be created by a supplier,
Expand All @@ -93,7 +101,7 @@ protected <T extends AbstractContext> T getOrCreateContext(
* which could be generated.
*/
List<T> list = contexts.stream()
.filter(context -> name.equals(context.name))
.filter(context -> name.equals(context.getName()))
.collect(Collectors.toList());

if (list.size() == 0) {
Expand All @@ -102,7 +110,7 @@ protected <T extends AbstractContext> T getOrCreateContext(
}
try {
T context = newContextSupplier.get();
context.name = name;
context.setName(name);
contexts.add(context);

if (whenAddedToQueue != null) {
Expand Down Expand Up @@ -150,9 +158,4 @@ public void updateEndTimeRecursive(Date date) {
context = context.parentContext;
}
}

@Override
public SynchronizableContext getParent() {
return parentContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
*
* @author pele
*/
public class ClassContext extends AbstractContext implements SynchronizableContext, Loggable {
public class ClassContext extends AbstractContext implements Loggable {
private final Queue<MethodContext> methodContexts = new ConcurrentLinkedQueue<>();
private final Class testClass;
private TestClassContext testClassContext = null;

public ClassContext(Class testClass, TestContext testContext) {
this.testClass = testClass;
this.parentContext = testContext;
this.name = testClass.getSimpleName();
setParentContext(testContext);
setName(testClass.getSimpleName());
}

@Override
Expand All @@ -80,7 +80,7 @@ public Optional<TestClassContext> getTestClassContext() {
}

public TestContext getTestContext() {
return (TestContext) this.parentContext;
return (TestContext) this.getParentContext();
}

public Class getTestClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Stream;

public class ExecutionContext extends AbstractContext implements SynchronizableContext {
public class ExecutionContext extends AbstractContext {
private final Queue<SuiteContext> suiteContexts = new ConcurrentLinkedQueue<>();
/**
* @deprecated Use {@link #getRunConfig()} instead
Expand All @@ -49,7 +49,7 @@ public class ExecutionContext extends AbstractContext implements SynchronizableC
private final ConcurrentLinkedQueue<LogMessage> methodContextLessLogs = new ConcurrentLinkedQueue<>();

public ExecutionContext() {
name = runConfig.RUNCFG;
setName(runConfig.RUNCFG);
TesterraListener.getEventBus().post(new ContextUpdateEvent().setContext(this));
}

Expand All @@ -71,7 +71,7 @@ public ExecutionContext addExclusiveSessionContext(SessionContext sessionContext
}
if (!this.exclusiveSessionContexts.contains(sessionContext)) {
this.exclusiveSessionContexts.add(sessionContext);
sessionContext.parentContext = this;
sessionContext.setParentContext(this);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
import java.util.stream.Stream;

/**
* Holds the informations of an test method.
* Holds the information of a test method.
*
* @author mibu
*/
public class MethodContext extends AbstractContext implements SynchronizableContext {
public class MethodContext extends AbstractContext {

public enum Type {
TEST_METHOD,
Expand All @@ -60,8 +60,8 @@ public enum Type {
private final Type methodType;
private List<Object> parameterValues;
private int retryNumber = 0;
public int methodRunIndex = -1;
public String threadName = "unrelated";
private final int methodRunIndex;
private final String threadName;
private TestStep lastFailedStep;
private Class failureCorridorClass = FailureCorridor.High.class;

Expand All @@ -70,9 +70,9 @@ public enum Type {
*/
public final List<String> infos = new LinkedList<>();
private final List<SessionContext> sessionContexts = new LinkedList<>();
public String priorityMessage = null;
private String priorityMessage = null;
private final TestStepController testStepController = new TestStepController();
private List<MethodContext> relatedMethodContexts = new LinkedList<>();
private final List<MethodContext> relatedMethodContexts = new LinkedList<>();
private final List<MethodContext> dependsOnMethodContexts = new LinkedList<>();
private List<CustomContext> customContexts;
private List<Annotation> customAnnotations;
Expand All @@ -89,10 +89,12 @@ public MethodContext(
final Type methodType,
final ClassContext classContext
) {
this.name = name;
this.parentContext = classContext;
this.setName(name);
this.setParentContext(classContext);
this.methodRunIndex = Counters.increaseMethodExecutionCounter();
this.methodType = methodType;
final Thread currentThread = Thread.currentThread();
this.threadName = currentThread.getName() + "#" + currentThread.getId();
}

public void setRetryCounter(int retryCounter) {
Expand Down Expand Up @@ -142,13 +144,13 @@ public void addSessionContext(SessionContext sessionContext) {
if (!this.sessionContexts.contains(sessionContext)) {
this.sessionContexts.add(sessionContext);
sessionContext.addMethodContext(this);
sessionContext.parentContext = this;
sessionContext.setParentContext(this);
TesterraListener.getEventBus().post(new ContextUpdateEvent().setContext(this));
}
}

public ClassContext getClassContext() {
return (ClassContext) this.parentContext;
return (ClassContext) this.getParentContext();
}

public Stream<MethodContext> readRelatedMethodContexts() {
Expand Down Expand Up @@ -239,11 +241,6 @@ public void addPriorityMessage(String msg) {
}
}

public void setThreadName() {
final Thread currentThread = Thread.currentThread();
this.threadName = currentThread.getName() + "#" + currentThread.getId();
}

public boolean isConfigMethod() {
return methodType == Type.CONFIGURATION_METHOD;
}
Expand All @@ -268,7 +265,7 @@ public boolean isStatusOneOf(Status ...statuses) {
public String toString() {
return "MethodContext{" +
"methodRunIndex=" + methodRunIndex +
", name='" + name + '\'' +
", name='" + getName() + '\'' +
'}';
}

Expand Down Expand Up @@ -337,4 +334,24 @@ public void addAnnotation(Annotation annotation) {
}
this.customAnnotations.add(annotation);
}

public int getMethodRunIndex() {
return methodRunIndex;
}

public String getThreadName() {
return threadName;
}

public Stream<String> readInfos() {
return infos.stream();
}

public void addInfo(String info) {
this.infos.add(info);
}

public Optional<String> getPriorityMessage() {
return Optional.ofNullable(priorityMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,40 @@
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Stream;

public class SessionContext extends AbstractContext implements SynchronizableContext {
private String remoteSessionId;
public class SessionContext extends AbstractContext {
private Video video;
private NodeInfo nodeInfo;
private String browserName;
private String browserVersion;
private Map<String, Object> capabilities;
private final WebDriverRequest webDriverRequest;
private final Queue<MethodContext> methodContexts = new ConcurrentLinkedQueue<>();
private String remoteSessionId;

public SessionContext(WebDriverRequest webDriverRequest) {
try {
this.webDriverRequest = webDriverRequest.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
this.name = webDriverRequest.getSessionKey();

// this.provider = provider;
//
// final MethodContext currentMethodContext = ExecutionContextController.getCurrentMethodContext();
// if (currentMethodContext != null) {
// this.name = currentMethodContext.getName() + "_";
// } else {
// this.name = "";
// }
this.setName(webDriverRequest.getSessionKey());
}

public WebDriverRequest getWebDriverRequest() {
return this.webDriverRequest;
}

public String getSessionKey() {
return this.name;
return this.getName();
}

public SessionContext setSessionKey(String sessionKey) {
this.name = sessionKey;
this.setName(sessionKey);
return this;
}

public Optional<String> getRemoteSessionId() {
return Optional.ofNullable(remoteSessionId);
return Optional.ofNullable(this.remoteSessionId);
}

public SessionContext setRemoteSessionId(String sessionId) {
Expand Down
Loading