Skip to content
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

Delete old videos in hub before downloading a new video #162

Merged
merged 2 commits into from
Oct 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,16 @@ public void afterSession(TestSession session) {
if (session.getExternalKey() != null) {
stopVideoRecording(session);

CommonThreadPool.startCallable(
new VideoDownloaderCallable(
session.getExternalKey().getKey(),
session.getSlot().getRemoteURL().getHost()));
// Download video only if 'videos_to_keep' is greater than 0
if (RuntimeConfig.getConfig() == null) {
RuntimeConfig.load(false);
}
if (RuntimeConfig.getConfig().getVideoRecording().getVideosToKeep() > 0) {
CommonThreadPool.startCallable(
new VideoDownloaderCallable(
session.getExternalKey().getKey(),
session.getSlot().getRemoteURL().getHost()));
}
}

CommonThreadPool.startCallable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.groupon.seleniumgridextras.VideoHttpExecutor;
import com.groupon.seleniumgridextras.config.DefaultConfig;
import com.groupon.seleniumgridextras.config.RuntimeConfig;
import com.groupon.seleniumgridextras.utilities.threads.video.VideoRecorderCallable;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

Expand Down Expand Up @@ -74,6 +75,9 @@ public static File downloadVideoFromUri(URI uri) {
destinationDir.mkdir();
}

// Delete old movies
VideoRecorderCallable.deleteOldMovies(destinationDir);

File destFile = new File(
destinationDir.getAbsolutePath(),
uri.getRawPath().replaceAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public VideoRecorderCallable(String sessionID, int timeout) {
dimension.getWidth(),
dimension.getHeight()));
}
deleteOldMovies();
VideoRecorderCallable.deleteOldMovies(outputDir);
}

@Override
Expand Down Expand Up @@ -249,8 +249,8 @@ public static BufferedImage convertToType(BufferedImage sourceImage,
return image;
}

protected void deleteOldMovies() {
File[] files = outputDir.listFiles();
public static void deleteOldMovies(File moviesDir) {
File[] files = moviesDir.listFiles();
//TODO: This is tested, but don't you dare modify this without writing a new test!
int filesToKeep = RuntimeConfig.getConfig().getVideoRecording().getVideosToKeep();
int currentFileCount = files.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,4 @@ public void testCheckIfUrlStatusCode() throws Exception{
assertEquals(200, HttpUtility.checkIfUrlStatusCode(new URL("https://github.com/groupon/Selenium-Grid-Extras/releases/download/v1.5.0/SeleniumGridExtras-1.5.0-SNAPSHOT-jar-with-dependencies.jar")));
}

// @Test //This is commented out until we find a more consistent place to download videos from
// public void testGetVideoFromUri() throws Exception {
// RuntimeConfig.load();
// File actual = HttpUtility.downloadVideoFromUri(new URI("http://192.168.168.144:3000/download_video/ad895b34-ee0a-4362-b542-a63d90ea221d.mp4"));
// assertTrue(actual.exists());
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.groupon.seleniumgridextras.utilities;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.groupon.seleniumgridextras.config.Config;
import com.groupon.seleniumgridextras.config.DefaultConfig;
import com.groupon.seleniumgridextras.config.RuntimeConfig;

import java.io.File;
import java.net.URI;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

// This class is ignored until we find a more consistent place to download videos from
@Ignore
public class HttpUtilityVideosTest {

public static final String VIDEO_RECORDER_TEST_JSON = "video_recorder_test.json";

final private String videosUrl = "http://192.168.168.144:3000/download_video/";
final private String video1Filename = "e985bb1c-bb92-4b16-8e76-62e039efbbc0.mp4";
final private String video2Filename = "f041511c-367d-4772-a82a-7b48ab69615c.mp4";
final private String video3Filename = "94a74a04-1579-4475-b65d-926b905f0a40.mp4";

@Before
public void setUp() throws Exception {
RuntimeConfig.setConfigFile(VIDEO_RECORDER_TEST_JSON);
Config config = DefaultConfig.getDefaultConfig();
config.getVideoRecording().setVideosToKeep(1);
config.writeToDisk(RuntimeConfig.getConfigFile());
RuntimeConfig.load();
}

@After
public void tearDown() throws Exception {
delete(new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, video1Filename));
delete(new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, video2Filename));
delete(new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, video3Filename));
delete(new File(RuntimeConfig.getConfigFile()));
delete(new File(VIDEO_RECORDER_TEST_JSON + ".example"));
}

