<commit_before>package com.krillsson.sysapi.core.metrics.defaultimpl;

import com.krillsson.sysapi.core.TickManager;
import com.krillsson.sysapi.core.domain.cpu.CpuHealth;
import com.krillsson.sysapi.core.domain.cpu.CpuLoad;
import com.krillsson.sysapi.util.Utils;
import org.junit.Before;
import org.junit.Test;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.Sensors;
import oshi.software.os.OperatingSystem;

import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.*;

public class DefaultCpuMetricsTest {
    DefaultCpuMetrics infoProvider;
    HardwareAbstractionLayer hal;
    OperatingSystem os;
    Utils utils;
    Sensors sensors;
    TickManager tickManager;
    DefaultCpuSensors cpuSensors;

    CentralProcessor centralProcessor;

    @Before
    public void setUp() throws Exception {
        hal = mock(HardwareAbstractionLayer.class);
        os = mock(OperatingSystem.class);
        utils = mock(Utils.class);
        sensors = mock(Sensors.class);
        tickManager = mock(TickManager.class);

        centralProcessor = mock(CentralProcessor.class);
        when(hal.getSensors()).thenReturn(sensors);
        infoProvider = new DefaultCpuMetrics(hal, os, cpuSensors, utils, tickManager);

    }


    @Test
    public void cpuLoadHappyPath() throws Exception {
        long[][] initialTicks = {{1402976, 0, 448399, 2780631, 0, 0, 0, 0}};
        long[][] secondTicks = {{1416739, 0, 452315, 2799166, 0, 0, 0, 0}};
        when(hal.getProcessor()).thenReturn(centralProcessor);
        when(centralProcessor.getLogicalProcessorCount()).thenReturn(1);
        when(centralProcessor.getProcessorCpuLoadTicks()).thenReturn(initialTicks, secondTicks);
        when(utils.currentSystemTime()).thenReturn(1000L, 2000L);

        CpuLoad cpuLoad = infoProvider.cpuLoad();

        assertEquals(38.0, cpuLoad.getCoreLoads().get(0).getUser(), 0.0);
        assertEquals(51.18, cpuLoad.getCoreLoads().get(0).getIdle(), 0.0);
        assertEquals(10.81, cpuLoad.getCoreLoads().get(0).getSys(), 0.0);
        assertNotNull(cpuLoad);
        verify(utils, times(1)).sleep(anyLong());
    }

    @Test
    public void cpuLoadOnlySleepsOnceIfInsideSamplingThreshold() throws Exception {
        long[][] initialTicks = {{1402976, 0, 448399, 2780631, 0, 0, 0, 0}};
        long[][] secondTicks = {{1416739, 0, 452315, 2799166, 0, 0, 0, 0}};
        long[][] thirdTicks = {{1466769, 0, 472315, 2899166, 0, 0, 0, 0}};
        when(hal.getProcessor()).thenReturn(centralProcessor);
        when(centralProcessor.getLogicalProcessorCount()).thenReturn(1);
        when(centralProcessor.getProcessorCpuLoadTicks()).thenReturn(initialTicks, secondTicks, thirdTicks);
        when(utils.currentSystemTime()).thenReturn(1000L, 2000L, 3000L);

        CpuLoad cpuLoad = infoProvider.cpuLoad();
        CpuLoad secondCpuLoad = infoProvider.cpuLoad();


        assertFalse(cpuLoad.getCoreLoads().get(0).getUser() == secondCpuLoad.getCoreLoads().get(0).getUser());
        assertFalse(cpuLoad.getCoreLoads().get(0).getIdle() == secondCpuLoad.getCoreLoads().get(0).getIdle());
        assertFalse(cpuLoad.getCoreLoads().get(0).getSys() == secondCpuLoad.getCoreLoads().get(0).getSys());

        verify(utils, times(1)).sleep(anyLong());
    }

