-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #397 from Cognifide/feature/inconsistent-screensho…
…t-height Feature/inconsistent screenshot height
- Loading branch information
Showing
12 changed files
with
308 additions
and
69 deletions.
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
Oops, something went wrong.