private void delete(File f) {
if (f.exists()) {
f.delete();
}
}

@Test
public void testGetVideoFromUri() throws Exception {
File local1 = HttpUtility.downloadVideoFromUri(new URI(videosUrl + video1Filename));
assertTrue(local1.exists());
}

@Test
public void testGetVideoFromUriAndDeleteOldMovies() throws Exception {
File video1 = HttpUtility.downloadVideoFromUri(new URI(videosUrl + video1Filename));
File video2 = HttpUtility.downloadVideoFromUri(new URI(videosUrl + video2Filename));
assertTrue(video1.exists());
assertTrue(video2.exists());

File video3 = HttpUtility.downloadVideoFromUri(new URI(videosUrl + video3Filename));

// Only one file has been removed because the number of files is checked before the download
assertFalse(video1.exists());
assertTrue(video2.exists());
assertTrue(video3.exists());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,48 @@

import java.awt.*;
import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class VideoRecorderCallableTest {

public static final String VIDEO_RECORDER_TEST_JSON = "video_recorder_test.json";
final private String session = "123456";
final private File output = new File("video_output", session + ".mp4");

final private String session1 = "123456";
final private String session2 = "654321";
final private String session3 = "abcdef";

final private File session1File = new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, session1 + ".mp4");
final private File session2File = new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, session2 + ".mp4");
final private File session3File = new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, session3 + ".mp4");

@Before
public void setUp() throws Exception {
RuntimeConfig.setConfigFile(VIDEO_RECORDER_TEST_JSON);
Config config = DefaultConfig.getDefaultConfig();
config.getVideoRecording().setVideosToKeep(1);
config.writeToDisk(RuntimeConfig.getConfigFile());
RuntimeConfig.load();
}

@After
public void tearDown() throws Exception {
if (output.exists()) {
output.delete();
}

if (new File(RuntimeConfig.getConfigFile()).exists()){
new File(RuntimeConfig.getConfigFile()).delete();
}
delete(session1File);
delete(session2File);
delete(session3File);
delete(new File(RuntimeConfig.getConfigFile()));
delete(new File(VIDEO_RECORDER_TEST_JSON + ".example"));
}

