<commit_before>/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2004
 *    Copyright by ESO (in the framework of the ALMA collaboration),
 *    All rights reserved
 *
 *    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 (at your option) 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
 */
package alma.acs.component.client;

import java.io.PrintWriter;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import com.cosylab.logging.engine.log.ILogEntry;
import com.cosylab.logging.engine.log.LogTypeHelper;

import alma.acs.logging.AcsLogLevel;
import alma.acs.logging.ClientLogManager;
import alma.acs.logging.config.LogConfig;
import alma.acs.logging.engine.LogReceiver;
import alma.acs.logging.engine.LogReceiver.DelayedLogEntry;
import alma.acs.logging.level.AcsLogLevelDefinition;
import alma.maci.loggingconfig.LoggingConfig;

/**
 * Tests the {@link alma.acs.logging.engine.LogReceiver} obtained 
 * from {@link alma.acs.component.client.ComponentClientTestCase#getLogReceiver()}.
 * 
 * @author hsommer
 * created 29.04.2006 10:27:51
 */
public class ClientWithLogReceiverTest extends ComponentClientTestCase {

    private LogReceiver logReceiver;

    public ClientWithLogReceiverTest() throws Exception {
        super("ClientWithLogReceiverTest");
    }
    
    protected void setUp() throws Exception {
    	// to not suppress any remote logging (would usually be configured through the CDB)  
    	System.setProperty("ACS.log.minlevel.remote", "2");
        super.setUp();
        logReceiver = getLogReceiver();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }
        
    /**
     * The log receiver is supposed to be initialized, 
     * since {@link LogReceiver#initialize(org.omg.CORBA.ORB, si.ijs.maci.Manager)} 
     */
    public void testInitialized() {
        assertTrue(logReceiver.isInitialized());
    }
    
    /**
     * Logs a single record and waits for it to come back from the Log service 
     * and to pass the queue (which is set to zero delay).
     * Then the record is verified, and the same test is repeated a couple of times with different log records. 
     * @throws Exception
     */
    public void testLogQueueNoDelay() throws Exception {
    	LogConfig logConfig = ClientLogManager.getAcsLogManager().getLogConfig();
    	assertEquals("For this test, even low-level logs must be sent off remotely.", AcsLogLevelDefinition.TRACE, logConfig.getDefaultMinLogLevel());
        logReceiver.setDelayMillis(0);
        BlockingQueue<DelayedLogEntry> queue = logReceiver.getLogQueue();
        
        Level[] levels = new Level[] {Level.FINEST, Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE};
        Random random = new Random(System.currentTimeMillis());
        for (int i=0; i<levels.length; i++) {
            Level level = levels[random.nextInt(levels.length)];
            String acsLevelName = AcsLogLevel.getNativeLevel(level).getEntryName();
            // it's pretty odd that jlog uses its own set of log type integers 
            LogTypeHelper logType = LogTypeHelper.fromLogTypeDescription(acsLevelName);
            
            String logMessage = "This is log number " + i;
            m_logger.log(level, logMessage);
            
            // in spite of zero queue sorting delay, we need a long timeout 
            // to compensate travel delay when sending log records to Log service,
            // and then getting them back over the network.
            long timeoutSec = 10L + logConfig.getFlushPeriodSeconds();
            while (true) {
	            DelayedLogEntry delayedLogEntry = queue.poll(timeoutSec, TimeUnit.SECONDS);            
	            if (delayedLogEntry != null) {
	            	if (delayedLogEntry.isQueuePoison()) {
	            		fail("Unexpected end of log queue.");
	            	}
	                ILogEntry logEntry = delayedLogEntry.getLogEntry();
	                String sourceObjectName = (String) logEntry.getField(ILogEntry.Field.SOURCEOBJECT);
	                if (sourceObjectName!=null && sourceObjectName.equals("ClientWithLogReceiverTest#testLogQueueNoDelay")) {
		                assertEquals(logMessage, logEntry.getField(ILogEntry.Field.LOGMESSAGE));
		                assertEquals(logType, ((LogTypeHelper)logEntry.getField(ILogEntry.Field.ENTRYTYPE)));
		                System.out.println("Received back log record #" + i);
		                break; // and continue outer loop with next log record
	                }
	                else {
	                	// was some other stray log, perhaps from a previously running ACS component
	                	System.out.println("Ignoring log from SourceObject=" + sourceObjectName);
	                }
	            }
	            else {
	            	// todo: change this timeout to be not circumvented by stray log messages from other clients.
	                fail("Did not receive the expected log record #" + i + " within " + timeoutSec + " seconds.");
	            }
            }
        }
        logReceiver.stop();
    }
    
