view vm-gc/command/src/test/java/com/redhat/thermostat/vm/gc/command/internal/GCCommandTest.java @ 2035:fa302aa73bae

Improve gc-command wait-for-completion logic Reusing a listener instance leads to reuse of the same CountDownLatch, which does not work as expected in a Thermostat shell use case. Additionally, a 1 second latch timeout leads to very frequent timeouts before the request response message is received, defeating the purpose of the latch. PR3241 Reviewed-by: jerboaa, omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-November/021731.html
author Andrew Azores <aazores@redhat.com>
date Tue, 22 Nov 2016 12:48:19 -0500
parents 31f274ab27a5
children b188a7f445f2
line wrap: on
line source

/*
 * Copyright 2012-2016 Red Hat, Inc.
 *
 * This file is part of Thermostat.
 *
 * Thermostat is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2, or (at your
 * option) any later version.
 *
 * Thermostat is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Thermostat; see the file COPYING.  If not see
 * <http://www.gnu.org/licenses/>.
 *
 * Linking this code with other modules is making a combined work
 * based on this code.  Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this code 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 this code.  If you modify
 * this code, you may extend this exception to your version of the
 * library, but you are not obligated to do so.  If you do not wish
 * to do so, delete this exception statement from your version.
 */

package com.redhat.thermostat.vm.gc.command.internal;

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.redhat.thermostat.common.cli.CommandContext;
import com.redhat.thermostat.common.cli.CommandException;
import com.redhat.thermostat.common.cli.SimpleArguments;
import com.redhat.thermostat.gc.remote.common.GCRequest;
import com.redhat.thermostat.storage.core.HostRef;
import com.redhat.thermostat.storage.core.VmId;
import com.redhat.thermostat.storage.core.VmRef;
import com.redhat.thermostat.storage.dao.AgentInfoDAO;
import com.redhat.thermostat.storage.dao.VmInfoDAO;
import com.redhat.thermostat.storage.model.AgentInformation;
import com.redhat.thermostat.storage.model.VmInfo;
import com.redhat.thermostat.test.TestCommandContextFactory;

import java.io.PrintStream;

public class GCCommandTest {

    private GCCommand command;
    private TestCommandContextFactory cmdCtxFactory;

    private VmInfoDAO vmInfoDAO;
    private AgentInfoDAO agentInfoDAO;
    private GCRequest gcRequest;

    @Before
    public void setup() {
        cmdCtxFactory = new TestCommandContextFactory();

        vmInfoDAO = mock(VmInfoDAO.class);
        agentInfoDAO = mock(AgentInfoDAO.class);
        gcRequest = mock(GCRequest.class);

        GCCommandListenerFactory listenerFactory = mock(GCCommandListenerFactory.class);
        GCCommandListener listener = mock(GCCommandListener.class);
        when(listenerFactory.createListener(any(PrintStream.class), any(PrintStream.class))).thenReturn(listener);

        command = new GCCommand(listenerFactory);
    }

    @Test
    public void testPerformGC() throws Exception {
        String vmId = "liveVM";

        VmRef vmRef = new VmRef(new HostRef("dummy", "dummy"), "liveVM", -1, "dummy");

        VmInfo vmInfo = new VmInfo("dummy", "liveVM", -1, 0l, 0l, null, null, null, null, "dummy", "dummy", null, null, null, null, null, 0l, null) {
            
            @Override
            public AliveStatus isAlive(AgentInformation agentInfo) {
                return AliveStatus.RUNNING;
            }
            
        };

        when(vmInfoDAO.getVmInfo(any(VmId.class))).thenReturn(vmInfo);

        final boolean[] complete = {false};
        doAnswer(new Answer() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                complete[0] = true;
                return null;
            }
        }).when(gcRequest).sendGCRequestToAgent(eq(vmRef), eq(agentInfoDAO), any(GCCommandListener.class));

        CommandContext context = createVmIdArgs(vmId);

        setServices();

        command.run(context);

        assertTrue(complete[0]);
    }

    @Test(expected = CommandException.class)
    public void testPerformGCOnMissingVM() throws Exception {
        String vmId = "nonexistentVM";

        CommandContext context = createVmIdArgs(vmId);

        setServices();

        command.run(context);
    }
    
    @Test(expected = CommandException.class)
    public void testPerformGCOnNotLivingVM() throws Exception {
        VmInfo vmInfo = new VmInfo("foo-agent", "my-vm-id", -1, 0l, 0l, null, null, null, null, "dummy", null, null, null, null, null, null, 0l, null) {
            @Override
            public AliveStatus isAlive(AgentInformation agentInfo) {
                return AliveStatus.EXITED;
            }
        };
        String vmId = "my-vm-id";
        VmId myId = new VmId(vmId);
        when(vmInfoDAO.getVmInfo(eq(myId))).thenReturn(vmInfo);
        
        CommandContext context = createVmIdArgs(vmId);

        setServices();

        command.run(context);
    }

    @Test(expected = CommandException.class)
    public void testGCWithoutServices() throws Exception {
        String vmId = "liveVM";

        CommandContext context = createVmIdArgs(vmId);

        command.run(context);
    }

    private CommandContext createVmIdArgs(String vmId) {
        SimpleArguments args = new SimpleArguments();
        args.addArgument("vmId", vmId);
        return cmdCtxFactory.createContext(args);
    }

    private void setServices() {
        command.setServices(gcRequest, agentInfoDAO, vmInfoDAO);
    }

}