<commit_before>/*
 * ALMA - Atacama Large Millimiter Array (c) European Southern Observatory, 2006
 * 
 * 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.lasercore.test;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Random;
import java.util.Vector;

import cern.laser.source.alarmsysteminterface.FaultState;

import alma.acs.component.client.ComponentClientTestCase;
import alma.acs.lasercore.test.stress.CategoryClient;
import alma.acs.lasercore.test.stress.category.AlarmView;
import alma.acs.lasercore.test.stress.category.CategoryListener;
import alma.alarmsystem.Alarm;
import alma.alarmsystem.source.ACSAlarmSystemInterface;
import alma.alarmsystem.source.ACSAlarmSystemInterfaceFactory;
import alma.alarmsystem.source.ACSFaultState;

/**
 * This test sends a lot of fault states and check if they are published in the
 * categories as expected.
 * To avoid defining a great number of alarms in the CDB, a default alarm
 * has been set in the TEST family
 * 
 * Sources are not tested here because they have already been tested in laser-source
 * 
 * NOTE: the test is performed without using the CategoryClient that at this level
 *       is not available (it requires modules compiled later in the build
 *       sequence)
 *       
 *    
 * @author acaproni
 *
 */
public class StressTest extends ComponentClientTestCase implements CategoryListener {
	
	/**
	 * The relevant fields of a fault state to publish
	 * 
	 * @author acaproni
	 *
	 */
	private class MiniFaultState {
		// The fields of the fault state
		public final String FF, FM;
		public final int FC;
		public final long msec;
		public final String description; // ACTIVE/Terminate
		public final Timestamp timestamp;
		
		public MiniFaultState() {
			FF=StressTest.FF;
			//FM=StressTest.FM+Math.abs(rnd.nextInt());
			FM=StressTest.FM+(count++);
			FC = Math.abs(rnd.nextInt() % 2) + 1;
			msec=System.currentTimeMillis();
			timestamp=new Timestamp(msec);
			if (rnd.nextInt()%2==0) {
				description=FaultState.ACTIVE;
				StressTest.this.activeFS++;
			} else {
				description=FaultState.TERMINATE;
			}
			assertNotNull(FF);
			assertNotNull(FM);
			assertNotNull(description);
			assertNotNull(timestamp);
		}
	}
	
	// FF and FM of the published states
	private static final String FF = "TEST";
	private static final String FM = "Member";
	
	// The number of alarms to publish
	private static final int NUM_ALARMS_TO_SEND = 1000;
	
	// The random number generator to create FMs
	private static Random rnd = new Random(System.currentTimeMillis());
	
	// The source
	private ACSAlarmSystemInterface alarmSource;

	// The category client
	private CategoryClient categoryClient;
	
	// The fault states to publish
	private MiniFaultState[] statesToPublish;
	
	// The number of active FS published
	private int activeFS=0;
	
	private static int count=0;
	
	/**
	 * Constructor
	 * @throws Exception
	 */
	public StressTest() throws Exception {
		super("StressTest");
	}
	
	// The vector with the alarms received from the categories
	// The kei is the alarm ID
	private HashMap<String,AlarmView> alarms = new HashMap<String,AlarmView>(); 

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		assertNotNull(getContainerServices());
		
		ACSAlarmSystemInterfaceFactory.init(getContainerServices());
		
		alarmSource = ACSAlarmSystemInterfaceFactory.createSource();
		assertNotNull("Error instantiating the source",alarmSource);
		
		// Connect the categories
		categoryClient= new CategoryClient(getContainerServices());
		assertNotNull(categoryClient);
		categoryClient.addAlarmListener(this);
		categoryClient.connect();
		
