예제 #1
0
        private void CheckResults(PatternTestStyle testStyle, String eventId)
        {
            // For each test descriptor, make sure the listener has received exactly the events expected
            int index = 0;

            Log.Debug("CheckResults: Checking results for event " + eventId);

            foreach (EventExpressionCase descriptor in _caseList.Results)
            {
                String expressionText = _expressions[index].Text;

                LinkedHashMap <String, LinkedList <EventDescriptor> > allExpectedResults = descriptor.ExpectedResults;
                EventBean[] receivedResults = _listeners[index].LastNewData;
                index++;

                // If nothing at all was expected for this event, make sure nothing was received
                if (!(allExpectedResults.ContainsKey(eventId)))
                {
                    if ((receivedResults != null) && (receivedResults.Length > 0))
                    {
                        Log.Debug("CheckResults: Incorrect result for style " + testStyle + " expression : " + expressionText);
                        Log.Debug("CheckResults: Expected no results for event " + eventId + ", but received " + receivedResults.Length + " events");
                        Log.Debug("CheckResults: Received, have " + receivedResults.Length + " entries");
                        PrintList(receivedResults);
                        Assert.Fail();
                    }
                    continue;
                }

                LinkedList <EventDescriptor> expectedResults = allExpectedResults.Get(eventId);

                // Compare the result lists, not caring about the order of the elements
                try {
                    if (!(CompareLists(receivedResults, expectedResults)))
                    {
                        Log.Debug("CheckResults: Incorrect result for style " + testStyle + " expression : " + expressionText);
                        Log.Debug("CheckResults: Expected size=" + expectedResults.Count + " received size=" + (receivedResults == null ? 0 : receivedResults.Length));

                        Log.Debug("CheckResults: Expected, have " + expectedResults.Count + " entries");
                        PrintList(expectedResults);
                        Log.Debug("CheckResults: Received, have " + (receivedResults == null ? 0 : receivedResults.Length) + " entries");
                        PrintList(receivedResults);

                        Assert.Fail();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                    Assert.Fail("For statement '" + expressionText + "' failed to assert: " + ex.Message);
                }
            }
        }
예제 #2
0
        private void RunTest(PatternTestStyle testStyle)
        {
            Configuration config = SupportConfigFactory.GetConfiguration();

            config.AddEventType("A", typeof(SupportBean_A));
            config.AddEventType("B", typeof(SupportBean_B));
            config.AddEventType("C", typeof(SupportBean_C));
            config.AddEventType("D", typeof(SupportBean_D));
            config.AddEventType("E", typeof(SupportBean_E));
            config.AddEventType("F", typeof(SupportBean_F));
            config.AddEventType("G", typeof(SupportBean_G));
            EPServiceProvider serviceProvider = EPServiceProviderManager.GetDefaultProvider(config);

            serviceProvider.Initialize();
            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.StartTest(serviceProvider, _testClass, _testMethodName);
            }

            EPRuntime runtime = serviceProvider.EPRuntime;

            // Send the start time to the runtime
            if (_sendEventCollection.GetTime(EventCollection.ON_START_EVENT_ID) != null)
            {
                TimerEvent startTime = new CurrentTimeEvent(_sendEventCollection.GetTime(EventCollection.ON_START_EVENT_ID).Value);
                runtime.SendEvent(startTime);
                Log.Debug("RunTest: Start time is " + startTime);
            }

            // Set up expression filters and match listeners

            int index = 0;

            foreach (EventExpressionCase descriptor in _caseList.Results)
            {
                String expressionText        = descriptor.ExpressionText;
                EPStatementObjectModel model = descriptor.ObjectModel;

                EPStatement statement = null;

                try
                {
                    if (model != null)
                    {
                        statement = serviceProvider.EPAdministrator.Create(model, "name--" + expressionText);
                    }
                    else
                    {
                        if (testStyle == PatternTestStyle.USE_PATTERN_LANGUAGE)
                        {
                            statement = serviceProvider.EPAdministrator.CreatePattern(expressionText, "name--" + expressionText);
                        }
                        else if (testStyle == PatternTestStyle.USE_EPL)
                        {
                            String text = "@Audit('pattern') @Audit('pattern-instances') select * from pattern [" + expressionText + "]";
                            statement      = serviceProvider.EPAdministrator.CreateEPL(text);
                            expressionText = text;
                        }
                        else if (testStyle == PatternTestStyle.USE_EPL_AND_CONSUME_NOCHECK)
                        {
                            String text = "select * from pattern @DiscardPartialsOnMatch @SuppressOverlappingMatches [" + expressionText + "]";
                            statement      = serviceProvider.EPAdministrator.CreateEPL(text);
                            expressionText = text;
                        }
                        else if (testStyle == PatternTestStyle.COMPILE_TO_MODEL)
                        {
                            String text = "select * from pattern [" + expressionText + "]";
                            EPStatementObjectModel mymodel = serviceProvider.EPAdministrator.CompileEPL(text);
                            statement      = serviceProvider.EPAdministrator.Create(mymodel);
                            expressionText = text;
                        }
                        else if (testStyle == PatternTestStyle.COMPILE_TO_EPL)
                        {
                            String text = "select * from pattern [" + expressionText + "]";
                            EPStatementObjectModel mymodel = serviceProvider.EPAdministrator.CompileEPL(text);
                            String reverse = mymodel.ToEPL();
                            statement      = serviceProvider.EPAdministrator.CreateEPL(reverse);
                            expressionText = reverse;
                        }
                        else
                        {
                            throw new ArgumentException("Unknown test style");
                        }
                    }
                }
                catch (Exception ex)
                {
                    String text = expressionText;
                    if (model != null)
                    {
                        text = "Model: " + model.ToEPL();
                    }
                    Log.Error(".runTest Failed to create statement for style " + testStyle + " pattern expression=" + text, ex);
                    Assert.Fail(".runTest Failed to create statement for style " + testStyle + " pattern expression=" + text);
                }

                // We stop the statement again and start after the first listener was added.
                // Thus we can handle patterns that fireStatementStopped on startup.
                statement.Stop();

                _expressions[index]         = statement;
                _expressions[index].Events += _listeners[index].Update;

                // Start the statement again: listeners now got called for on-start events such as for a "not"
                statement.Start();

                index++;
            }

            // Some expressions may fireStatementStopped as soon as they are started, such as a "not b()" expression, for example.
            // Check results for any such listeners/expressions.
            // NOTE: For EPL statements we do not support calling listeners when a pattern that fires upon start.
            // Reason is that this should not be a relevant functionality of a pattern, the start pattern
            // event itself cannot carry any information and is thus ignore. Note subsequent events
            // generated by the same pattern are fine.
            int totalEventsReceived = 0;

            if (testStyle != PatternTestStyle.USE_PATTERN_LANGUAGE)
            {
                ClearListenerEvents();
                totalEventsReceived += CountExpectedEvents(EventCollection.ON_START_EVENT_ID);
            }
            else    // Patterns do need to handle event publishing upon pattern expression start (patterns that turn true right away)
            {
                CheckResults(testStyle, EventCollection.ON_START_EVENT_ID);
                totalEventsReceived += CountListenerEvents();
                ClearListenerEvents();
            }

            // Send actual test events
            var entryCollection = _sendEventCollection.ToArray();

            for (int ii = 0; ii < entryCollection.Length; ii++)
            {
                var entry   = entryCollection[ii];
                var eventId = entry.Key;

                // Manipulate the time when this event was send
                if (_sendEventCollection.GetTime(eventId) != null)
                {
                    TimerEvent currentTimeEvent = new CurrentTimeEvent(_sendEventCollection.GetTime(eventId).Value);
                    runtime.SendEvent(currentTimeEvent);
                    Log.Debug("RunTest: Sending event {0} = {1} timed {2}", entry.Key, entry.Value, currentTimeEvent);
                }

                // Send event itself
                runtime.SendEvent(entry.Value);

                // Check expected results for this event
                if (testStyle != PatternTestStyle.USE_EPL_AND_CONSUME_NOCHECK)
                {
                    CheckResults(testStyle, eventId);

                    // Count and clear the list of events that each listener has received
                    totalEventsReceived += CountListenerEvents();
                }

                ClearListenerEvents();
            }

            // Count number of expected matches
            int totalExpected = 0;

            foreach (EventExpressionCase descriptor in _caseList.Results)
            {
                totalExpected += descriptor.ExpectedResults.Values.Sum(events => events.Count);
            }

            if (totalExpected != totalEventsReceived && testStyle != PatternTestStyle.USE_EPL_AND_CONSUME_NOCHECK)
            {
                Log.Debug(".test Count expected does not match count received, expected=" + totalExpected +
                          " received=" + totalEventsReceived);
                Assert.Fail();
            }

            // Kill all expressions
            foreach (EPStatement expression in _expressions)
            {
                expression.RemoveAllEventHandlers();
            }

            // Send test events again to also test that all were indeed killed
            foreach (var entry in _sendEventCollection)
            {
                runtime.SendEvent(entry.Value);
            }

            // Make sure all listeners are still at zero
            foreach (SupportUpdateListener listener in _listeners)
            {
                if (listener.NewDataList.Count > 0)
                {
                    Log.Debug(".test A match was received after stopping all expressions");
                    Assert.Fail();
                }
            }

            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.EndTest();
            }
        }
