protected void mockCollectorWithDefaults() { MemoryUsage heapMemoryUsage = Mockito.mock(MemoryUsage.class); when(heapMemoryUsage.getUsed()).thenReturn(0L); when(heapMemoryUsage.getCommitted()).thenReturn(0L); MemoryUsage nonHeapMemoryUsage = Mockito.mock(MemoryUsage.class); when(nonHeapMemoryUsage.getUsed()).thenReturn(0L); when(nonHeapMemoryUsage.getCommitted()).thenReturn(0L); when(this.osBean.getFreePhysicalMemorySize()).thenReturn(0L); when(this.osBean.getFreeSwapSpaceSize()).thenReturn(0L); when(this.osBean.getCommittedVirtualMemorySize()).thenReturn(0L); when(this.memoryBean.getHeapMemoryUsage()).thenReturn(heapMemoryUsage); when(this.memoryBean.getNonHeapMemoryUsage()).thenReturn(nonHeapMemoryUsage); } }
/** * {@inheritDoc} */ @Override public void gather() { // The timestamp is set in the {@link CpuInformation#reset()} to avoid multiple renewal. It // will not be set on the first execution of {@link CpuInformation#gather()}, but shortly // before. float cpuUsage = this.getOsBean().retrieveCpuUsage(); long cpuTime = this.getOsBean().getProcessCpuTime(); this.cpuInformationData.incrementCount(); this.cpuInformationData.updateProcessCpuTime(cpuTime); this.cpuInformationData.addCpuUsage(cpuUsage); if (cpuUsage < this.cpuInformationData.getMinCpuUsage()) { this.cpuInformationData.setMinCpuUsage(cpuUsage); } if (cpuUsage > this.cpuInformationData.getMaxCpuUsage()) { this.cpuInformationData.setMaxCpuUsage(cpuUsage); } }
when(this.osBean.getTotalPhysicalMemorySize()).thenReturn(2L); when(this.osBean.getTotalSwapSpaceSize()).thenReturn(3L); when(this.osBean.getAvailableProcessors()).thenReturn(4); when(this.osBean.getArch()).thenReturn("5"); when(this.osBean.getName()).thenReturn("6"); when(this.osBean.getVersion()).thenReturn("7"); when(this.osBean.getTotalPhysicalMemorySize()).thenReturn(200L); when(this.osBean.getTotalSwapSpaceSize()).thenReturn(300L); when(this.osBean.getAvailableProcessors()).thenReturn(400); when(this.osBean.getArch()).thenReturn("500"); when(this.osBean.getName()).thenReturn("600"); when(this.osBean.getVersion()).thenReturn("700");
@Test void updateProcessCpuTime() { when(this.osBean.getProcessCpuTime()).thenReturn(10L).thenReturn(9L).thenReturn(11L).thenReturn(10L); this.cut.gather(); this.cut.gather(); this.cut.gather(); this.cut.gather(); CpuInformationData collector = (CpuInformationData) this.cut.get(); assertThat(collector.getProcessCpuTime(), is(11L)); }
@Test void comittedVirtualMemSizeIsCalculated() { this.mockCollectorWithDefaults(); when(this.osBean.getCommittedVirtualMemorySize()).thenReturn(10L).thenReturn(9L).thenReturn(11L).thenReturn(10L); this.cut.gather(); this.cut.gather(); this.cut.gather(); this.cut.gather(); MemoryInformationData collector = (MemoryInformationData) this.cut.get(); assertThat(collector.getMinComittedVirtualMemSize(), is(9L)); assertThat(collector.getMaxComittedVirtualMemSize(), is(11L)); assertThat(collector.getTotalComittedVirtualMemSize(), is(40L)); }
@Test void freeMemoryIsCalculated() { this.mockCollectorWithDefaults(); when(this.osBean.getFreePhysicalMemorySize()).thenReturn(10L).thenReturn(9L).thenReturn(11L).thenReturn(10L); this.cut.gather(); this.cut.gather(); this.cut.gather(); this.cut.gather(); MemoryInformationData collector = (MemoryInformationData) this.cut.get(); assertThat(collector.getMinFreePhysMemory(), is(9L)); assertThat(collector.getMaxFreePhysMemory(), is(11L)); assertThat(collector.getTotalFreePhysMemory(), is(40L)); }
@Test void freeSwapSpaceIsCalculated() { this.mockCollectorWithDefaults(); when(this.osBean.getFreeSwapSpaceSize()).thenReturn(10L).thenReturn(9L).thenReturn(11L).thenReturn(10L); this.cut.gather(); this.cut.gather(); this.cut.gather(); this.cut.gather(); MemoryInformationData collector = (MemoryInformationData) this.cut.get(); assertThat(collector.getMinFreeSwapSpace(), is(9L)); assertThat(collector.getMaxFreeSwapSpace(), is(11L)); assertThat(collector.getTotalFreeSwapSpace(), is(40L)); }
when(this.osBean.getTotalPhysicalMemorySize()).thenReturn(2L); when(this.osBean.getTotalSwapSpaceSize()).thenReturn(3L); when(this.osBean.getAvailableProcessors()).thenReturn(4); when(this.osBean.getArch()).thenReturn("5"); when(this.osBean.getName()).thenReturn("6"); when(this.osBean.getVersion()).thenReturn("7");
this.systemInformationData.setTimeStamp(timestamp); this.systemInformationData.setTotalPhysMemory(this.getOsBean().getTotalPhysicalMemorySize()); this.systemInformationData.setTotalSwapSpace(this.getOsBean().getTotalSwapSpaceSize()); this.systemInformationData.setAvailableProcessors(this.getOsBean().getAvailableProcessors()); this.systemInformationData.setArchitecture(this.getOsBean().getArch()); this.systemInformationData.setOsName(this.getOsBean().getName()); this.systemInformationData.setOsVersion(this.getOsBean().getVersion());
long freePhysMemory = this.getOsBean().getFreePhysicalMemorySize(); long freeSwapSpace = this.getOsBean().getFreeSwapSpaceSize(); long comittedVirtualMemSize = this.getOsBean().getCommittedVirtualMemorySize(); long usedHeapMemorySize = this.getMemoryBean().getHeapMemoryUsage().getUsed(); long comittedHeapMemorySize = this.getMemoryBean().getHeapMemoryUsage().getCommitted();