Skip to content

Commit

Permalink
Merge pull request #295 from YamStranger/darkroom_fix
Browse files Browse the repository at this point in the history
Updated test to check how darkroom works in parallel screenshot taking
  • Loading branch information
YamStranger committed Mar 1, 2016
2 parents 704f48b + 1a6f158 commit 411b11c
Showing 1 changed file with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,68 +1,104 @@
package net.serenitybdd.core.photography.integration

import com.google.common.collect.Lists
import net.serenitybdd.core.photography.Darkroom
import net.serenitybdd.core.photography.Photographer
import net.serenitybdd.core.photography.ScreenshotPhoto
import org.openqa.selenium.WebDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.phantomjs.PhantomJSDriver
import spock.lang.Specification

import java.nio.file.Files
import java.nio.file.Path
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Future

class WhenAPhotographerTakesLotsOfScreenshots extends Specification {

def "when a photographer takes a series of screenshots they all should be stored"() {
given:
def photographer = new Photographer();
when:
def driver = new FirefoxDriver()
Darkroom.isOpenForBusiness();
List<ScreenshotPhoto> photos = Lists.newArrayList();
for (photoCount in 0..10){
photos.add(photographer.takesAScreenshot()
def driver
try {
driver = new PhantomJSDriver()
Darkroom.isOpenForBusiness();
for (photoCount in 0..10) {
photos.add(photographer.takesAScreenshot()
.with(driver)
.andSaveToDirectory(screenshotDirectory));
}
Darkroom.waitUntilClose();
} finally {
try {
if (driver) {
driver.close()
driver.quit()
}
} catch (some) {
}
}
Darkroom.waitUntilClose();
driver.quit()
then:
photos.findAll {
photo -> Files.exists(photo.pathToScreenshot)
}.size() == 11

}

def "should handle multiple screenshots in parallel"() {
given:
def photographer = new Photographer();
List<ScreenshotPhoto> photos = Lists.newArrayList();
and:
def threads = []
for (i in 1..10) {
threads << Thread.start {
Darkroom.isOpenForBusiness();
def driver = new FirefoxDriver()
driver.get(siteFromUrlAt("/static-site/index.html"))
def List<Future<List<ScreenshotPhoto>>> processing = new ArrayList<>();
def List<ScreenshotPhoto> screenshots = new ArrayList<>();
def Integer threads = 10
def Integer photos = 3

for (j in 1..10) {
photos.add(photographer.takesAScreenshot()
.with(driver)
.andSaveToDirectory(screenshotDirectory));
def ExecutorService service = Executors.newFixedThreadPool(10);
when:
threads.times {
processing << service.submit(new Callable<List<ScreenshotPhoto>>() {
@Override
List<ScreenshotPhoto> call() throws Exception {
def List<ScreenshotPhoto> photo = new ArrayList<>()
Darkroom.isOpenForBusiness();
def driver = null
try {
driver = new PhantomJSDriver()
driver.get(siteFromUrlAt("/static-site/index.html"))
photos.times {
photo.add(photographer.takesAScreenshot()
.with(driver)
.andSaveToDirectory(screenshotDirectory));
}
} catch (some) {
println some

} finally {
try {
if (driver) {
driver.close()
driver.quit()
}
} catch (some) {
}
}
Darkroom.waitUntilClose();
return photo
}
driver.quit()
Darkroom.waitUntilClose();
}
})
}
processing.each { future ->
screenshots.addAll(future.get())
}
println threads
when:
threads.forEach { it.join() }
then:
photos.size() == 100
photos.findAll {
processing.size() == threads
screenshots.size() == threads * photos
screenshots.findAll {
photo -> Files.exists(photo.pathToScreenshot)
}.size() == 100
}.size() == threads * photos
}
WebDriver driver;

Path screenshotDirectory;

long startTime;
Expand All @@ -76,7 +112,7 @@ class WhenAPhotographerTakesLotsOfScreenshots extends Specification {

String siteFromUrlAt(String path) {
File baseDir = new File(System.getProperty("user.dir"));
File testSite = new File(baseDir, "src/test/resources" + path);
File testSite = new File(baseDir, "src/test/resources" + path);
return "file://" + testSite.getAbsolutePath();
}

Expand Down

0 comments on commit 411b11c

Please sign in to comment.