view src/org/thermostat/qa2/framework/services/AbstractService.java @ 192:5d335809cd51

Test fixes changes: - Broken CliClientAgentInfoTest is now using more reliable way to find agent-id. - Now, when pattern is not found in tomcat log(s), it is printed, so that problem can be more easily found and fixed. - Fixing problem with race condition caused by asynchronous nature of shutdown tomcat script - Making ShellService class cleaner and more robust
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 14 Jul 2015 19:00:56 +0200
parents 0d34a379de82
children 0351a5e0ca27
line wrap: on
line source

/*
 ThermostatQA - test framework for Thermostat Monitoring Tool

 Copyright 2015 Red Hat, Inc.

 This file is part of ThermostatQA

 ThermostatQA is distributed under the GNU General Public License,
 version 2 or any later version (with a special exception described
 below, commonly known as the "Classpath Exception").

 A copy of GNU General Public License (GPL) is included in this
 distribution, in the file COPYING.

 Linking ThermostatQA code with other modules is making a combined work
 based on ThermostatQA.  Thus, the terms and conditions of the GPL
 cover the whole combination.

 As a special exception, the copyright holders of ThermostatQA give you
 permission to link this code with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on ThermostatQA code.  If you modify ThermostatQA, you may
 extend this exception to your version of the software, but you are
 not obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
 */
package org.thermostat.qa2.framework.services;

import org.thermostat.qa2.framework.NativeProcess;
import org.thermostat.qa2.framework.TestRunner;
import org.thermostat.qa2.framework.utils.CommonUtilities;

/**
 *
 * @author Zdeněk Žamberský
 */
public abstract class AbstractService implements Service {

    private final Object lock = new Object();
    boolean isRunning = false;

    public abstract String getServiceName() throws Exception;

    public abstract void startServiceImpl() throws Exception;

    public abstract void stopServiceImpl() throws Exception;

    @Override
    public boolean isRunning() {
        synchronized (lock) {
            return isRunning;
        }
    }

    protected void setRunning(boolean running) {
        synchronized (lock) {
            isRunning = running;
        }
    }

    public boolean isShutdownHookEnabled() {
        return true;
    }

    @Override
    public void start() throws Exception {
        CommonUtilities.printHeading("Starting " + getServiceName() + " ...");
        if (isRunning()) {
            System.out.println("INFO: " + getServiceName() + " is already running");
            return;
        }
        if (isShutdownHookEnabled()) {
            TestRunner.registerServiceCleanup(this);
        }
        try {
            startServiceImpl();
        } catch (Exception e) {
            System.out.println("INFO: Error starting " + getServiceName() + "! ");
            setRunning(false);
            throw e;
        }
    }

    @Override
    public void stop() throws Exception {
        CommonUtilities.printHeading("Stopping " + getServiceName() + " ...");
        if (!isRunning()) {
            System.out.println("INFO: " + getServiceName() + " is not running");
            return;
        }
        try {
            stopServiceImpl();
        } catch (Exception e) {
            System.out.println("INFO: Error stopping " + getServiceName() + "! ");
            throw e;
        } finally {
            TestRunner.unregisterServiceCleanup(this);
        }
    }

    protected void waitForListeningPort(int port, int timeout, boolean start) throws Exception {
        for (int i = 0; i < timeout; ++i) {
            NativeProcess process = new NativeProcess("sh", "-c", "netstat -tln | grep -q :" + port + " &> /dev/null");
            process.setLabel("Port checker");
            process.start();
            int ret = process.waitForRaw();

            //int ret = NativeProcess.runRaw("sh", "-c", "netstat -tln | grep -q :" + port + " &> /dev/null");
            if (start) {
                if (ret == 0) {
                    return;
                }
            } else {
                if (ret != 0) {
                    return;
                }
            }
            CommonUtilities.sleep(1000);
            if (start && !isRunning()) {
                throw new Exception(getServiceName() + ": stopped while waiting for listening port " + port);
            }
        }
        throw new Exception(getServiceName() + ": expired timeout " + timeout + " s waitng for listening port" + port);
    }

}