예제 #3
0
        private void RunTest(
            RegressionEnvironment env,
            PatternTestStyle testStyle,
            AtomicLong milestone)
        {
            // Send the start time to the eventService
            if (sendEventCollection.GetTime(EventCollection.ON_START_EVENT_ID) != null) {
                var startTime = sendEventCollection.GetTime(EventCollection.ON_START_EVENT_ID);
                env.AdvanceTime(startTime.Value);
                log.Debug(".RunTest Start time is " + startTime);
            }

            // Set up expression filters and match listeners
            var expressions = new string[caseList.NumTests];
            var index = -1;
            foreach (var descriptor in caseList.Results) {
                index++;
                var epl = descriptor.ExpressionText;
                var model = descriptor.ObjectModel;
                var statementName = NameOfStatement(descriptor);
                var nameAnnotation = "@Name(\"" + statementName + "\") ";
                EPCompiled compiled;
                log.Debug(".RunTest Deploying " + epl);

                try {
                    if (model != null) {
                        model.Annotations = Collections.SingletonList(AnnotationPart.NameAnnotation(statementName));
                        var module = new Module();
                        module.Items.Add(new ModuleItem(model));
                        compiled = env.Compiler.Compile(
                            module,
                            new CompilerArguments(env.Configuration));
                    }
                    else {
                        if (testStyle == PatternTestStyle.USE_EPL) {
                            var text = nameAnnotation +
                                       "@Audit('pattern') @Audit('pattern-instances') select * from pattern [" +
                                       epl +
                                       "]";
                            compiled = env.Compile(text);
                            epl = text;
                        }
                        else if (testStyle == PatternTestStyle.USE_EPL_AND_CONSUME_NOCHECK) {
                            var text = nameAnnotation +
                                       "select * from pattern @DiscardPartialsOnMatch @SuppressOverlappingMatches [" +
                                       epl +
                                       "]";
                            compiled = env.Compile(text);
                            epl = text;
                        }
                        else if (testStyle == PatternTestStyle.COMPILE_TO_MODEL) {
                            var text = nameAnnotation + "select * from pattern [" + epl + "]";
                            var mymodel = env.Compiler.EplToModel(text, env.Configuration);
                            var module = new Module();
                            module.Items.Add(new ModuleItem(mymodel));
                            compiled = env.Compiler.Compile(
                                module,
                                new CompilerArguments(env.Configuration));
                            epl = text;
                        }
                        else if (testStyle == PatternTestStyle.COMPILE_TO_EPL) {
                            var text = "select * from pattern [" + epl + "]";
                            var mymodel = env.Compiler.EplToModel(text, env.Configuration);
                            var reverse = nameAnnotation + mymodel.ToEPL();
                            compiled = env.Compile(reverse);
                            epl = reverse;
                        }
                        else {
                            throw new ArgumentException("Unknown test style");
                        }
                    }
                }
                catch (Exception ex) {
                    var text = epl;
                    if (model != null)
                    {
                        text = "Model: " + model.ToEPL();
                    }

                    log.Error(
                        ".RunTest Failed to create statement for style " + testStyle + " pattern expression=" + text,
                        ex);

#if DO_NOT_CATCH_EXCEPTIONS
                    Assert.Fail(text + ": " + ex.Message);
                    compiled = null;
#else
                    throw;
#endif
                }

                // We stop the statement again and start after the first listener was added.
                // Thus we can handle patterns that fireStatementStopped on startup.
                var unit = compiled;
                env.Deploy(unit).AddListener(statementName);
                expressions[index] = epl;
            }

            // milestone
            env.Milestone(milestone.GetAndIncrement());

            // Some expressions may fireStatementStopped as soon as they are started, such as a "not b()" expression, for example.
            // Check results for any such listeners/expressions.
            // NOTE: For EPL statements we do not support calling listeners when a pattern that fires upon start.
            // Reason is that this should not be a relevant functionality of a pattern, the start pattern
            // event itself cannot carry any information and is thus ignore. Note subsequent events
            // generated by the same pattern are fine.
            CheckResults(testStyle, EventCollection.ON_START_EVENT_ID, expressions, env);
            var totalEventsReceived = CountExpectedEvents(EventCollection.ON_START_EVENT_ID);
            ClearListenerEvents(caseList, env);

            // Send actual test events
            foreach (var entry in sendEventCollection) {
                var eventId = entry.Key;

                // Manipulate the time when this event was send
                if (sendEventCollection.GetTime(eventId) != null) {
                    if (sendEventCollection.TryGetTime(eventId, out var currentTime)) {
                        env.AdvanceTime(currentTime);
                        log.Debug(
                            ".RunTest Sending event " +
                            entry.Key +
                            " = " +
                            entry.Value +
                            "  timed " +
                            currentTime);
                    }
                }

                // Send event itself
                env.SendEventBean(entry.Value);

                // Check expected results for this event
                if (testStyle != PatternTestStyle.USE_EPL_AND_CONSUME_NOCHECK) {
                    CheckResults(testStyle, eventId, expressions, env);

                    // Count and clear the list of events that each listener has received
                    totalEventsReceived += CountListenerEvents(caseList, env);
                }

                ClearListenerEvents(caseList, env);

                env.Milestone(milestone.GetAndIncrement());
            }

            // Count number of expected matches
            var totalExpected = 0;
            foreach (var descriptor in caseList.Results) {
                foreach (var events in descriptor.ExpectedResults.Values) {
                    totalExpected += events.Count;
                }
            }

            if (totalExpected != totalEventsReceived && testStyle != PatternTestStyle.USE_EPL_AND_CONSUME_NOCHECK) {
                log.Debug(
                    ".test Count expected does not match count received, expected=" +
                    totalExpected +
                    " received=" +
                    totalEventsReceived);
                Assert.IsTrue(false);
            }

            // Kill all expressions
            env.UndeployAll();

            // Send test events again to also test that all were indeed killed
            foreach (var entry in sendEventCollection) {
                env.SendEventBean(entry.Value);
            }

            // Make sure all listeners are still at zero
            foreach (var descriptor in caseList.Results) {
                var statementName = NameOfStatement(descriptor);
                Assert.IsNull(env.Statement(statementName));
            }
        }