    @Test
    public void cpuHealthHappyPath() {
        when(sensors.getCpuTemperature()).thenReturn(30.0);
        when(sensors.getCpuVoltage()).thenReturn(1.35);
        when(sensors.getFanSpeeds()).thenReturn(new int[]{1200});
        when(sensors.getFanSpeeds()).thenReturn(new int[]{1200});<commit_msg>Fix DefaultCpuMetrics unit tests<commit_after>package com.krillsson.sysapi.core.metrics.defaultimpl;

import com.krillsson.sysapi.core.TickManager;
import com.krillsson.sysapi.core.domain.cpu.CpuHealth;
import com.krillsson.sysapi.core.domain.cpu.CpuLoad;
import com.krillsson.sysapi.util.Utils;
import org.junit.Before;
import org.junit.Test;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.Sensors;
import oshi.software.os.OperatingSystem;

import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.*;

public class DefaultCpuMetricsTest {
    DefaultCpuMetrics infoProvider;
    HardwareAbstractionLayer hal;
    OperatingSystem os;
    Utils utils;
    Sensors sensors;
    TickManager tickManager;
    DefaultCpuSensors cpuSensors;

    CentralProcessor centralProcessor;

    @Before
    public void setUp() throws Exception {
        hal = mock(HardwareAbstractionLayer.class);
        os = mock(OperatingSystem.class);
        utils = mock(Utils.class);
        sensors = mock(Sensors.class);
        tickManager = mock(TickManager.class);

        centralProcessor = mock(CentralProcessor.class);
        when(hal.getSensors()).thenReturn(sensors);
        when(sensors.getCpuTemperature()).thenReturn(30.0);
        when(sensors.getCpuVoltage()).thenReturn(1.35);
        when(sensors.getFanSpeeds()).thenReturn(new int[]{1200});
        cpuSensors = new DefaultCpuSensors(hal);
        infoProvider = new DefaultCpuMetrics(hal, os, cpuSensors, utils, tickManager);

    }


    @Test
    public void cpuLoadHappyPath() throws Exception {
        long[][] initialTicks = {{1402976, 0, 448399, 2780631, 0, 0, 0, 0}};
        long[][] secondTicks = {{1416739, 0, 452315, 2799166, 0, 0, 0, 0}};
        when(hal.getProcessor()).thenReturn(centralProcessor);
        when(centralProcessor.getLogicalProcessorCount()).thenReturn(1);
        when(centralProcessor.getProcessorCpuLoadTicks()).thenReturn(initialTicks, secondTicks);

        infoProvider.onTick();
        CpuLoad cpuLoad = infoProvider.cpuLoad();

        assertEquals(38.0, cpuLoad.getCoreLoads().get(0).getUser(), 0.0);
        assertEquals(51.18, cpuLoad.getCoreLoads().get(0).getIdle(), 0.0);
        assertEquals(10.81, cpuLoad.getCoreLoads().get(0).getSys(), 0.0);
        assertNotNull(cpuLoad);
        verify(utils, times(1)).sleep(anyLong());
    }

    @Test
    public void cpuLoadOnlySleepsOnceIfInsideSamplingThreshold() throws Exception {
        long[][] initialTicks = {{1402976, 0, 448399, 2780631, 0, 0, 0, 0}};
        long[][] secondTicks = {{1416739, 0, 452315, 2799166, 0, 0, 0, 0}};
        long[][] thirdTicks = {{1466769, 0, 472315, 2899166, 0, 0, 0, 0}};
        when(hal.getProcessor()).thenReturn(centralProcessor);
        when(centralProcessor.getLogicalProcessorCount()).thenReturn(1);
        when(centralProcessor.getProcessorCpuLoadTicks()).thenReturn(initialTicks, secondTicks, thirdTicks);
        when(utils.currentSystemTime()).thenReturn(1000L, 2000L, 3000L);

        infoProvider.onTick();
        CpuLoad cpuLoad = infoProvider.cpuLoad();
        infoProvider.onTick();
        CpuLoad secondCpuLoad = infoProvider.cpuLoad();


        assertFalse(cpuLoad.getCoreLoads().get(0).getUser() == secondCpuLoad.getCoreLoads().get(0).getUser());
        assertFalse(cpuLoad.getCoreLoads().get(0).getIdle() == secondCpuLoad.getCoreLoads().get(0).getIdle());
        assertFalse(cpuLoad.getCoreLoads().get(0).getSys() == secondCpuLoad.getCoreLoads().get(0).getSys());

        verify(utils, times(1)).sleep(anyLong());
    }

    @Test
    public void cpuHealthHappyPath() {
        long[][] initialTicks = {{1402976, 0, 448399, 2780631, 0, 0, 0, 0}};
        long[][] secondTicks = {{1416739, 0, 452315, 2799166, 0, 0, 0, 0}};
        when(hal.getProcessor()).thenReturn(centralProcessor);
        when(centralProcessor.getLogicalProcessorCount()).thenReturn(1);
        when(centralProcessor.getProcessorCpuLoadTicks()).thenReturn(initialTicks, secondTicks);

        infoProvider.onTick();
{'fraction_non_alphanumeric': 0.1361302442078898, 'fraction_numerical': 0.05222291797119599, 'mean_word_length': 4.064045656309449, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492527', 'n_tokens_mistral': 2829, 'n_tokens_neox': 2488, 'n_words': 423}