/*
 * 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.stests.manac;

import javax.rmi.PortableRemoteObject;
import org.objectweb.jonas.stests.util.JTestCase;

/**
 * This test suite tests remove operations in a multithreading environment.
 */
public abstract class A_remove extends JTestCase {

    static final int initialValue = 1000;
    protected static ManagerHome home = null;
    protected static Manager manager = null;

    int accounts = 100;
    int loops = 1;
    int threads = 1;

    public static boolean threadfail;

    public A_remove(String name) {
        super(name);
    }

    public abstract String getManagerHomeName();

    protected void setUp() throws Exception {
        super.setUp();
        if (home == null) {
            useBeans("manac");
            home = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(getManagerHomeName()),
                                                             ManagerHome.class);
        }
        if (manager == null) {
            manager = home.create(initialValue);
        }
        threadfail = false;
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Run a multithreaded client test. Common part to all test cases.
     */
    public void remac(int accounts, int loops, int threads) throws Exception {

        // Create and start all threads
        A_remthread[] t_thr = new A_remthread[threads];
        for (int i = 0; i < threads; i++) {
            t_thr[i] = new A_remthread(getManagerHomeName(), i, ictx, accounts, loops);
            t_thr[i].start();
        }

        // Wait end of all threads
        for (int p = 0; p < threads; p++) {
            t_thr[p].join();
        }

        // Check if all threads run ok
        if (threadfail) {
            fail("Error in a thread");
        }
    }

    public void testBasic() throws Exception {
        remac(accounts, loops, threads);
    }

    public void testConc() throws Exception {
        int accounts = 10;
        int threads = 12;
        int loops = 4;
        remac(accounts, loops, threads);
    }

    public void testL100() throws Exception {
        int loops = 100;
        remac(accounts, loops, threads);
    }

    public void testL50T2() throws Exception {
        int loops = 50;
        int threads = 2;
        remac(accounts, loops, threads);
    }

    public void testL20T5() throws Exception {
        int loops = 20;
        int threads = 5;
        remac(accounts, loops, threads);
    }

    public void testL10T8() throws Exception {
        int accounts = 1000;
        int loops = 10;
        int threads = 8;
        remac(accounts, loops, threads);
    }

}
