/*******************************************************************************
 * Copyright (c) 2012 Oak Ridge National Laboratory.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.alarm.beast.server;

import static org.junit.Assert.assertEquals;

import org.csstudio.apputil.time.BenchmarkTimer;
import org.junit.Test;

/** JUnit test of the NagTimer
 *  @author Kay Kasemir
 */
@SuppressWarnings("nls")
public class NagTimerUnitTest implements NagTimerHandler
{
    private volatile int active = 2;
    private int nags = 0;

    @Override
    public int getActiveAlarmCount()
    {
        return active;
    }

    @Override
    public void nagAboutActiveAlarms(final int active)
    {
        System.out.println("There are " + active + " active alarms");
        synchronized (this)
        {
            ++nags;
            notifyAll();
        }
    }

    @Test(timeout=8000)
    public void testNagTimer() throws Exception
    {
        final NagTimer nag_timer = new NagTimer(1000, this);
        nag_timer.start();
        BenchmarkTimer t = new BenchmarkTimer();

        synchronized (this)
        {
            // Initially, nothing
            assertEquals(0, nags);

            // Expect nag after one second
            wait(1100);
            assertEquals(1, nags);
        }
        t.stop();
        System.out.println("Time: " + t);
        assertEquals(1.0, t.getSeconds(), 0.1);

        // No more if we keep resetting
        Thread.sleep(500);
        nag_timer.reset();
        Thread.sleep(500);
        nag_timer.reset();
        Thread.sleep(500);
        nag_timer.reset();

        // But then time out again
        t.start();
        synchronized (this)
        {
            // Initially, nothing new
            assertEquals(1, nags);

            // Expect nag after one second
            wait(1100);
            assertEquals(2, nags);
        }
        t.stop();
        System.out.println("Time: " + t);
        assertEquals(1.0, t.getSeconds(), 0.1);

        // .. and again
        t.start();
        synchronized (this)
        {
            wait(1100);
            assertEquals(3, nags);
        }
        t.stop();
        System.out.println("Time: " + t);
        assertEquals(1.0, t.getSeconds(), 0.1);

        // Clear all alarms
        active = 0;
        Thread.sleep(1100);
        synchronized (this)
        {   // Should not see another
            assertEquals(3, nags);
        }

        // and then again have alarms
        active = 1;
        Thread.sleep(1100);
        synchronized (this)
        {   // Should have received another
            assertEquals(4, nags);
        }

        nag_timer.cancel();
    }
}

@@ -1232,24 +1232,58 @@
 ption%0A    %7B%0A
+        final long period = 1000;%0A
         fina
@@ -1318,20 +1318,22 @@
 agTimer(
-1000
+period
 , this);
@@ -1569,33 +1569,99 @@
             
-wait(1100
+// To be tolerant, wait up to 3 times that long%0A            wait(3 * period
 );%0A         
@@ -1752,32 +1752,33 @@
 me: %22 + t);%0A
+%0A
         
 assertEquals
@@ -1769,45 +1769,120 @@
     
-assertEquals(1.0, t.getSeconds(), 0.1
+if (Math.abs(t.getSeconds() - 1.0) %3E 0.1)%0A            System.out.println(%22WARNING: Expected nag after 1 seconds%22
 );%0A%0A
@@ -1934,35 +1934,40 @@
    Thread.sleep(
-500
+period/2
 );%0A        nag_t
@@ -1993,35 +1993,40 @@
    Thread.sleep(
-500
+period/2
 );%0A        nag_t
@@ -2064,11 +2064,16 @@
 eep(
-500
+period/2
 );%0A 
@@ -2317,36 +2317,42 @@
            wait(
-1100
+3 * period
 );%0A            a
@@ -2455,45 +2455,120 @@
     
-assertEquals(1.0, t.getSeconds(), 0.1
+if (Math.abs(t.getSeconds() - 1.0) %3E 0.1)%0A            System.out.println(%22WARNING: Expected nag after 1 seconds%22
 );%0A%0A
@@ -2665,20 +2665,26 @@
    wait(
-1100
+3 * period
 );%0A     
@@ -2795,45 +2795,120 @@
     
-assertEquals(1.0, t.getSeconds(), 0.1
+if (Math.abs(t.getSeconds() - 1.0) %3E 0.1)%0A            System.out.println(%22WARNING: Expected nag after 1 seconds%22
 );%0A%0A
@@ -2968,36 +2968,38 @@
    Thread.sleep(
-1100
+period
 );%0A        synch
@@ -3180,12 +3180,14 @@
 eep(
-1100
+period
 );%0A 

Make more tolerant