/*********************************************************************
* MassiveRemoteTest.java
* created on 15.08.2004 by netseeker
* $Source: /cvsroot/ejoe/EJOE/cltest/de/netseeker/ejoe/cltest/MassiveRemoteTest.java,v $
* $Date: 2007/11/17 10:59:55 $
* $Revision: 1.2 $
*********************************************************************/
package de.netseeker.ejoe.cltest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.TestCase;
import de.netseeker.ejoe.EJClient;
import de.netseeker.ejoe.EJConstants;
import de.netseeker.ejoe.adapter.SerializeAdapter;
import de.netseeker.ejoe.adapter.XStreamAdapter;
/**
* @author netseeker aka Michael Manske
*/
public class MassiveRemoteTest extends TestCase
{
private Logger log = Logger.getLogger( this.getClass().getName() );
EJClient client;
int threads = 10, readers = 5, writers = 3;
AtomicInteger successCount = new AtomicInteger( 0 );
AtomicInteger activeCount;
SerializeAdapter adapter = new XStreamAdapter( true );
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{
super.setUp();
activeCount = new AtomicInteger( threads );
}
public void testExecute()
{
long start = System.currentTimeMillis();
log.log( Level.INFO, "Starting " + threads + " client threads..." );
for ( int i = 1; i <= threads; i++ )
{
new ClientThread( adapter, i ).start();
}
while ( activeCount.get() > 0 )
{
log.log( Level.INFO, "active threads: " + activeCount );
try
{
Thread.sleep( 50 );
}
catch ( InterruptedException e )
{
// do nothing
}
}
log.log( Level.INFO, successCount + " clients returned successfully..." );
System.out.println( "Test took " + (System.currentTimeMillis() - start) + " ms." );
assertEquals( threads, successCount.get() );
}
class ClientThread extends Thread
{
private int number;
private SerializeAdapter adapter;
public ClientThread(SerializeAdapter adapter, int number)
{
super( "EJOE Clientthread: " + number );
this.number = number;
this.adapter = adapter;
}
public void run()
{
log.log( Level.FINEST, System.currentTimeMillis() + " -- ClientThread" + number + " started" );
EJClient ejclient = new EJClient( "127.0.0.1", EJConstants.EJOE_PORT, adapter );
ejclient.enablePersistentConnection( true );
ejclient.setConnectionTimeout( 60000 );
Map result = null;
Map input = new HashMap();
input.put( "KEY1", "Hello" );
input.put( "KEY2", Integer.valueOf( 101 ) );
input.put( "KEY3", BigDecimal.valueOf( 102 ) );
try
{
for ( int i = 0; i < 5; i++ )
{
result = (Map) ejclient.execute( input );
if ( result == null ) fail( "Result was null!" );
if ( result.get( "KEY4" ) == null )
fail( "Additional key which should be set by EJServer was not found!" );
log.log( Level.FINEST, System.currentTimeMillis() + " -- ClientThread" + number
+ " return map contains " + result.size() + " entries." );
}
}
catch ( Exception e )
{
log.log( Level.SEVERE, "Test failed!", e );
}
finally
{
if ( result != null )
{
successCount.getAndIncrement();
}
activeCount.getAndDecrement();
ejclient.close();
}
log.log( Level.FINEST, System.currentTimeMillis() + " -- ClientThread" + number + " finished" );
}
}
}