<commit_before>package org.yamcs.alarms;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.yamcs.api.EventProducerFactory;
import org.yamcs.parameter.ParameterValue;
import org.yamcs.protobuf.Pvalue.MonitoringResult;
import org.yamcs.utils.TimeEncoding;
import org.yamcs.xtce.Parameter;

public class AlarmServerTest {
    Parameter p1 = new Parameter("p1");
    Parameter p2 = new Parameter("p2");
    AlarmServer<Parameter, ParameterValue> alarmServer;
    ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1);
    
    
    @BeforeClass
    static public void setupBeforeClass() {
        EventProducerFactory.setMockup(true);
        TimeEncoding.setUp();
    }

    ParameterValue getParameterValue(Parameter p, MonitoringResult mr) {
        ParameterValue pv = new ParameterValue(p);
        pv.setMonitoringResult(mr);

        return pv;
    }
    
    @Before
    public void before() {
        alarmServer = new AlarmServer<>("toto", timer);
    }

    @Test
    public void test1() {
        MyListener l = new MyListener();
        alarmServer.addAlarmListener(l);
        ParameterValue pv1_0 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_0, 1);

        ActiveAlarm<ParameterValue> aa = l.triggered.remove();
        assertEquals(pv1_0, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ParameterValue pv1_1 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_1, 1);
        assertTrue(l.triggered.isEmpty());
        aa = l.valueUpdates.remove();
        assertEquals(pv1_1, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ParameterValue pv1_2 = getParameterValue(p1, MonitoringResult.CRITICAL);
        alarmServer.update(pv1_2, 1);
        assertTrue(l.triggered.isEmpty());
        assertFalse(l.valueUpdates.isEmpty());
        aa = l.severityIncreased.remove();
        assertEquals(pv1_2, aa.currentValue);
        assertEquals(pv1_2, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        long ackTime = 123L;
        alarmServer.acknowledge(aa, "test1", ackTime, "bla");
        assertTrue(l.cleared.isEmpty());

        assertEquals(1, l.acknowledged.size());
        assertEquals(aa, l.acknowledged.remove());

        ParameterValue pv1_3 = getParameterValue(p1, MonitoringResult.IN_LIMITS);
        alarmServer.update(pv1_3, 1);
        aa = l.cleared.remove();
        assertEquals(pv1_3, aa.currentValue);
        assertEquals(pv1_2, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);
        assertEquals("test1", aa.usernameThatAcknowledged);
        assertEquals(ackTime, aa.acknowledgeTime);
        assertEquals("bla", aa.getAckMessage());
    }

    @Test
    public void test2() {
        MyListener l = new MyListener();
        alarmServer.addAlarmListener(l);
        ParameterValue pv1_0 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_0, 1);

        ActiveAlarm<ParameterValue> aa = l.triggered.remove();
        assertEquals(pv1_0, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ParameterValue pv1_1 = getParameterValue(p1, MonitoringResult.IN_LIMITS);
        alarmServer.update(pv1_1, 1);
        assertTrue(l.cleared.isEmpty());
        aa = l.valueUpdates.remove();
        assertEquals(pv1_1, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        assertEquals(1, l.rtn.size());
        
        long ackTime = 123L;
        alarmServer.acknowledge(aa, "test2", ackTime, "bla");


        aa = l.cleared.remove();
        assertEquals(pv1_1, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);
        assertEquals("test2", aa.usernameThatAcknowledged);
        assertEquals(ackTime, aa.acknowledgeTime);
        assertEquals("bla", aa.getAckMessage());
    }

    
    @Test
    public void testShelve() throws InterruptedException {
        MyListener l = new MyListener();
        alarmServer.addAlarmListener(l);
        ParameterValue pv1_0 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_0, 1);

        ActiveAlarm<ParameterValue> aa = l.triggered.remove();
        assertEquals(pv1_0, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        alarmServer.shelve(aa, "busy operator", "looking at it later", 500);
        assertEquals(1, l.shelved.size());
        assertEquals(aa, l.shelved.remove());
        
        ActiveAlarm<ParameterValue> aa1 = l.unshelved.poll(2000, TimeUnit.MILLISECONDS);

        assertEquals(aa, aa1);
        assertFalse(aa.isShelved());
    }

    @Test
    public void testAutoAck() {
        MyListener l = new MyListener();
        alarmServer.addAlarmListener(l);
        ParameterValue pv1_0 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_0, 1, true, false);

        ActiveAlarm<ParameterValue> aa = l.triggered.remove();
        assertEquals(pv1_0, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ParameterValue pv1_1 = getParameterValue(p1, MonitoringResult.IN_LIMITS);
        alarmServer.update(pv1_1, 1, true, false);

        aa = l.cleared.remove();
        assertEquals(pv1_1, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);<commit_msg>try to find the cause of spurious test failure<commit_after>package org.yamcs.alarms;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.yamcs.api.EventProducerFactory;
import org.yamcs.parameter.ParameterValue;
import org.yamcs.protobuf.Pvalue.MonitoringResult;
import org.yamcs.utils.TimeEncoding;
import org.yamcs.xtce.Parameter;

public class AlarmServerTest {
    Parameter p1 = new Parameter("p1");
    Parameter p2 = new Parameter("p2");
    AlarmServer<Parameter, ParameterValue> alarmServer;
    ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1);
    
    
    @BeforeClass
    static public void setupBeforeClass() {
        EventProducerFactory.setMockup(true);
        TimeEncoding.setUp();
    }

    ParameterValue getParameterValue(Parameter p, MonitoringResult mr) {
        ParameterValue pv = new ParameterValue(p);
        pv.setMonitoringResult(mr);

        return pv;
    }
    
    @Before
    public void before() {
        alarmServer = new AlarmServer<>("toto", timer);
    }

    @Test
    public void test1() {
        MyListener l = new MyListener();
        alarmServer.addAlarmListener(l);
        ParameterValue pv1_0 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_0, 1);

        ActiveAlarm<ParameterValue> aa = l.triggered.remove();
        assertEquals(pv1_0, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ParameterValue pv1_1 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_1, 1);
        assertTrue(l.triggered.isEmpty());
        aa = l.valueUpdates.remove();
        assertEquals(pv1_1, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ParameterValue pv1_2 = getParameterValue(p1, MonitoringResult.CRITICAL);
        alarmServer.update(pv1_2, 1);
        assertTrue(l.triggered.isEmpty());
        assertFalse(l.valueUpdates.isEmpty());
        aa = l.severityIncreased.remove();
        assertEquals(pv1_2, aa.currentValue);
        assertEquals(pv1_2, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        long ackTime = 123L;
        alarmServer.acknowledge(aa, "test1", ackTime, "bla");
        assertTrue(l.cleared.isEmpty());

        assertEquals(1, l.acknowledged.size());
        assertEquals(aa, l.acknowledged.remove());

        ParameterValue pv1_3 = getParameterValue(p1, MonitoringResult.IN_LIMITS);
        alarmServer.update(pv1_3, 1);
        aa = l.cleared.remove();
        assertEquals(pv1_3, aa.currentValue);
        assertEquals(pv1_2, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);
        assertEquals("test1", aa.usernameThatAcknowledged);
        assertEquals(ackTime, aa.acknowledgeTime);
        assertEquals("bla", aa.getAckMessage());
    }

    @Test
    public void test2() {
        MyListener l = new MyListener();
        alarmServer.addAlarmListener(l);
        ParameterValue pv1_0 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_0, 1);

        ActiveAlarm<ParameterValue> aa = l.triggered.remove();
        assertEquals(pv1_0, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ParameterValue pv1_1 = getParameterValue(p1, MonitoringResult.IN_LIMITS);
        alarmServer.update(pv1_1, 1);
        assertTrue(l.cleared.isEmpty());
        aa = l.valueUpdates.remove();
        assertEquals(pv1_1, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        assertEquals(1, l.rtn.size());
        
        long ackTime = 123L;
        alarmServer.acknowledge(aa, "test2", ackTime, "bla");


        aa = l.cleared.remove();
        assertEquals(pv1_1, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);
        assertEquals("test2", aa.usernameThatAcknowledged);
        assertEquals(ackTime, aa.acknowledgeTime);
        assertEquals("bla", aa.getAckMessage());
    }

    
    @Test
    public void testShelve() throws InterruptedException {
        MyListener l = new MyListener();
        alarmServer.addAlarmListener(l);
        ParameterValue pv1_0 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_0, 1);

        ActiveAlarm<ParameterValue> aa = l.triggered.remove();
        assertEquals(pv1_0, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ActiveAlarm<ParameterValue> aa1 = alarmServer.shelve(aa, "busy operator", "looking at it later", 500);
        assertNotNull(aa1);
        
        assertEquals(1, l.shelved.size());
        assertEquals(aa, l.shelved.remove());
        
        ActiveAlarm<ParameterValue> aa2 = l.unshelved.poll(2000, TimeUnit.MILLISECONDS);

        assertEquals(aa, aa2);
        assertFalse(aa.isShelved());
    }

    @Test
    public void testAutoAck() {
        MyListener l = new MyListener();
        alarmServer.addAlarmListener(l);
        ParameterValue pv1_0 = getParameterValue(p1, MonitoringResult.WARNING);
        alarmServer.update(pv1_0, 1, true, false);

        ActiveAlarm<ParameterValue> aa = l.triggered.remove();
        assertEquals(pv1_0, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
        assertEquals(pv1_0, aa.triggerValue);

        ParameterValue pv1_1 = getParameterValue(p1, MonitoringResult.IN_LIMITS);
        alarmServer.update(pv1_1, 1, true, false);

        aa = l.cleared.remove();
        assertEquals(pv1_1, aa.currentValue);
        assertEquals(pv1_0, aa.mostSevereValue);
{'fraction_non_alphanumeric': 0.11, 'fraction_numerical': 0.022903225806451613, 'mean_word_length': 3.2498286497601097, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '3138974', 'n_tokens_mistral': 3969, 'n_tokens_neox': 3749, 'n_words': 655}