Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remibergsma authored and sanderv32 committed Dec 6, 2019
1 parent c37e51e commit c4796d8
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions cosmic-common/src/test/java/com/cloud/utils/linux/MemStatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,57 @@

import java.util.Scanner;

import org.junit.Assert;
import org.junit.Test;
import org.junit.Assert;
import org.junit.Test;

import java.util.Scanner;

public class MemStatTest {
final String memInfo = "MemTotal: 5830236 kB\n" +
"MemFree: 156752 kB\n" +
"Buffers: 326836 kB\n" +
"Cached: 2606764 kB\n" +
"SwapCached: 0 kB\n" +
"Active: 4260808 kB\n" +
"Inactive: 949392 kB\n";

@Test
public void getMemInfoParseTest() {
final String memInfo = "MemTotal: 5830236 kB\n" +
"MemFree: 156752 kB\n" +
"Buffers: 326836 kB\n" +
"Cached: 2606764 kB\n" +
"SwapCached: 0 kB\n" +
"Active: 4260808 kB\n" +
"Inactive: 949392 kB\n";

MemStat memStat = null;
try {
memStat = new MemStat();
} catch (final RuntimeException ex) {
} catch (RuntimeException ex) {
// If test isn't run on linux we'll fail creation of linux-specific MemStat class due
// to dependency on /proc/meminfo if we don't catch here.
// We are really only interested in testing the parsing algorithm and getters.
if (memStat == null) {
throw ex;
}
}
final Scanner scanner = new Scanner(memInfo);
Scanner scanner = new Scanner(memInfo);
memStat.parseFromScanner(scanner);

Assert.assertEquals(memStat.getTotal(), Long.valueOf(5970161664L));
Assert.assertEquals(memStat.getAvailable(), Long.valueOf(2829840384L));
Assert.assertEquals(memStat.getFree(), Long.valueOf(160514048L));
Assert.assertEquals(memStat.getCache(), Long.valueOf(2669326336L));
}

@Test
public void reservedMemoryTest() {
MemStat memStat = null;
try {
memStat = new MemStat(1024);
} catch (RuntimeException ex) {
if (memStat == null) {
throw ex;
}
}
Scanner scanner = new Scanner(memInfo);
memStat.parseFromScanner(scanner);

Assert.assertEquals(memStat.getTotal(), Double.valueOf(5830236));
Assert.assertEquals(memStat.getAvailable(), Double.valueOf(2763516));
Assert.assertEquals(memStat.getFree(), Double.valueOf(156752));
Assert.assertEquals(memStat.getCache(), Double.valueOf(2606764));
Assert.assertEquals(memStat.getTotal(), Long.valueOf(5970160640L));
}
}
}

0 comments on commit c4796d8

Please sign in to comment.