-
Notifications
You must be signed in to change notification settings - Fork 48
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
Feature/inconsistent screenshot height #397
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
99e225c
Add algorithm to enable taking long screenshots without resolution-sl…
Jakub-Izbicki 597d913
Update documentation
Jakub-Izbicki 4c36891
Fix logger creation
Jakub-Izbicki 675de4c
Use CircularFifoBuffer instead of LinkedList in algorithm
Jakub-Izbicki d94624e
Fix review issues
Jakub-Izbicki 16d944f
Return default sampling wait time when conversion fails
Skejven f9acd40
Check range for samplingPeriod
Jakub-Izbicki 937047a
Merge branch 'master' into feature/inconsistent-screenshot-height
tkaik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
core/jobs/src/main/java/com/cognifide/aet/job/common/utils/CurrentThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* AET | ||
* | ||
* Copyright (C) 2013 Cognifide Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.cognifide.aet.job.common.utils; | ||
|
||
public final class CurrentThread { | ||
|
||
public static void sleep(int ms) { | ||
try { | ||
Thread.sleep(ms); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
core/jobs/src/main/java/com/cognifide/aet/job/common/utils/Sampler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* AET | ||
* | ||
* Copyright (C) 2013 Cognifide Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.cognifide.aet.job.common.utils; | ||
|
||
import java.util.function.Supplier; | ||
import org.apache.commons.collections.buffer.CircularFifoBuffer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Sampler { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(Sampler.class); | ||
|
||
/** | ||
* Collects values from supplier in specified periods of time, and compares last n samples every | ||
* iteration. If all n samples are equal(), returns the value. If last n samples don't match | ||
* before max iterations threshold is reached, returns last collected sample. | ||
* | ||
* @param samplesSupplier supplier of value to wait for, | ||
* @param samplingPeriod milliseconds period between taking each sample, | ||
* @param sampleQueueSize defines the last n elements that are to be compared, | ||
* @param maxSamplesThreshold max number of samples before return | ||
* @return last collected sample | ||
*/ | ||
public static <T> T waitForValue(Supplier<T> samplesSupplier, int samplingPeriod, | ||
int sampleQueueSize, int maxSamplesThreshold) { | ||
CircularFifoBuffer samplesQueue = new CircularFifoBuffer(sampleQueueSize); | ||
|
||
int samplesTaken = 0; | ||
while (!isThresholdReached(samplesTaken, maxSamplesThreshold) && | ||
!areAllSamplesEqual(samplesQueue)) { | ||
|
||
CurrentThread.sleep(samplingPeriod); | ||
|
||
T nextSample = samplesSupplier.get(); | ||
samplesQueue.add(nextSample); | ||
++samplesTaken; | ||
} | ||
return (T) samplesQueue.get(); | ||
} | ||
|
||
private static boolean isThresholdReached(int samplesTaken, int maxSamplesThreshold) { | ||
if (samplesTaken >= maxSamplesThreshold) { | ||
LOG.warn("Sampling reached threshold"); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean areAllSamplesEqual(CircularFifoBuffer samplesQueue) { | ||
return samplesQueue.isFull() && | ||
samplesQueue.stream().allMatch(sample -> samplesQueue.get() != null && | ||
samplesQueue.get().equals(sample)); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
core/jobs/src/test/java/com/cognifide/aet/job/common/utils/SamplerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* AET | ||
* | ||
* Copyright (C) 2013 Cognifide Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.cognifide.aet.job.common.utils; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Random; | ||
import java.util.function.Supplier; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SamplerTest { | ||
|
||
private static final int MAX_SAMPLES_THRESHOLD = 5; | ||
private static final int SAMPLE_QUEUE_SIZE = 3; | ||
private static final int SAMPLING_PERIOD = 1; | ||
|
||
@Mock | ||
private Supplier<Integer> supplier; | ||
|
||
@Test | ||
public void sampleChangingValueTest_AllSamplesMatch() { | ||
when(supplier.get()).thenReturn(0); | ||
|
||
Integer finalSample = Sampler | ||
.waitForValue(supplier, SAMPLING_PERIOD, SAMPLE_QUEUE_SIZE, MAX_SAMPLES_THRESHOLD); | ||
|
||
assertEquals((int) finalSample, 0); | ||
} | ||
|
||
@Test | ||
public void sampleChangingValueTest_AllSamplesDiffer_ThresholdReached() { | ||
Random random = new Random(); | ||
when(supplier.get()) | ||
.thenReturn(random.nextInt()) | ||
.thenReturn(random.nextInt()) | ||
.thenReturn(random.nextInt()) | ||
.thenReturn(random.nextInt()) | ||
.thenReturn(random.nextInt()); | ||
|
||
Sampler.waitForValue(supplier, 1, 3, 5); | ||
|
||
verify(supplier, times(5)).get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ation-tests/sample-site/src/main/webapp/sanity/comparators/layout/long_expanding_page.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<%-- | ||
|
||
AET | ||
|
||
Copyright (C) 2013 Cognifide Limited | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--%> | ||
|
||
<%-- | ||
This page simulates a page that takes a long time to load. | ||
The final height of the page is above 12k pixels. | ||
--%> | ||
|
||
<script> | ||
window.onload = function () { | ||
var counter = 0; | ||
var looper = setInterval(function() { | ||
counter++; | ||
console.log("Counter is: " + counter); | ||
|
||
var emptyDiv = document.createElement("div"); | ||
emptyDiv.style = "height: 1000px"; | ||
|
||
var src = document.getElementById("enclosingDiv"); | ||
src.appendChild(emptyDiv); | ||
|
||
if (counter >= 12) { | ||
clearInterval(looper); | ||
} | ||
}, 100); | ||
|
||
<%-- | ||
TODO: Change 12 iterations to 30 when (https://github.com/Cognifide/aet/pull/387) is merged. | ||
--%> | ||
|
||
}; | ||
</script> | ||
|
||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | ||
<%@ include file="/includes/header.jsp" %> | ||
<div id="enclosingDiv"></div> | ||
<%@ include file="dynamic_content.jsp" %> | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no limit for the
samplingPeriod
.Just a question here: shouldn't that be limited somehow?
What will happen if user sets here e.g.
10000000 ms
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct, there should be a range for this param. I will set max at 10 seconds. Please give suggestion if the value seems reasonable.