/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.clients.timer;


import org.objectweb.jonas.jtests.beans.transacted.Simple;

/**
 * This is test of the TimerService.
 * @author Philippe Durieux (jonas team)
 */
public abstract class A_TimerSession extends A_Timer {

    /**
     * constructor
     * @param name name of the test suite.
     */
    public A_TimerSession(String name) {
        super(name);
    }

    protected void setUp() {
        super.setUp();
    }

    /**
     * - cancel timers from test
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Test interval timer
     */
    public void testTimerS2() throws Exception {
        int duration = 4;
        int intervalduration = 3;
        Simple s = getSimple(10);
        int expected = s.getTimerCount();
        int id = s.setTimer(duration, intervalduration);
        try {
            assertEquals("timer expired too quickly", expected, s.getTimerCount());
            sleep(1000 * duration + 1500);
            expected++;
            assertEquals("timer did not expire first", expected, s.getTimerCount());
            // Change session. Timer must survive.
            s.remove();
            s = getSimple(103);
            for (int i = 0; i < 3; i++) {
                sleep(1000 * intervalduration);
                expected++;
                assertEquals("timer did not expired at " + i, expected, s.getTimerCount());
            }
        } finally {
            // Remove this line to test persistent timers.
            s.cancelTimer(id);      // necessary in case of stateless or MDB
            s.remove();
        }
    }

}
