<commit_before>package org.apache.jcs.auxiliary.lateral.socket.tcp;

import junit.framework.TestCase;

import org.apache.jcs.JCS;
import org.apache.jcs.auxiliary.AuxiliaryCache;
import org.apache.jcs.auxiliary.lateral.LateralCacheAttributes;
import org.apache.jcs.auxiliary.lateral.LateralCacheFactory;
import org.apache.jcs.auxiliary.lateral.LateralElementDescriptor;
import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes;
import org.apache.jcs.engine.CacheElement;
import org.apache.jcs.engine.behavior.ICacheElement;
import org.apache.jcs.engine.behavior.ICompositeCacheManager;
import org.apache.jcs.engine.control.CompositeCacheManager;
import org.apache.jcs.engine.control.CompositeCacheManagerMockImpl;

/**
 * Basic unit tests for the sending and receiving portions of the lateral cache.
 * 
 * @author <NAME>
 *  
 */
public class TestTCPLateral
    extends TestCase
{

    /**
     * Test setup
     */
    public void setUp()
    {
        JCS.setConfigFilename( "/TestTCPLateralCache.ccf" );
    }

    /**
     * Make sure we can send a bunch to the listener. This would be better if we
     * could plugin a Mock CacheManger. The listener will instantiate on on its
     * own. We have to configure one before that.
     * 
     * @throws Exception
     */
    public void testSimpleSend()
        throws Exception
    {

        JCS jcs = JCS.getInstance( "test" );

        LateralCacheAttributes lac = new LateralCacheAttributes();
        lac.setTransmissionType( LateralCacheAttributes.TCP );
        lac.setTcpServer( "localhost" + ":" + 8111 );
        lac.setTcpListenerPort( 8111 );

        ICompositeCacheManager cacheMgr = CompositeCacheManager.getInstance();

        // start the listener
        LateralTCPListener listener = (LateralTCPListener) LateralTCPListener.getInstance( lac, cacheMgr );

        // send to the listener
        LateralTCPSender lur = new LateralTCPSender( lac );

        int numMes = 10;
        for ( int i = 0; i < numMes; i++ )
        {
            String message = "adsfasasfasfasdasf";
            CacheElement ce = new CacheElement( "test", "test", message );
            LateralElementDescriptor led = new LateralElementDescriptor( ce );
            led.command = LateralElementDescriptor.UPDATE;
            led.requesterId = 1;
            lur.send( led );
        }

        Thread.sleep( numMes );

        System.out.println( "PutCount = " + listener.getPutCnt() );
        assertEquals( "Should have received " + numMes + " by now.", numMes, listener.getPutCnt() );

    }

    public void testReceive()
        throws Exception
    {
        ILateralCacheAttributes lattr = new LateralCacheAttributes();
        lattr.setTcpListenerPort( 1101 );
        lattr.setTransmissionTypeName( "TCP" );
        CompositeCacheManagerMockImpl cacheMgr = new CompositeCacheManagerMockImpl();
        System.out.println( "mock cache = " + cacheMgr.getCache( "test" ) );

        LateralCacheFactory fact = new LateralCacheFactory();
        //.getInstance( lattr, cacheMgr );
        //LateralCacheNoWait nwait1 = (LateralCacheNoWait)lcMgr1.getCache(
        // "test" );
        //AuxiliaryCache nowait1 = fact.createCache( lattr, cacheMgr );

        //nowait1.update( );

        ILateralCacheAttributes lattr2 = new LateralCacheAttributes();
        lattr2.setTcpListenerPort( 1102 );
        lattr2.setTransmissionTypeName( "TCP" );
        lattr2.setTcpServer( "localhost:1101" );

        LateralTCPService service = new LateralTCPService( lattr2 );
        service.setListenerId( 123456 );

        int cnt = 100;
        for ( int i = 0; i < cnt; i++ )
        {
            ICacheElement element = new CacheElement( "test", "key" + i, "value1" );
            service.update( element );
        }

        Thread.sleep( 1000 );

        System.out.println( "cache. getPutCount = " + cacheMgr.getCache().getUpdateCount() );

        assertEquals( "Didn't get the correct number", cnt, cacheMgr.getCache().getUpdateCount() );<commit_msg>Refactoring lateral manager and factory.  The old model was to have one factory and mananger know about all types of lateral.  This created too many dependencies  and forced lots of ad hoc logic into the managers.  The new model will have each lateral responsible for its own factory and manager.  This keeps things clean and will allow laterals to ahve their own configuration settings.  The old attributes object had stuff for every lateral, but only a small subset applied to any given lateral.  This is a work in progress but it is almost done.  <commit_after>package org.apache.jcs.auxiliary.lateral.socket.tcp;

import junit.framework.TestCase;

import org.apache.jcs.JCS;
import org.apache.jcs.auxiliary.lateral.LateralCacheAttributes;
import org.apache.jcs.auxiliary.lateral.LateralElementDescriptor;
import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes;
import org.apache.jcs.engine.CacheElement;
import org.apache.jcs.engine.behavior.ICacheElement;
import org.apache.jcs.engine.behavior.ICompositeCacheManager;
import org.apache.jcs.engine.control.CompositeCacheManager;
import org.apache.jcs.engine.control.CompositeCacheManagerMockImpl;

/**
 * Basic unit tests for the sending and receiving portions of the lateral cache.
 * 
 * @author <NAME>
 *  
 */
public class TestTCPLateral
    extends TestCase
{

    /**
     * Test setup
     */
    public void setUp()
    {
        JCS.setConfigFilename( "/TestTCPLateralCache.ccf" );
    }

    /**
     * Make sure we can send a bunch to the listener. This would be better if we
     * could plugin a Mock CacheManger. The listener will instantiate on on its
     * own. We have to configure one before that.
     * 
     * @throws Exception
     */
    public void testSimpleSend()
        throws Exception
    {

        JCS jcs = JCS.getInstance( "test" );

        LateralCacheAttributes lac = new LateralCacheAttributes();
        lac.setTransmissionType( LateralCacheAttributes.TCP );
        lac.setTcpServer( "localhost" + ":" + 8111 );
        lac.setTcpListenerPort( 8111 );

        ICompositeCacheManager cacheMgr = CompositeCacheManager.getInstance();

        // start the listener
        LateralTCPListener listener = (LateralTCPListener) LateralTCPListener.getInstance( lac, cacheMgr );

        // send to the listener
        LateralTCPSender lur = new LateralTCPSender( lac );

        int numMes = 10;
        for ( int i = 0; i < numMes; i++ )
        {
            String message = "adsfasasfasfasdasf";
            CacheElement ce = new CacheElement( "test", "test", message );
            LateralElementDescriptor led = new LateralElementDescriptor( ce );
            led.command = LateralElementDescriptor.UPDATE;
            led.requesterId = 1;
            lur.send( led );
        }

        Thread.sleep( numMes );

        System.out.println( "PutCount = " + listener.getPutCnt() );
        assertEquals( "Should have received " + numMes + " by now.", numMes, listener.getPutCnt() );

    }

    public void testReceive()
        throws Exception
    {
        ILateralCacheAttributes lattr = new LateralCacheAttributes();
        lattr.setTcpListenerPort( 1101 );
        lattr.setTransmissionTypeName( "TCP" );
        CompositeCacheManagerMockImpl cacheMgr = new CompositeCacheManagerMockImpl();
        System.out.println( "mock cache = " + cacheMgr.getCache( "test" ) );

        LateralTCPCacheFactory fact = new LateralTCPCacheFactory();
        //.getInstance( lattr, cacheMgr );
        //LateralCacheNoWait nwait1 = (LateralCacheNoWait)lcMgr1.getCache(
        // "test" );
        //AuxiliaryCache nowait1 = fact.createCache( lattr, cacheMgr );

        //nowait1.update( );

        // start the listener
        LateralTCPListener listener = (LateralTCPListener) LateralTCPListener.getInstance( lattr, cacheMgr );

        
        ILateralCacheAttributes lattr2 = new LateralCacheAttributes();
        lattr2.setTcpListenerPort( 1102 );
        lattr2.setTransmissionTypeName( "TCP" );
        lattr2.setTcpServer( "localhost:1101" );

        LateralTCPService service = new LateralTCPService( lattr2 );
        service.setListenerId( 123456 );

        int cnt = 100;
        for ( int i = 0; i < cnt; i++ )
        {
            ICacheElement element = new CacheElement( "test", "key" + i, "value1" );
            service.update( element );
        }

        Thread.sleep( 1000 );

        System.out.println( "cache. getPutCount = " + cacheMgr.getCache().getUpdateCount() );

        assertEquals( "Didn't get the correct number", cnt, cacheMgr.getCache().getUpdateCount() );
{'fraction_non_alphanumeric': 0.09726408532344076, 'fraction_numerical': 0.011129144447020635, 'mean_word_length': 2.8138815207780725, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3230014', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2451, 'n_words': 633}