예제 #4
0
        private void CheckResults(
            PatternTestStyle testStyle,
            string eventId,
            string[] expressions,
            RegressionEnvironment env)
        {
            // For each test descriptor, make sure the listener has received exactly the events expected
            var index = 0;
            log.Debug(".checkResults Checking results for event " + eventId);

            foreach (var descriptor in caseList.Results) {
                var statementName = NameOfStatement(descriptor);
                var expressionText = expressions[index];

                var allExpectedResults = descriptor.ExpectedResults;
                var listener = env.Listener(statementName);
                var receivedResults = listener.LastNewData;
                index++;

                // If nothing at all was expected for this event, make sure nothing was received
                if (!allExpectedResults.ContainsKey(eventId)) {
                    if (receivedResults != null && receivedResults.Length > 0) {
                        log.Debug(
                            ".checkResults Incorrect result for style " +
                            testStyle +
                            " expression : " +
                            expressionText);
                        log.Debug(
                            ".checkResults Expected no results for event " +
                            eventId +
                            ", but received " +
                            receivedResults.Length +
                            " events");
                        log.Debug(".checkResults Received, have " + receivedResults.Length + " entries");
                        PrintList(receivedResults);
                        Assert.IsFalse(true);
                    }

                    continue;
                }

                var expectedResults = allExpectedResults.Get(eventId);

                // Compare the result lists, not caring about the order of the elements
                try {
                    if (!CompareLists(receivedResults, expectedResults)) {
                        log.Debug(
                            ".checkResults Incorrect result for style " +
                            testStyle +
                            " expression : " +
                            expressionText);
                        log.Debug(
                            ".checkResults Expected size=" +
                            expectedResults.Count +
                            " received size=" +
                            (receivedResults == null ? 0 : receivedResults.Length));

                        log.Debug(".checkResults Expected, have " + expectedResults.Count + " entries");
                        PrintList(expectedResults);
                        log.Debug(
                            ".checkResults Received, have " +
                            (receivedResults == null ? 0 : receivedResults.Length) +
                            " entries");
                        PrintList(receivedResults);

                        Assert.IsFalse(true);
                    }
                }
                catch (Exception ex) {
                    log.Error("Unexpected exception", ex);
                    Assert.Fail("For statement '" + expressionText + "' failed to assert: " + ex.Message);
                }
            }
        }