Example #1
0
        public void Test_WriteEvent_ArgsCornerCases()
        {
            TestUtilities.CheckNoEventSourcesRunning("Start");
            using (var log = new EventSourceTest())
            {
                using (var el = new LoudListener(log))
                {
                    // coverage for EventSource.SendCommand()
                    var options = new Dictionary <string, string>()
                    {
                        { "arg", "val" }
                    };
                    EventSource.SendCommand(log, EventCommand.SendManifest, options);


                    log.EventWith7Strings("s0", "s1", "s2", "s3", "s4", "s5", "s6");
                    Assert.Equal(26, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(7, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal("s0", (string)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal("s6", (string)LoudListener.t_lastEvent.Payload[6]);

                    log.EventWith9Strings("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8");
                    Assert.Equal(27, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(9, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal("s0", (string)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal("s8", (string)LoudListener.t_lastEvent.Payload[8]);

                    Test_WriteEvent_ArgsCornerCases_TestEtw(log);
                }
            }
            TestUtilities.CheckNoEventSourcesRunning("Stop");
        }
Example #2
0
        public void Test_WriteEvent_ArgsCornerCases()
        {
            TestUtilities.CheckNoEventSourcesRunning("Start");
            using (var log = new EventSourceTest())
            {
                using (var el = new LoudListener())
                {
                    // coverage for EventSource.SendCommand()
                    var options = new Dictionary <string, string>()
                    {
                        { "arg", "val" }
                    };
                    EventSource.SendCommand(log, EventCommand.SendManifest, options);

                    Guid guid = Guid.NewGuid();
#if USE_ETW // TODO: Enable when TraceEvent is available on CoreCLR. GitHub issue #4864.
                    log.EventWithManyTypeArgs("Hello", 0, 0, 0, 'a', 0, 0, 0, 0, (float)10.0, (double)11.0, guid);
                    Assert.Equal(25, LoudListener.LastEvent.EventId);
                    Assert.Equal(12, LoudListener.LastEvent.Payload.Count);
                    Assert.Equal("Hello", (string)LoudListener.LastEvent.Payload[0]);
                    Assert.Equal(0, (long)LoudListener.LastEvent.Payload[1]);
                    Assert.Equal((uint)0, (uint)LoudListener.LastEvent.Payload[2]);
                    Assert.Equal((ulong)0, (ulong)LoudListener.LastEvent.Payload[3]);
                    Assert.Equal('a', (char)LoudListener.LastEvent.Payload[4]);
                    Assert.Equal((byte)0, (byte)LoudListener.LastEvent.Payload[5]);
                    Assert.Equal((sbyte)0, (sbyte)LoudListener.LastEvent.Payload[6]);
                    Assert.Equal((short)0, (short)LoudListener.LastEvent.Payload[7]);
                    Assert.Equal((ushort)0, (ushort)LoudListener.LastEvent.Payload[8]);
                    Assert.Equal((float)10.0, (float)LoudListener.LastEvent.Payload[9]);
                    Assert.Equal((double)11.0, (double)LoudListener.LastEvent.Payload[10]);
                    Assert.Equal(guid, (Guid)LoudListener.LastEvent.Payload[11]);
#endif // USE_ETW
                    log.EventWith7Strings("s0", "s1", "s2", "s3", "s4", "s5", "s6");
                    Assert.Equal(26, LoudListener.LastEvent.EventId);
                    Assert.Equal(7, LoudListener.LastEvent.Payload.Count);
                    Assert.Equal("s0", (string)LoudListener.LastEvent.Payload[0]);
                    Assert.Equal("s6", (string)LoudListener.LastEvent.Payload[6]);

                    log.EventWith9Strings("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8");
                    Assert.Equal(27, LoudListener.LastEvent.EventId);
                    Assert.Equal(9, LoudListener.LastEvent.Payload.Count);
                    Assert.Equal("s0", (string)LoudListener.LastEvent.Payload[0]);
                    Assert.Equal("s8", (string)LoudListener.LastEvent.Payload[8]);
#if USE_ETW // TODO: Enable when TraceEvent is available on CoreCLR. GitHub issue #4864.
                    log.EventWithWeirdArgs(IntPtr.Zero, true, MyLongEnum.LongVal1 /*, 9999999999999999999999999999m*/);
                    Assert.Equal(30, LoudListener.LastEvent.EventId);
                    Assert.Equal(3 /*4*/, LoudListener.LastEvent.Payload.Count);
                    Assert.Equal(IntPtr.Zero, (IntPtr)LoudListener.LastEvent.Payload[0]);
                    Assert.Equal(true, (bool)LoudListener.LastEvent.Payload[1]);
                    Assert.Equal(MyLongEnum.LongVal1, (MyLongEnum)LoudListener.LastEvent.Payload[2]);
                    // Assert.Equal(9999999999999999999999999999m, (decimal)LoudListener.LastEvent.Payload[3]);
#endif // USE_ETW
                }
            }
            TestUtilities.CheckNoEventSourcesRunning("Stop");
        }
Example #3
0
        /// <summary>
        /// Helper method for the two tests above.
        /// </summary>
        private void Test_WriteEvent(Listener listener, bool useSelfDescribingEvents, bool isEtwListener = false)
        {
            using (var logger = new EventSourceTest(useSelfDescribingEvents))
            {
                var tests = new List <SubTest>();

                /*************************************************************************/
                tests.Add(new SubTest("WriteEvent/Basic/EventII",
                                      delegate()
                                      { logger.EventII(10, 11); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventII", evt.EventName);
                    Assert.Equal(10, evt.PayloadValue(0, "arg1"));
                    Assert.Equal(11, evt.PayloadValue(1, "arg2"));
                }));
                /*************************************************************************/
                tests.Add(new SubTest("WriteEvent/Basic/EventSS",
                                      delegate()
                                      { logger.EventSS("one", "two"); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventSS", evt.EventName);
                    Assert.Equal("one", evt.PayloadValue(0, "arg1"));
                    Assert.Equal("two", evt.PayloadValue(1, "arg2"));
                }));
                /*************************************************************************/
                tests.Add(new SubTest("Write/Basic/EventWith7Strings",
                                      delegate()
                {
                    logger.EventWith7Strings("s0", "s1", "s2", "s3", "s4", "s5", "s6");
                },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventWith7Strings", evt.EventName);
                    Assert.Equal("s0", (string)evt.PayloadValue(0, "s0"));
                    Assert.Equal("s6", (string)evt.PayloadValue(6, "s6"));
                }));
                /*************************************************************************/
                tests.Add(new SubTest("Write/Basic/EventWith9Strings",
                                      delegate()
                {
                    logger.EventWith9Strings("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8");
                },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventWith9Strings", evt.EventName);
                    Assert.Equal("s0", (string)evt.PayloadValue(0, "s0"));
                    Assert.Equal("s8", (string)evt.PayloadValue(8, "s8"));
                }));
                /*************************************************************************/
                Test_WriteEvent_AddEtwTests(tests, logger);

                /*************************************************************************/
                /*************************** ENUM TESTING *******************************/
                /*************************************************************************/

                /*************************************************************************/
                tests.Add(new SubTest("WriteEvent/Enum/EventEnum",
                                      delegate()
                {
                    logger.EventEnum(SdtEventSources.MyColor.Blue);
                },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventEnum", evt.EventName);

                    Assert.Equal(1, ((IConvertible)evt.PayloadValue(0, "x")).ToInt32(null));
                    if (evt.IsEtw && !useSelfDescribingEvents)
                    {
                        Assert.Equal("Blue", evt.PayloadString(0, "x"));
                    }
                }));

                tests.Add(new SubTest("WriteEvent/Enum/EventEnum1",
                                      delegate()
                {
                    logger.EventEnum1(MyColor.Blue);
                },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventEnum1", evt.EventName);

                    Assert.Equal(1, ((IConvertible)evt.PayloadValue(0, "x")).ToInt32(null));
                    if (evt.IsEtw && !useSelfDescribingEvents)
                    {
                        Assert.Equal("Blue", evt.PayloadString(0, "x"));
                    }
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithIntIntString",
                                      delegate()
                                      { logger.EventWithIntIntString(10, 11, "test"); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventWithIntIntString", evt.EventName);
                    Assert.Equal(10, evt.PayloadValue(0, "i1"));
                    Assert.Equal(11, evt.PayloadValue(1, "i2"));
                    Assert.Equal("test", evt.PayloadValue(2, "str"));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithIntLongString",
                                      delegate()
                                      { logger.EventWithIntLongString(10, (long)11, "test"); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventWithIntLongString", evt.EventName);
                    Assert.Equal(10, evt.PayloadValue(0, "i1"));
                    Assert.Equal(evt.PayloadValue(1, "l1"), (long)11);
                    Assert.Equal("test", evt.PayloadValue(2, "str"));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithString",
                                      delegate()
                                      { logger.EventWithString(null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(1, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                }));


                tests.Add(new SubTest("WriteEvent/Basic/EventWithIntAndString",
                                      delegate()
                                      { logger.EventWithIntAndString(12, null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal(12, evt.PayloadValue(0, null));
                    Assert.Equal("", evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithLongAndString",
                                      delegate()
                                      { logger.EventWithLongAndString(120L, null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal(120L, evt.PayloadValue(0, null));
                    Assert.Equal("", evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndInt",
                                      delegate()
                                      { logger.EventWithStringAndInt(null, 12); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal(12, evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndIntAndInt",
                                      delegate()
                                      { logger.EventWithStringAndIntAndInt(null, 12, 13); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(3, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal(12, evt.PayloadValue(1, null));
                    Assert.Equal(13, evt.PayloadValue(2, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndLong",
                                      delegate()
                                      { logger.EventWithStringAndLong(null, 120L); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal(120L, evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndString",
                                      delegate()
                                      { logger.EventWithStringAndString(null, null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal("", evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndStringAndString",
                                      delegate()
                                      { logger.EventWithStringAndStringAndString(null, null, null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(3, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal("", evt.PayloadValue(1, null));
                    Assert.Equal("", evt.PayloadValue(2, null));
                }));

                // Self-describing ETW does not support NULL arguments.
                if (useSelfDescribingEvents && !(isEtwListener))
                {
                    tests.Add(new SubTest("WriteEvent/Basic/EventVarArgsWithString",
                                          delegate()
                                          { logger.EventVarArgsWithString(1, 2, 12, null); },
                                          delegate(Event evt)
                    {
                        Assert.Equal(logger.Name, evt.ProviderName);
                        Assert.Equal(4, evt.PayloadCount);
                        Assert.Equal(1, evt.PayloadValue(0, null));
                        Assert.Equal(2, evt.PayloadValue(1, null));
                        Assert.Equal(12, evt.PayloadValue(2, null));
                        Assert.Equal("", evt.PayloadValue(3, null));
                    }));
                }

                // Probably belongs in the user TestUsersErrors.cs.
                if (!useSelfDescribingEvents)
                {
                    tests.Add(new SubTest("WriteEvent/Basic/EventWithIncorrectNumberOfParameters",
                                          delegate()
                    {
                        logger.EventWithIncorrectNumberOfParameters("TestMessage", "TestPath", 10);
                    },
                                          delegate(List <Event> evts)
                    {
                        Assert.True(0 < evts.Count);

                        // We give an error message in EventListener case but not the ETW case.
                        if (1 < evts.Count)
                        {
                            Assert.Equal(2, evts.Count);
                            Assert.Equal(logger.Name, evts[0].ProviderName);
                            Assert.Equal("EventSourceMessage", evts[0].EventName);
                            string errorMsg = evts[0].PayloadString(0, "message");
                            Assert.Matches("called with 1.*defined with 3", errorMsg);
                        }

                        int eventIdx = evts.Count - 1;
                        Assert.Equal(logger.Name, evts[eventIdx].ProviderName);
                        Assert.Equal("EventWithIncorrectNumberOfParameters", evts[eventIdx].EventName);
                        Assert.Equal("{TestPath:10}TestMessage", evts[eventIdx].PayloadString(0, "message"));
                    }));
                }

                // If you only wish to run one or several of the tests you can filter them here by
                // Uncommenting the following line.
                // tests = tests.FindAll(test => Regex.IsMatch(test.Name, "ventWithByteArray"));

                // Next run the same tests with the TraceLogging path.
                EventTestHarness.RunTests(tests, listener, logger);
            }
        }