if (new File(VIDEO_RECORDER_TEST_JSON + ".example").exists()){
new File(VIDEO_RECORDER_TEST_JSON + ".example").delete();
private void delete(File f) {
if (f.exists()) {
f.delete();
}
}

Expand All @@ -53,7 +62,7 @@ public void testRecordVideo() throws Exception {
}

if (RuntimeConfig.getOS().hasGUI()) {
VideoRecorderCallable video = new VideoRecorderCallable(session, 60);
VideoRecorderCallable video = new VideoRecorderCallable(session1, 60);

ExecutorService cachedPool = Executors.newCachedThreadPool();

Expand All @@ -68,7 +77,7 @@ public void testRecordVideo() throws Exception {

future.get(); //wait for thread to finish

assertTrue(output.exists());
assertTrue(session1File.exists());

} finally {
cachedPool.shutdown();
Expand All @@ -84,7 +93,7 @@ public void testTimeout() throws Exception {
}

if (RuntimeConfig.getOS().hasGUI()) {
VideoRecorderCallable video = new VideoRecorderCallable(session, 2);
VideoRecorderCallable video = new VideoRecorderCallable(session1, 2);

ExecutorService cachedPool = Executors.newCachedThreadPool();

Expand All @@ -103,8 +112,9 @@ public void testTimeoutExtended() throws Exception {
if (!ImageProcessorTest.testIfDimasComputer()){
return;
}

if (RuntimeConfig.getOS().hasGUI()) {
VideoRecorderCallable video = new VideoRecorderCallable(session, 2);
VideoRecorderCallable video = new VideoRecorderCallable(session1, 2);

ExecutorService cachedPool = Executors.newCachedThreadPool();

Expand Down Expand Up @@ -139,4 +149,60 @@ public void testResolutionDivisibleByTwo() throws Exception{
assertEquals(false, VideoRecorderCallable.isResolutionDivisibleByTwo(new Dimension(1024, 769)));
}

@Test
public void testDeleteOldMovies() throws Exception {
// Create empty files
File outputDir = new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY);
if (!outputDir.exists()) {
outputDir.mkdir();
}
session1File.createNewFile();
Thread.sleep(100);
session2File.createNewFile();
Thread.sleep(100);
session3File.createNewFile();

// Delete older files
VideoRecorderCallable.deleteOldMovies(outputDir);

// Older files has been removed
assertFalse(session1File.exists());
assertFalse(session2File.exists());
assertTrue(session3File.exists());
}

@Test
public void testRecordVideoAndDeleteOldMovies() throws Exception {
if (!ImageProcessorTest.testIfDimasComputer()){
return;
}

if (RuntimeConfig.getOS().hasGUI()) {
ExecutorService cachedPool = Executors.newCachedThreadPool();

try {
recordVideo(cachedPool, session1);
recordVideo(cachedPool, session2);
assertTrue(session1File.exists());
assertTrue(session2File.exists());

recordVideo(cachedPool, session3);

// Only one file has been removed because the number of files is checked before the record
assertFalse(session1File.exists());
assertTrue(session2File.exists());
assertTrue(session3File.exists());
} finally {
cachedPool.shutdown();
}
}
}

private void recordVideo(ExecutorService cachedPool, String session) throws InterruptedException, ExecutionException {
VideoRecorderCallable video = new VideoRecorderCallable(session, 60);
Future<String> future = cachedPool.submit(video);
video.stop();
future.get(); //wait for thread to finish
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.groupon.seleniumgridextras.videorecording;


import com.groupon.seleniumgridextras.config.Config;
import com.groupon.seleniumgridextras.config.DefaultConfig;
import com.groupon.seleniumgridextras.config.RuntimeConfig;
import com.groupon.seleniumgridextras.utilities.threads.video.VideoRecordingThreadPool;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
Expand All @@ -12,19 +15,31 @@

public class VideoRecordingThreadPoolTest {

public static final String VIDEO_RECORDER_TEST_JSON = "video_recorder_test.json";

final private String session1 = "123456";
final private String session2 = "654321";
final private String session3 = "abcdef";

final private File session1File = new File("video_output", session1 + ".mp4");
final private File session2File = new File("video_output", session2 + ".mp4");
final private File session3File = new File("video_output", session3 + ".mp4");
final private File session1File = new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, session1 + ".mp4");
final private File session2File = new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, session2 + ".mp4");
final private File session3File = new File(DefaultConfig.VIDEO_OUTPUT_DIRECTORY, session3 + ".mp4");

@Before
public void setUp() throws Exception {
RuntimeConfig.setConfigFile(VIDEO_RECORDER_TEST_JSON);
Config config = DefaultConfig.getDefaultConfig();
config.writeToDisk(RuntimeConfig.getConfigFile());
RuntimeConfig.load();
}

@After
public void tearDown() throws Exception {
delete(session1File);
delete(session2File);
delete(session3File);
delete(new File(RuntimeConfig.getConfigFile()));
delete(new File(VIDEO_RECORDER_TEST_JSON + ".example"));
}

private void delete(File f) {
Expand Down