JavaProgramTestBase.java 5.3 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/***********************************************************************************************************************
 * 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.test.util;

import org.junit.Assert;
import org.junit.Test;

import eu.stratosphere.api.common.JobExecutionResult;
import eu.stratosphere.api.common.Plan;
import eu.stratosphere.api.java.ExecutionEnvironment;
import eu.stratosphere.client.minicluster.NepheleMiniCluster;
import eu.stratosphere.compiler.DataStatistics;
import eu.stratosphere.compiler.PactCompiler;
import eu.stratosphere.compiler.plan.OptimizedPlan;
import eu.stratosphere.compiler.plandump.PlanJSONDumpGenerator;
import eu.stratosphere.compiler.plantranslate.NepheleJobGraphGenerator;
import eu.stratosphere.configuration.Configuration;
import eu.stratosphere.nephele.client.JobClient;
import eu.stratosphere.nephele.jobgraph.JobGraph;


public abstract class JavaProgramTestBase extends AbstractTestBase {

	private static final int DEFAULT_DEGREE_OF_PARALLELISM = 4;
	
	private JobExecutionResult latestExecutionResult;
	
	private int degreeOfParallelism = DEFAULT_DEGREE_OF_PARALLELISM;
	
	
	public JavaProgramTestBase() {
		this(new Configuration());
	}
	
	public JavaProgramTestBase(Configuration config) {
		super(config);
	}
	
	
	public void setDegreeOfParallelism(int degreeOfParallelism) {
		this.degreeOfParallelism = degreeOfParallelism;
	}
	
	public JobExecutionResult getLatestExecutionResult() {
		return this.latestExecutionResult;
	}
	
	// --------------------------------------------------------------------------------------------
	//  Methods to create the test program and for pre- and post- test work
	// --------------------------------------------------------------------------------------------

	protected abstract void testProgram() throws Exception;
	

	protected void preSubmit() throws Exception {}
	
	protected void postSubmit() throws Exception {}
	

	// --------------------------------------------------------------------------------------------
	//  Test entry point
	// --------------------------------------------------------------------------------------------

	@Test
	public void testJob() throws Exception {
		// pre-submit
		try {
			preSubmit();
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			Assert.fail("Pre-submit work caused an error: " + e.getMessage());
		}
		
		// prepare the test environment
		TestEnvironment env = new TestEnvironment(this.executor, this.degreeOfParallelism);
		env.setAsContext();
		
		// call the test program
		try {
			testProgram();
			this.latestExecutionResult = env.latestResult;
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			Assert.fail("Error while calling the test program: " + e.getMessage());
		}
		
		Assert.assertNotNull("The test program never triggered an execution.", this.latestExecutionResult);
		
		// post-submit
		try {
			postSubmit();
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			Assert.fail("Post-submit work caused an error: " + e.getMessage());
		}
	}
	
	private static final class TestEnvironment extends ExecutionEnvironment {

		private final NepheleMiniCluster executor;
		
		private JobExecutionResult latestResult;
		
		
		private TestEnvironment(NepheleMiniCluster executor, int degreeOfParallelism) {
			this.executor = executor;
			setDegreeOfParallelism(degreeOfParallelism);
		}

		@Override
		public JobExecutionResult execute(String jobName) throws Exception {
			try {
				OptimizedPlan op = compileProgram(jobName);
				
				NepheleJobGraphGenerator jgg = new NepheleJobGraphGenerator();
				JobGraph jobGraph = jgg.compileJobGraph(op);
				
				JobClient client = this.executor.getJobClient(jobGraph);
				client.setConsoleStreamForReporting(AbstractTestBase.getNullPrintStream());
				JobExecutionResult result = client.submitJobAndWait();
				
				this.latestResult = result;
				return result;
			}
			catch (Exception e) {
				System.err.println(e.getMessage());
				e.printStackTrace();
				Assert.fail("Job execution failed!");
				return null;
			}
		}


		@Override
		public String getExecutionPlan() throws Exception {
			OptimizedPlan op = compileProgram("unused");
			
			PlanJSONDumpGenerator jsonGen = new PlanJSONDumpGenerator();
			return jsonGen.getOptimizerPlanAsJSON(op);
		}
		
		
		private OptimizedPlan compileProgram(String jobName) {
			Plan p = createProgramPlan(jobName);
			p.setDefaultParallelism(getDegreeOfParallelism());
			
			PactCompiler pc = new PactCompiler(new DataStatistics());
			return pc.compile(p);
		}
		
		private void setAsContext() {
			initializeContextEnvironment(this);
		}
	}
}