示例#1
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);
            }
        }