com.applitools.eyes.services.CheckService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eyes-sdk-core-java3 Show documentation
Show all versions of eyes-sdk-core-java3 Show documentation
Applitools Eyes SDK base for Java
The newest version!
package com.applitools.eyes.services;
import com.applitools.connectivity.ServerConnector;
import com.applitools.eyes.*;
import com.applitools.eyes.logging.Stage;
import com.applitools.eyes.logging.TraceLevel;
import com.applitools.eyes.logging.Type;
import org.apache.commons.lang3.tuple.Pair;
import java.util.*;
public class CheckService extends EyesService {
// Queue for tests that finished uploading and waiting for match window
private final List> matchWindowQueue = Collections.synchronizedList(new ArrayList>());
private final Set inUploadProcess = Collections.synchronizedSet(new HashSet());
private final Set inMatchWindowProcess = Collections.synchronizedSet(new HashSet());
public CheckService(Logger logger, ServerConnector serverConnector) {
super(logger, serverConnector);
}
@Override
public void run() {
while (!inputQueue.isEmpty()) {
final Pair nextInput = inputQueue.remove(0);
final MatchWindowData matchWindowData = nextInput.getRight();
inUploadProcess.add(nextInput.getLeft());
tryUploadImage(nextInput.getLeft(), matchWindowData, new ServiceTaskListener() {
@Override
public void onComplete(Void output) {
inUploadProcess.remove(nextInput.getLeft());
matchWindowQueue.add(Pair.of(nextInput.getLeft(), matchWindowData));
}
@Override
public void onFail(Throwable t) {
inUploadProcess.remove(nextInput.getLeft());
errorQueue.add(Pair.of(nextInput.getLeft(), t));
}
});
}
while (!matchWindowQueue.isEmpty()) {
final Pair nextInput = matchWindowQueue.remove(0);
final MatchWindowData matchWindowData = nextInput.getRight();
inMatchWindowProcess.add(nextInput.getLeft());
ServiceTaskListener listener = new ServiceTaskListener() {
@Override
public void onComplete(MatchResult taskResponse) {
inMatchWindowProcess.remove(nextInput.getLeft());
outputQueue.add(Pair.of(nextInput.getLeft(), taskResponse));
}
@Override
public void onFail(Throwable t) {
inMatchWindowProcess.remove(nextInput.getLeft());
errorQueue.add(Pair.of(nextInput.getLeft(), t));
}
};
matchWindow(nextInput.getLeft(), matchWindowData, listener);
}
}
public void tryUploadImage(final String testId, MatchWindowData data, final ServiceTaskListener taskListener) {
final AppOutput appOutput = data.getAppOutput();
if (appOutput.getScreenshotUrl() != null) {
taskListener.onComplete(null);
return;
}
// Getting the screenshot bytes
TaskListener uploadListener = new TaskListener() {
@Override
public void onComplete(String s) {
if (s == null) {
onFail();
return;
}
logger.log(TraceLevel.Info, Collections.singleton(testId), Stage.CHECK, Type.UPLOAD_COMPLETE, Pair.of("url", s));
appOutput.setScreenshotUrl(s);
taskListener.onComplete(null);
}
@Override
public void onFail() {
appOutput.setScreenshotUrl(null);
taskListener.onFail(new EyesException("Failed uploading image"));
}
};
try {
logger.log(TraceLevel.Info, Collections.singleton(testId), Stage.CHECK, Type.UPLOAD_START, Pair.of("matchWindowData", matchWindowQueue));
((ServerConnector) serverConnector).uploadImage(uploadListener, appOutput.getScreenshotBytes());
} catch (Throwable t) {
taskListener.onFail(t);
}
}
public void matchWindow(final String testId, MatchWindowData data, final ServiceTaskListener listener) {
try {
logger.log(TraceLevel.Info, Collections.singleton(testId), Stage.CHECK, Type.MATCH_START, Pair.of("matchWindowData", data));
((ServerConnector) serverConnector).matchWindow(new TaskListener() {
@Override
public void onComplete(MatchResult taskResponse) {
logger.log(testId, Stage.CHECK, Type.MATCH_COMPLETE, Pair.of("matchResult", taskResponse));
listener.onComplete(taskResponse);
}
@Override
public void onFail() {
listener.onFail(new EyesException("Match window failed"));
}
}, data);
} catch (Throwable t) {
listener.onFail(t);
}
}
}