Line data Source code
1 : // Copyright (C) 2020 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance.ssh; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.sshd.SshCommand; 19 : import java.util.concurrent.CyclicBarrier; 20 : import org.kohsuke.args4j.Option; 21 : 22 1 : public abstract class TestCommand extends SshCommand { 23 1 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 24 1 : public static final CyclicBarrier syncPoint = new CyclicBarrier(2); 25 : 26 : @Option( 27 : name = "--duration", 28 : aliases = {"-d"}, 29 : required = true, 30 : usage = "Duration of the command execution in seconds") 31 : private int duration; 32 : 33 : @Override 34 : protected void run() throws UnloggedFailure, Failure, Exception { 35 1 : logger.atFine().log("Starting command."); 36 1 : if (isGraceful()) { 37 1 : enableGracefulStop(); 38 : } 39 : try { 40 1 : syncPoint.await(); 41 1 : Thread.sleep(duration * 1000); 42 1 : logger.atFine().log("Stopping command."); 43 0 : } catch (Exception e) { 44 0 : throw die("Command ended prematurely.", e); 45 1 : } 46 1 : } 47 : 48 : abstract boolean isGraceful(); 49 : }