view vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/VmMemoryVmListenerTest.java @ 894:0ba74f790a8a

Create VM Memory agent and common bundles This commit extracts VM Memory data collection from SystemBackend and related classes into a vm-memory-agent bundle. It also moves the VmMemoryStatDAO from common-core into a vm-memory-common bundle that registers the DAO once Storage is available. This also removes the DAO from DAOFactory. This also temporarily adds the vm-memory-common bundle as a dependency to client-cli until my later dynamic vm-stat patch. Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-January/004995.html
author Elliott Baron <ebaron@redhat.com>
date Mon, 07 Jan 2013 15:50:03 -0500
parents
children 259c60624174
line wrap: on
line source

/*
 * Copyright 2013 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.memory.agent.internal;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import sun.jvmstat.monitor.MonitorException;
import sun.jvmstat.monitor.MonitoredVm;

import com.redhat.thermostat.storage.model.VmMemoryStat;
import com.redhat.thermostat.storage.model.VmMemoryStat.Generation;
import com.redhat.thermostat.storage.model.VmMemoryStat.Space;
import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO;

public class VmMemoryVmListenerTest {
    private static final String[] GEN_NAMES = new String[] { "Gen1", "Gen2" };
    private static final Long[] GEN_CAPS = new Long[] { 500L, 1000L };
    private static final Long[] GEN_MAX_CAPS = new Long[] { 5000L, 10000L };
    private static final String[] GEN_GCS = new String[] { "GC1", "GC2" };
    private static final Long[] GEN_SPACES = new Long[] { 2L, 1L };
    private static final String[][] SPACE_NAME = new String[][] { 
        { "Space1", "Space2" },
        { "Space3" }
    };
    private static final Long[][] SPACE_CAPS = new Long[][] {
        { 225L, 275L },
        { 1000L }
    };
    private static final Long[][] SPACE_MAX_CAPS = new Long[][] {
        { 2250L, 2750L },
        { 10000L }
    };
    private static final Long[][] SPACE_USED = new Long[][] {
        { 125L, 175L },
        { 900L }
    };
    
    private VmMemoryVmListener vmListener;
    private MonitoredVm monitoredVm;
    private VmMemoryDataExtractor extractor;
    private VmMemoryStatDAO vmMemoryStatDAO;
    
    @Before
    public void setup() throws MonitorException {
        final int numGens = 2;
        vmMemoryStatDAO = mock(VmMemoryStatDAO.class);
        vmListener = new VmMemoryVmListener(vmMemoryStatDAO, 0);
        
        monitoredVm = mock(MonitoredVm.class);
        extractor = mock(VmMemoryDataExtractor.class);
        
        for (int i = 0; i < numGens; i++) {
            mockGenerationName(i);
            mockGenerationCapacity(i);
            mockGenerationMaxCapacity(i);
            mockGenerationGC(i);
            mockTotalSpaces(i);
            int numSpaces = GEN_SPACES[i].intValue();
            for (int j = 0; j < numSpaces; j++) {
                mockSpaceName(i, j);
                mockSpaceCapacity(i, j);
                mockSpaceMaxCapacity(i, j);
                mockSpaceUsed(i, j);
            }
        }
    }

    private void mockGenerationName(int gen) throws MonitorException {
        when(extractor.getGenerationName(gen)).thenReturn(GEN_NAMES[gen]);
    }
    
    private void mockGenerationCapacity(int gen) throws MonitorException {
        when(extractor.getGenerationCapacity(gen)).thenReturn(GEN_CAPS[gen]);
    }

    private void mockGenerationMaxCapacity(int gen) throws MonitorException {
        when(extractor.getGenerationMaxCapacity(gen)).thenReturn(GEN_MAX_CAPS[gen]);
    }
    
    private void mockGenerationGC(int gen) throws MonitorException {
        when(extractor.getGenerationCollector(gen)).thenReturn(GEN_GCS[gen]);
    }
    
    private void mockTotalSpaces(int gen) throws MonitorException {
        when(extractor.getTotalSpaces(gen)).thenReturn(GEN_SPACES[gen]);
    }
    
    private void mockSpaceName(int gen, int space) throws MonitorException {
        when(extractor.getSpaceName(gen, space)).thenReturn(SPACE_NAME[gen][space]);
    }
    
    private void mockSpaceCapacity(int gen, int space) throws MonitorException {
        when(extractor.getSpaceCapacity(gen, space)).thenReturn(SPACE_CAPS[gen][space]);
    }
    
    private void mockSpaceMaxCapacity(int gen, int space) throws MonitorException {
        when(extractor.getSpaceMaxCapacity(gen, space)).thenReturn(SPACE_MAX_CAPS[gen][space]);
    }
    
    private void mockSpaceUsed(int gen, int space) throws MonitorException {
        when(extractor.getSpaceUsed(gen, space)).thenReturn(SPACE_USED[gen][space]);
    }
    
    @Test
    public void testRecordMemoryStat() {
        vmListener.recordMemoryStat(monitoredVm, extractor);
        ArgumentCaptor<VmMemoryStat> captor = ArgumentCaptor.forClass(VmMemoryStat.class);
        verify(vmMemoryStatDAO).putVmMemoryStat(captor.capture());
        VmMemoryStat memoryStat = captor.getValue();
        
        Generation[] gens = memoryStat.getGenerations();
        for (int i = 0; i < gens.length; i++) {
            Generation gen = gens[i];
            assertEquals(GEN_NAMES[i], gen.getName());
            assertEquals(GEN_CAPS[i], (Long) gen.getCapacity());
            assertEquals(GEN_MAX_CAPS[i], (Long) gen.getMaxCapacity());
            assertEquals(GEN_GCS[i], gen.getCollector());
            assertEquals(GEN_SPACES[i], Long.valueOf(gen.getSpaces().length));
            Space[] spaces = gen.getSpaces();
            for (int j = 0; j < spaces.length; j++) {
                Space space = spaces[j];
                assertEquals(SPACE_NAME[i][j], space.getName());
                assertEquals(SPACE_CAPS[i][j], (Long) space.getCapacity());
                assertEquals(SPACE_MAX_CAPS[i][j], (Long) space.getMaxCapacity());
                assertEquals(SPACE_USED[i][j], (Long) space.getUsed());
            }
        }
    }
}