Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.launcher;
0019 
0020 import java.time.Duration;
0021 
0022 import org.junit.After;
0023 import org.slf4j.bridge.SLF4JBridgeHandler;
0024 import static org.junit.Assert.*;
0025 
0026 /**
0027  * Handles configuring the JUL -> SLF4J bridge, and provides some utility methods for tests.
0028  */
0029 class BaseSuite {
0030 
0031   static {
0032     SLF4JBridgeHandler.removeHandlersForRootLogger();
0033     SLF4JBridgeHandler.install();
0034   }
0035 
0036   @After
0037   public void postChecks() {
0038     LauncherServer server = LauncherServer.getServer();
0039     if (server != null) {
0040       // Shut down the server to clean things up for the next test.
0041       try {
0042         server.close();
0043       } catch (Exception e) {
0044         // Ignore.
0045       }
0046     }
0047     assertNull(server);
0048   }
0049 
0050   protected void waitFor(final SparkAppHandle handle) throws Exception {
0051     try {
0052       eventually(Duration.ofSeconds(10), Duration.ofMillis(10), () -> {
0053         assertTrue("Handle is not in final state.", handle.getState().isFinal());
0054       });
0055     } finally {
0056       if (!handle.getState().isFinal()) {
0057         handle.kill();
0058       }
0059     }
0060 
0061     // Wait until the handle has been marked as disposed, to make sure all cleanup tasks
0062     // have been performed.
0063     AbstractAppHandle ahandle = (AbstractAppHandle) handle;
0064     eventually(Duration.ofSeconds(10), Duration.ofMillis(10), () -> {
0065       assertTrue("Handle is still not marked as disposed.", ahandle.isDisposed());
0066     });
0067   }
0068 
0069   /**
0070    * Call a closure that performs a check every "period" until it succeeds, or the timeout
0071    * elapses.
0072    */
0073   protected void eventually(Duration timeout, Duration period, Runnable check) throws Exception {
0074     assertTrue("Timeout needs to be larger than period.", timeout.compareTo(period) > 0);
0075     long deadline = System.nanoTime() + timeout.toNanos();
0076     int count = 0;
0077     while (true) {
0078       try {
0079         count++;
0080         check.run();
0081         return;
0082       } catch (Throwable t) {
0083         if (System.nanoTime() >= deadline) {
0084           String msg = String.format("Failed check after %d tries: %s.", count, t.getMessage());
0085           throw new IllegalStateException(msg, t);
0086         }
0087         Thread.sleep(period.toMillis());
0088       }
0089     }
0090   }
0091 
0092 }