package azkaban.execapp;

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

import azkaban.executor.ExecutorInfo;

public class StatisticsServletTest {
  private class MockStatisticsServlet extends ServerStatisticsServlet{
    /** */
    private static final long serialVersionUID = 1L;

    public  ExecutorInfo getStastics(){
      return cachedstats;
    }

    public  long getUpdatedTime(){
      return lastRefreshedTime;
    }

    public void callPopulateStatistics(){
       this.populateStatistics(false);
    }

    public void callFillCpuUsage(ExecutorInfo stats){
      this.fillCpuUsage(stats);}

    public void callFillRemainingMemoryPercent(ExecutorInfo stats){
        this.fillRemainingMemoryPercent(stats);}
  }
  private MockStatisticsServlet statServlet = new MockStatisticsServlet();

  @Test
  public void testFillMemory()  {
    ExecutorInfo stats = new ExecutorInfo();
    this.statServlet.callFillRemainingMemoryPercent(stats);
    // assume any machine that runs this test should
    // have bash and top available and at least got some remaining memory.
    Assert.assertTrue(stats.getRemainingMemoryInMB() > 0);
    Assert.assertTrue(stats.getRemainingMemoryPercent() > 0);
  }

  @Test
  public void testFillCpu()  {
    ExecutorInfo stats = new ExecutorInfo();
    this.statServlet.callFillCpuUsage(stats);
    Assert.assertTrue(stats.getCpuUsage() > 0);
  }

  @Test
  public void testPopulateStatistics()  {
    this.statServlet.callPopulateStatistics();
    Assert.assertNotNull(this.statServlet.getStastics());
    Assert.assertTrue(this.statServlet.getStastics().getRemainingMemoryInMB() > 0);
    Assert.assertTrue(this.statServlet.getStastics().getRemainingMemoryPercent() > 0);
    Assert.assertTrue(this.statServlet.getStastics().getCpuUsage() > 0);
  }

  @Test
  public void testPopulateStatisticsCache()  {
    this.statServlet.callPopulateStatistics();
    final long updatedTime = this.statServlet.getUpdatedTime();
    while (System.currentTimeMillis() - updatedTime < 1000){
      this.statServlet.callPopulateStatistics();
      Assert.assertEquals(updatedTime, this.statServlet.getUpdatedTime());
    }

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {}

    // make sure cache expires after timeout.
    this.statServlet.callPopulateStatistics();
    Assert.assertNotEquals(updatedTime, this.statServlet.getUpdatedTime());
  }
}

@@ -44,16 +44,41 @@
 Assert;%0A
+import org.junit.Ignore;%0A
 import o
@@ -132,16 +132,24 @@
 rInfo;%0A%0A
+@Ignore%0A
 public c
@@ -246,16 +246,17 @@
 sServlet
+ 
 %7B%0A    /*
@@ -325,17 +325,16 @@
  public 
- 
 Executor
@@ -347,24 +347,25 @@
 etStastics()
+ 
 %7B%0A      retu
@@ -398,17 +398,16 @@
  public 
- 
 long get
@@ -419,16 +419,17 @@
 edTime()
+ 
 %7B%0A      
@@ -501,24 +501,25 @@
 istics()
+ 
 %7B%0A      
  this.po
@@ -506,25 +506,24 @@
 s() %7B%0A      
- 
 this.populat
@@ -593,32 +593,33 @@
 cutorInfo stats)
+ 
 %7B%0A      this.fil
@@ -635,16 +635,21 @@
 (stats);
+%0A    
 %7D%0A%0A    p
@@ -709,20 +709,19 @@
 o stats)
+ 
 %7B%0A
-  
       th
@@ -757,16 +757,21 @@
 (stats);
+%0A    
 %7D%0A  %7D%0A  
@@ -882,17 +882,16 @@
 Memory()
- 
  %7B%0A    E
@@ -1279,17 +1279,16 @@
 illCpu()
- 
  %7B%0A    E
@@ -1472,17 +1472,16 @@
 istics()
- 
  %7B%0A    t
@@ -1880,17 +1880,16 @@
 sCache()
- 
  %7B%0A    t
@@ -2053,16 +2053,17 @@
  %3C 1000)
+ 
 %7B%0A      
@@ -2261,16 +2261,21 @@
 ion e) %7B
+%0A    
 %7D%0A%0A    /

Disable some system specific tests that may mail on the Traivs boxes during build.