All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.applitools.eyes.triggers.VisualGridRunner0 Maven / Gradle / Ivy

The newest version!
package com.applitools.eyes.triggers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.applitools.connectivity.ServerConnector;
import com.applitools.eyes.AbstractProxySettings;
import com.applitools.eyes.EyesException;
import com.applitools.eyes.Logger;
import com.applitools.eyes.NetworkLogHandler;
import com.applitools.eyes.TestResultContainer;
import com.applitools.eyes.TestResultsSummary;
import com.applitools.eyes.logging.Stage;
import com.applitools.eyes.logging.TraceLevel;
import com.applitools.eyes.services.EyesServiceRunner;
import com.applitools.eyes.visualgrid.model.FrameData;
import com.applitools.eyes.visualgrid.model.IDebugResourceWriter;
import com.applitools.eyes.visualgrid.model.RGridResource;
import com.applitools.eyes.visualgrid.model.RenderingInfo;
import com.applitools.eyes.visualgrid.services.CheckTask;
import com.applitools.eyes.visualgrid.services.EyesRunner0;
import com.applitools.eyes.visualgrid.services.IEyes;
import com.applitools.eyes.visualgrid.services.RunnerOptions;
import com.applitools.eyes.visualgrid.services.VisualGridRunningTest;
import com.applitools.utils.ArgumentGuard;
import com.applitools.utils.GeneralUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.tuple.Pair;

/**
 * Used to manage multiple Eyes sessions when working with the Ultrafast Grid
 */
public class VisualGridRunner0 extends EyesRunner0 {
  static class TestConcurrency {
    final int userConcurrency;
    final int actualConcurrency;
    final boolean isLegacy;
    boolean isDefault = false;

    TestConcurrency() {
      isDefault = true;
      isLegacy = false;
      userConcurrency = DEFAULT_CONCURRENCY;
      actualConcurrency = DEFAULT_CONCURRENCY;
    }

    TestConcurrency(int userConcurrency, boolean isLegacy) {
      this.userConcurrency = userConcurrency;
      this.actualConcurrency = isLegacy ? userConcurrency * CONCURRENCY_FACTOR : userConcurrency;
      this.isLegacy = isLegacy;
    }
  }

  private static final int CONCURRENCY_FACTOR = 5;
  static final int DEFAULT_CONCURRENCY = 5;

  EyesServiceRunner eyesServiceRunner;
  final TestConcurrency testConcurrency;
  private boolean wasConcurrencyLogSent = false;
  final Set allEyes = Collections.synchronizedSet(new HashSet());
  private final Map resourcesCacheMap = Collections.synchronizedMap(new HashMap());

  private RenderingInfo renderingInfo;
  private IDebugResourceWriter debugResourceWriter;
  private boolean isDisabled;

  private String suiteName;

  public VisualGridRunner0() {
    this(Thread.currentThread().getStackTrace()[2].getClassName());
  }

  public VisualGridRunner0(String suiteName) {
    super();
    this.testConcurrency = new TestConcurrency();
    //managerRef = commandExecutor.coreMakeManager(ManagerType.VISUAL_GRID.value, testConcurrency.actualConcurrency, testConcurrency.isLegacy);
  }

  public VisualGridRunner0(int testConcurrency) {
    this(testConcurrency, Thread.currentThread().getStackTrace()[2].getClassName());
  }

  public VisualGridRunner0(int testConcurrency0, String suiteName) {
    this.testConcurrency = new TestConcurrency(testConcurrency0, true);
    //managerRef = commandExecutor.coreMakeManager(ManagerType.VISUAL_GRID.value, testConcurrency.actualConcurrency, testConcurrency.isLegacy);
  }

  public VisualGridRunner0(RunnerOptions runnerOptions) {
    this(runnerOptions, Thread.currentThread().getStackTrace()[2].getClassName());
  }

  public VisualGridRunner0(RunnerOptions runnerOptions, String suiteName) {
    ArgumentGuard.notNull(runnerOptions, "runnerOptions");
    int testConcurrency0 = runnerOptions.getTestConcurrency() == null ? DEFAULT_CONCURRENCY : runnerOptions.getTestConcurrency();
    this.testConcurrency = new TestConcurrency(testConcurrency0, false);
    //managerRef = commandExecutor.coreMakeManager(ManagerType.VISUAL_GRID.value, testConcurrency.actualConcurrency, testConcurrency.isLegacy);

    //setApiKey(runnerOptions.getApiKey());
    //setServerUrl(runnerOptions.getServerUrl());
    //init(suiteName);
//        if (runnerOptions.isAutProxySet()) {
//            eyesServiceRunner.setAutProxy(runnerOptions.getAutProxy(), runnerOptions.getAutProxyDomains(), runnerOptions.getAutProxyMode());
//        } else {
//            if (runnerOptions.getProxy() != null) {
//                eyesServiceRunner.setAutProxy(runnerOptions.getProxy(), null, null);
//            }
//        }
//        setProxy(runnerOptions.getProxy());
  }

