Beispiel #1
0
        public void PerfTimerWriteException()
        {
            // Create an invalid testName using characters not allowed on host OS
            PerfTimerCollection p = new PerfTimerCollection("<>" + Path.GetInvalidFileNameChars().Aggregate("", (curr, next) => curr + next));

            p.StartTimer("testTimer");
            p.StopTimer("testTimer");
            FileLogger log = new FileLogger("PerfTimerWriteException", "PerfTimerWriteException", MessageType.GENERIC, true);

            p.Write(log);
            // Tests that an exception is thrown and logged.
            Assert.IsTrue(File.ReadAllText(log.FilePath).Contains("Could not save response time file.  Error was:"));
        }
Beispiel #2
0
        public void PerfStartStop2Timers()
        {
            PerfTimerCollection p = this.PerfTimerCollection;

            // build an object to store in the payloadstring of the PerfTimerCollection
            this.tc = new Tconfig
            {
                LogPath = Config.GetGeneralValue("FileLoggerPath"),
                Logtype = Config.GetGeneralValue("LogType"),
                WebURI  = Config.GetGeneralValue("WebServiceUri")
            };

            // store it (as a JSON string)
            p.PerfPayloadString = JsonConvert.SerializeObject(this.tc);
            string json_string = p.PerfPayloadString;

            p.StartTimer("Outer", "test1");
            System.Threading.Thread.Sleep(1000);
            p.StartTimer("Inner", "test2");
            System.Threading.Thread.Sleep(1000);
            p.StopTimer("test1");
            p.StopTimer("test2");

            // Write the log and validate the resulting file contents
            p.Write(this.Log);
            string filepath = Path.Combine(LoggingConfig.GetLogDirectory(), p.FileName);

            // If the file doesn't exist, just bail
            Assert.IsTrue(File.Exists(filepath), "File Check : Expected File does not exist:" + filepath);

            // Otherwise record the assertion as true and continue...
            SoftAssert.Assert(() => Assert.IsTrue(true, "File Check : Expected File exists."));

            PerfTimerCollection r = PerfTimerCollection.LoadPerfTimerCollection(filepath);

            // Payload check
            SoftAssert.Assert(() => Assert.AreEqual(json_string, r.PerfPayloadString, "Payload", "Validated Payload (json)"));

            // There should be 2 timers
            SoftAssert.Assert(() => Assert.AreEqual(2.ToString(), r.Timerlist.Count.ToString(), "Expected number of timers"));

            // Check the timers
            int badnamecount = 0;

            foreach (PerfTimer pt in r.Timerlist)
            {
                switch (pt.TimerName)
                {
                // Timer = test1 should have a context of Outer
                case "test1":
                    SoftAssert.Assert(() => Assert.AreEqual("Outer", pt.TimerContext, "test1", "Test1 Context"));
                    break;

                // Timer = test2 should have an empty context
                case "test2":
                    SoftAssert.Assert(() => Assert.AreEqual("Inner", pt.TimerContext, "test2", "Test2 Context"));
                    break;

                // Catch any extra timers
                default:
                    badnamecount++;
                    SoftAssert.Assert(() => Assert.IsTrue(false, "ExtraTimer", "Extra timer present: " + pt.TimerName));
                    break;
                }
            }

            if (badnamecount != 0)
            {
                // We would have logged any extra timers, so pass the ExtraTimer assert
                SoftAssert.Assert(() => Assert.IsTrue(true, "ExtraTimer"));
            }

            SoftAssert.FailTestIfAssertFailed();
        }
Beispiel #3
0
        public void PerfEndTimerThrowException()
        {
            PerfTimerCollection p = this.PerfTimerCollection;

            p.StopTimer("notStarted");
        }
Beispiel #4
0
        public void PerfDontStopTimer()
        {
            PerfTimerCollection r;
            PerfTimerCollection p = this.PerfTimerCollection;
            string filepath;

            p.StartTimer("StoppedOuter", "test1");
            p.StartTimer("test2");
            System.Threading.Thread.Sleep(1000);
            p.StopTimer("test1");
            p.StopTimer("test2");
            p.StartTimer("NotStopped", "test3");

            // Write the log and validate the resulting file contents
            p.Write(this.Log);
            filepath = Path.Combine(LoggingConfig.GetLogDirectory(), p.FileName);

            // If the file doesn't exist, just bail
            Assert.IsTrue(File.Exists(filepath), "File Check : Expected File does not exist:" + filepath);

            // Otherwise record the assertion as true and continue...
            SoftAssert.Assert(() => Assert.IsTrue(true, "File Check : Expected File exists."));

            r = PerfTimerCollection.LoadPerfTimerCollection(filepath);

            // Payload should be empty
            SoftAssert.Assert(() => Assert.IsTrue(string.IsNullOrEmpty(r.PerfPayloadString), "EmptyPayload", "Payload was not Empty! Contained: " + r.PerfPayloadString));

            // There should be 2 timers
            SoftAssert.Assert(() => Assert.AreEqual(2.ToString(), r.Timerlist.Count.ToString(), "Expected number of timers"));

            // Check the timers
            int badnamecount = 0;

            foreach (PerfTimer pt in r.Timerlist)
            {
                switch (pt.TimerName)
                {
                // Timer = test1 should have a context of StoppedOuter
                case "test1":
                    SoftAssert.Assert(() => Assert.AreEqual("StoppedOuter", pt.TimerContext, "test1", "Test1 Context"));
                    break;

                // Timer = test2 should have an empty context
                case "test2":
                    SoftAssert.Assert(() => Assert.IsTrue(string.IsNullOrEmpty(pt.TimerContext), "Timer2Context", "Context for " + pt.TimerName + " was not Empty! Contained: " + pt.TimerContext));
                    break;

                // Catch any extra timers
                default:
                    badnamecount++;
                    SoftAssert.Assert(() => Assert.IsTrue(false, "ExtraTimer", "Extra timer present: " + pt.TimerName));
                    break;
                }
            }

            if (badnamecount != 0)
            {
                // We would have logged any extra timers, so pass the ExtraTimer assert
                SoftAssert.Assert(() => Assert.IsTrue(true, "ExtraTimer"));
            }

            SoftAssert.FailTestIfAssertFailed();
        }