		// generate the alarms
		statesToPublish=new MiniFaultState[NUM_ALARMS_TO_SEND];
		for (int t=0; t<statesToPublish.length; t++) {
			MiniFaultState mfs = new MiniFaultState();
			assertNotNull(mfs);
			statesToPublish[t]=mfs;
		}
	}

	@Override
	protected void tearDown() throws Exception {
		cleanActiveAlarms();
		categoryClient.disconnect();<commit_msg>Cleanup<commit_after>/*
 * ALMA - Atacama Large Millimiter Array (c) European Southern Observatory, 2006
 * 
 * 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.lasercore.test;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Random;

import cern.laser.source.alarmsysteminterface.FaultState;

import alma.acs.component.client.ComponentClientTestCase;
import alma.acs.container.ContainerServices;
import alma.acs.lasercore.test.stress.CategoryClient;
import alma.acs.lasercore.test.stress.category.AlarmView;
import alma.acs.lasercore.test.stress.category.CategoryListener;
import alma.alarmsystem.source.ACSAlarmSystemInterface;
import alma.alarmsystem.source.ACSAlarmSystemInterfaceFactory;
import alma.alarmsystem.source.ACSFaultState;

/**
 * This test sends a lot of fault states and check if they are published in the
 * categories as expected.
 * To avoid defining a great number of alarms in the CDB, a default alarm
 * has been set in the TEST family
 * 
 * Sources are not tested here because they have already been tested in laser-source
 * 
 * NOTE: the test is performed without using the CategoryClient that at this level
 *       is not available (it requires modules compiled later in the build
 *       sequence)
 *       
 *    
 * @author acaproni
 *
 */
public class StressTest extends ComponentClientTestCase implements CategoryListener {
	
	/**
	 * The relevant fields of a fault state to publish
	 * 
	 * @author acaproni
	 *
	 */
	private class MiniFaultState {
		// The fields of the fault state
		public final String FF, FM;
		public final int FC;
		public final long msec;
		public final String description; // ACTIVE/Terminate
		public final Timestamp timestamp;
		
		public MiniFaultState() {
			FF=StressTest.FF;
			//FM=StressTest.FM+Math.abs(rnd.nextInt());
			FM=StressTest.FM+(count++);
			FC = Math.abs(rnd.nextInt() % 2) + 1;
			msec=System.currentTimeMillis();
			timestamp=new Timestamp(msec);
			if (rnd.nextInt()%2==0) {
				description=FaultState.ACTIVE;
				StressTest.this.activeFS++;
			} else {
				description=FaultState.TERMINATE;
			}
			assertNotNull(FF);
			assertNotNull(FM);
			assertNotNull(description);
			assertNotNull(timestamp);
		}
	}
	
	/**
	 * FF of the published states
	 */
	private static final String FF = "TEST";
	
	/**
	 * FM of the published states
	 */
	private static final String FM = "Member";
	
	/**
	 *  The number of alarms to publish
	 */
	private static final int NUM_ALARMS_TO_SEND = 1000;
	
	/**
	 * The random number generator to create FMs
	 */
	private static Random rnd = new Random(System.currentTimeMillis());
	
	/**
	 *  The source
	 */
	private ACSAlarmSystemInterface alarmSource;
	
	/**
	 *  The category client
	 */
	private CategoryClient categoryClient;
	
	/**
	 * The fault states to publish
	 */
	private MiniFaultState[] statesToPublish;
	
	/**
	 *  Container services
	 */
	private ContainerServices contSvcs;
	
	/**
	 * The number of active FS published
	 */
	private int activeFS=0;
	
	private static int count=0;
	
	/**
	 * The vector with the alarms received from the categories.
	 * The key is the alarm ID
	 */
	private HashMap<String,AlarmView> alarms = new HashMap<String,AlarmView>();
	
	/**
	 * Constructor
	 * @throws Exception
	 */
	public StressTest() throws Exception {
		super("StressTest");
	}
	
	 

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		
		contSvcs = getContainerServices();
		assertNotNull(contSvcs);
		
		ACSAlarmSystemInterfaceFactory.init(contSvcs);
		
		alarmSource = ACSAlarmSystemInterfaceFactory.createSource();
		assertNotNull("Error instantiating the source",alarmSource);
		
		// Connect the categories
		categoryClient= new CategoryClient(contSvcs);
		assertNotNull(categoryClient);
		categoryClient.addAlarmListener(this);
		categoryClient.connect();
		
		// generate the alarms
		statesToPublish=new MiniFaultState[NUM_ALARMS_TO_SEND];
		for (int t=0; t<statesToPublish.length; t++) {
			MiniFaultState mfs = new MiniFaultState();
			assertNotNull(mfs);
			statesToPublish[t]=mfs;
		}
	}

	@Override
	protected void tearDown() throws Exception {
		cleanActiveAlarms();
		categoryClient.disconnect();
{'fraction_non_alphanumeric': 0.08757679891700511, 'fraction_numerical': 0.00645631573466625, 'mean_word_length': 3.9813278008298756, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '4827564', 'n_tokens_mistral': 3059, 'n_tokens_neox': 2711, 'n_words': 923}