FileCacheDeleteValidationTest.java 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/***********************************************************************************************************************
 * Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
 *
 * 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 eu.stratosphere.pact.runtime.cache;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

import eu.stratosphere.core.fs.Path;
import eu.stratosphere.core.fs.local.LocalFileSystem;
import eu.stratosphere.nephele.jobgraph.JobID;
import junit.framework.Assert;

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

import java.io.File;
import java.io.IOException;

/**
 * Test delete process of {@link FileCache}. The local cache file should not be deleted why another task comes in 5 seconds.
 */
public class FileCacheDeleteValidationTest {
	FileCache fileCache = new FileCache();
	LocalFileSystem lfs = new LocalFileSystem();


	String testFileContent = "Goethe - Faust: Der Tragoedie erster Teil\n" + "Prolog im Himmel.\n"
		+ "Der Herr. Die himmlischen Heerscharen. Nachher Mephistopheles. Die drei\n" + "Erzengel treten vor.\n"
		+ "RAPHAEL: Die Sonne toent, nach alter Weise, In Brudersphaeren Wettgesang,\n"
		+ "Und ihre vorgeschriebne Reise Vollendet sie mit Donnergang. Ihr Anblick\n"
		+ "gibt den Engeln Staerke, Wenn keiner Sie ergruenden mag; die unbegreiflich\n"
		+ "hohen Werke Sind herrlich wie am ersten Tag.\n"
		+ "GABRIEL: Und schnell und unbegreiflich schnelle Dreht sich umher der Erde\n"
		+ "Pracht; Es wechselt Paradieseshelle Mit tiefer, schauervoller Nacht. Es\n"
		+ "schaeumt das Meer in breiten Fluessen Am tiefen Grund der Felsen auf, Und\n"
		+ "Fels und Meer wird fortgerissen Im ewig schnellem Sphaerenlauf.\n"
		+ "MICHAEL: Und Stuerme brausen um die Wette Vom Meer aufs Land, vom Land\n"
		+ "aufs Meer, und bilden wuetend eine Kette Der tiefsten Wirkung rings umher.\n"
		+ "Da flammt ein blitzendes Verheeren Dem Pfade vor des Donnerschlags. Doch\n"
		+ "deine Boten, Herr, verehren Das sanfte Wandeln deines Tags.\n";

	@Before
	public void createTmpCacheFile() {
		File f = new File(System.getProperty("java.io.tmpdir"), "cacheFile");
		try {
			Files.write(testFileContent, f, Charsets.UTF_8);
		} catch (IOException e) {
			throw new RuntimeException("Error initializing the test", e);
		}
	}

	@Test
	public void testFileReuseForNextTask() {
		JobID jobID = new JobID();
		String filePath = "file://" + new Path(System.getProperty("java.io.tmpdir"), "cacheFile").toString();
		fileCache.createTmpFile("test_file", filePath, jobID);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			throw new RuntimeException("Interrupted error", e);
		}
		fileCache.deleteTmpFile("test_file", jobID);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			throw new RuntimeException("Interrupted error", e);
		}
		//new task comes after 1 second
		try {
			Assert.assertTrue("Local cache file should not be deleted when another task comes in 5 seconds!", lfs.exists(fileCache.getTempDir(jobID, "test_file")));
		} catch (IOException e) {
			throw new RuntimeException("Interrupted error", e);
		}
		fileCache.createTmpFile("test_file", filePath, jobID);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			throw new RuntimeException("Interrupted error", e);
		}
		fileCache.deleteTmpFile("test_file", jobID);
		try {
			Thread.sleep(7000);
		} catch (InterruptedException e) {
			throw new RuntimeException("Interrupted error", e);
		}
		//no task comes in 7 seconds
		try {
			Assert.assertTrue("Local cache file should be deleted when no task comes in 5 seconds!", !lfs.exists(fileCache.getTempDir(jobID, "test_file")));
		} catch (IOException e) {
			throw new RuntimeException("Interrupted error", e);
		}
	}

	@After
	public void shutdown() {
		fileCache.shutdown();
	}
}