    public void testLogCapture() throws Exception {<commit_msg>Fixed testLogQueueNoDelay() to not hang forever if the expected log message is not received, but always some other message comes in during the waiting timeout interval (which then started again). Also changed log levels to the new enums used in ACS and special version used inside jlog.<commit_after>/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2004
 *    Copyright by ESO (in the framework of the ALMA collaboration),
 *    All rights reserved
 *
 *    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 (at your option) 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
 */
package alma.acs.component.client;

import java.io.PrintWriter;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import alma.acs.logging.AcsLogLevel;
import alma.acs.logging.ClientLogManager;
import alma.acs.logging.config.LogConfig;
import alma.acs.logging.engine.LogReceiver;
import alma.acs.logging.engine.LogReceiver.DelayedLogEntry;
import alma.acs.logging.level.AcsLogLevelDefinition;
import alma.acs.util.IsoDateFormat;

/**
 * Tests the {@link alma.acs.logging.engine.LogReceiver} obtained 
 * from {@link alma.acs.component.client.ComponentClientTestCase#getLogReceiver()}.
 * 
 * @author hsommer
 * created 29.04.2006 10:27:51
 */
public class ClientWithLogReceiverTest extends ComponentClientTestCase {

    private LogReceiver logReceiver;

    public ClientWithLogReceiverTest() throws Exception {
        super("ClientWithLogReceiverTest");
    }
    
    protected void setUp() throws Exception {
    	// to not suppress any remote logging (would usually be configured through the CDB)  
    	System.setProperty(LogConfig.PROPERTYNAME_MIN_LOG_LEVEL, "2");
        super.setUp();
        m_logger.info("------------ setUp " + getName() + " --------------");
        logReceiver = getLogReceiver();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }
        
    /**
     * The log receiver is supposed to be initialized, 
     * since {@link LogReceiver#initialize(org.omg.CORBA.ORB, si.ijs.maci.Manager)} 
     */
    public void testInitialized() {
        assertTrue(logReceiver.isInitialized());
    }
    
    /**
     * Logs a single record and waits for it to come back from the Log service 
     * and to pass the queue (which is set to zero delay).
     * Then the record is verified, and the same test is repeated a couple of times with different log records. 
     * @throws Exception
     */
    public void testLogQueueNoDelay() throws Exception {
    	LogConfig logConfig = ClientLogManager.getAcsLogManager().getLogConfig();
    	assertEquals("For this test, even low-level logs must be sent off remotely.", 
    			AcsLogLevelDefinition.TRACE, logConfig.getDefaultMinLogLevel());
        logReceiver.setDelayMillis(0);
        BlockingQueue<DelayedLogEntry> queue = logReceiver.getLogQueue();
        
        // the set of log levels to be randomly selected for the test logs 
        AcsLogLevelDefinition[] coreLevels = new AcsLogLevelDefinition[] {
        		AcsLogLevelDefinition.TRACE,
        		AcsLogLevelDefinition.DEBUG,
        		AcsLogLevelDefinition.INFO,
        		AcsLogLevelDefinition.WARNING, 
        		AcsLogLevelDefinition.EMERGENCY };
        
        int numberOfTestLogs = 10;
        Random random = new Random(System.currentTimeMillis());        
        
        // loop for sending several test logs
        for (int i=0; i < numberOfTestLogs; i++) {
        	AcsLogLevelDefinition coreLevel = coreLevels[random.nextInt(coreLevels.length)];
        	AcsLogLevel acsJdkLevel = AcsLogLevel.fromAcsCoreLevel(coreLevel);
        	
        	// Log the test record
            String logMessage = "This is log number " + i;
            m_logger.log(acsJdkLevel, logMessage);
            
            // Wait for the test record to come back from the Log service.
            // In spite of zero queue sorting delay, we need a long timeout 
            // to compensate for travel delays to and from the Log service.
            int delayLogServiceSec = 20; // should be much less, but currently we have problems there.
            int timeoutSec = logConfig.getFlushPeriodSeconds() + delayLogServiceSec;
            long timeoutSysMillis = System.currentTimeMillis() + timeoutSec*1000;
            while (true) { // System.currentTimeMillis() < timeoutSysMillis
            	// wait on the queue for a log record to arrive
	            DelayedLogEntry delayedLogEntry = queue.poll(timeoutSysMillis - System.currentTimeMillis(), TimeUnit.MILLISECONDS);            
	            if (delayedLogEntry != null) {
	            	// got something, must check if it was the record we sent
	            	if (delayedLogEntry.isQueuePoison()) {
	            		fail("Unexpected end of log queue.");
	            	}
	                LogReceiver.ReceivedLogRecord logRecord = delayedLogEntry.getLogRecord();
	                String sourceObjectName = logRecord.getSourceObject();
	                if (sourceObjectName!=null && sourceObjectName.equals("ClientWithLogReceiverTest#testLogQueueNoDelay")) {
	                	// it's a log record sent from this process 
		                assertEquals("Log message text must match the test log record", 
		                		logMessage, logRecord.getMessage());
		                assertEquals("Log level must match the test log record", 
		                		coreLevel, logRecord.getLevel().getAcsCoreLevel());
		                System.out.println("Received back log record #" + i);
		                break; // and continue outer loop with next log record
	                }
	                else {
	                	// was some other stray log, perhaps from a previously running ACS component, or the pinging jmanager
	                	// we stay in the while loop and wait again for another record to be delivered by the queue
	                	System.out.println(IsoDateFormat.formatCurrentDate() + ": Ignoring received log " + IsoDateFormat.formatDate(logRecord.getTimestamp()) + 
	                			" [" + sourceObjectName + "]");
	                }
	            }
	            else {
	                fail("Did not receive the expected log record #" + i + " within " + timeoutSec + " seconds.");
	            }
            }
        }
        logReceiver.stop();
    }
    
    
    public void testLogCapture() throws Exception {
{'fraction_non_alphanumeric': 0.08277559812293253, 'fraction_numerical': 0.006462035541195477, 'mean_word_length': 2.549003549003549, 'pattern_counts': {'":': 1, '<': 8, '<?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': '3877095', 'n_tokens_mistral': 3499, 'n_tokens_neox': 3320, 'n_words': 1117}