  private void init(String suiteName) {
    this.suiteName = suiteName;
    eyesServiceRunner = new EyesServiceRunner(logger, serverConnector, allEyes, testConcurrency.actualConcurrency, debugResourceWriter, resourcesCacheMap);
    eyesServiceRunner.start();
  }

  public void open(IEyes eyes, List newTests) {
    if (renderingInfo == null) {
      renderingInfo = serverConnector.getRenderInfo();
    }

    eyesServiceRunner.setRenderingInfo(renderingInfo);
    if (allEyes.isEmpty()) {
      this.setLogger(eyes.getLogger());
    }
    synchronized (allEyes) {
      allEyes.add(eyes);
    }

    try {
      String logMessage = getConcurrencyLog();
      if (logMessage != null) {
        NetworkLogHandler.sendSingleLog(serverConnector, TraceLevel.Notice, logMessage);
      }
    } catch (JsonProcessingException e) {
      GeneralUtils.logExceptionStackTrace(logger, Stage.OPEN, e);
    }

    this.addBatch(eyes.getBatchId(), eyes.getBatchCloser());
    eyesServiceRunner.openTests(newTests);
  }

  public synchronized void check(FrameData domData, List checkTasks) {
    eyesServiceRunner.addResourceCollectionTask(domData, checkTasks);
    logMemoryUsage();
  }

  public TestResultsSummary getAllTestResultsImpl(boolean throwException) {
    synchronized (allEyes) {
      for (IEyes eyes : allEyes) {
        // Closing all tests that the user didn't close
        eyes.closeAsync();
      }
    }

    boolean isRunning = true;
    while (isRunning && getError() == null) {
      isRunning = false;
      synchronized (allEyes) {
        for (IEyes eyes : allEyes) {
          isRunning = isRunning || !eyes.isCompleted();
        }
      }

      try {
        Thread.sleep(500);
      } catch (InterruptedException ignored) {}
    }

    if (getError() != null) {
      throw new EyesException("Execution crashed", getError());
    }

    eyesServiceRunner.stopServices();

    Throwable exception = null;
    List allResults = new ArrayList<>();
    synchronized (allEyes) {
      for (IEyes eyes : allEyes) {
        List eyesResults = eyes.getAllTestResults();
        for (TestResultContainer result : eyesResults) {
          if (exception == null && result.getException() != null) {
            exception = result.getException();
          }
        }

        allResults.addAll(eyesResults);
      }
    }

    if (throwException && exception != null) {
      throw new Error(exception);
    }
    return new TestResultsSummary(allResults);
  }

  public Throwable getError() {
    return eyesServiceRunner.getError();
  }

  public void setDebugResourceWriter(IDebugResourceWriter debugResourceWriter) {
    this.debugResourceWriter = debugResourceWriter;
    eyesServiceRunner.setDebugResourceWriter(debugResourceWriter);
  }

  public IDebugResourceWriter getDebugResourceWriter() {
    return this.debugResourceWriter;
  }

  public void setLogger(Logger logger) {
    eyesServiceRunner.setLogger(logger);
    this.logger = logger;
  }

  @Override
  public void setServerConnector(ServerConnector serverConnector) {
    super.setServerConnector(serverConnector);
    eyesServiceRunner.setServerConnector(serverConnector);
  }

  public void setProxy(AbstractProxySettings proxySettings) {
    super.setProxy(proxySettings);
    if (proxySettings != null) {
      eyesServiceRunner.setAutProxy(proxySettings, null, null);
    }
  }

  public String getConcurrencyLog() throws JsonProcessingException {
    if (wasConcurrencyLogSent) {
      return null;
    }

    wasConcurrencyLogSent = true;
    String key = testConcurrency.isDefault ? "defaultConcurrency" : testConcurrency.isLegacy ? "concurrency" : "testConcurrency";
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode objectNode = objectMapper.createObjectNode();
    objectNode.put("type", "runnerStarted");
    objectNode.put(key, testConcurrency.userConcurrency);
    return objectMapper.writeValueAsString(objectNode);
  }

  public Map getResourcesCacheMap() {
    return resourcesCacheMap;
  }

  public void setIsDisabled(boolean isDisabled) {
    this.isDisabled = isDisabled;
  }

  public boolean getIsDisabled() {
    return this.isDisabled;
  }

  public String getSuiteName() {
    return suiteName;
  }

  public void setSuiteName(String suiteName) {
    this.suiteName = suiteName;
  }

  public void logMemoryUsage() {
    logger.log(TraceLevel.Debug, Collections.emptySet(), Stage.GENERAL, null,
        Pair.of("totalMemory", Runtime.getRuntime().totalMemory()),
        Pair.of("freeMemory", Runtime.getRuntime().freeMemory()),
        Pair.of("maxMemory", Runtime.getRuntime().maxMemory()));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy