Exemplo n.º 1
0
        public void buggy_handlers_are_removed_and_their_errors_are_caught()
        {
            CriticalErrorCollector c   = new CriticalErrorCollector();
            int buggyEventHandlerCount = 0;

            var goodCollector = new List <string>();
            Action <CriticalErrorCollector.ErrorEventArgs> addMsg = errorEvent =>
            {
                lock ( goodCollector )
                {
                    foreach (var e in errorEvent.Errors)
                    {
                        goodCollector.Add(e.Exception.Message);
                    }
                }
            };

            var hGood = new EventHandler <CriticalErrorCollector.ErrorEventArgs>((sender, e) => addMsg(e));
            var hBad  = new EventHandler <CriticalErrorCollector.ErrorEventArgs>((sender, e) => { ++buggyEventHandlerCount; throw new Exception("From buggy handler."); });

            c.OnErrorFromBackgroundThreads += hGood;
            c.OnErrorFromBackgroundThreads += hBad;
            try
            {
                c.Add(new Exception("The-Test-Exception-Message"), "First call");
                c.WaitOnErrorFromBackgroundThreadsPending();
                buggyEventHandlerCount.Should().Be(1);
                goodCollector.Count.Should().Be(2, "We also received the error of the buggy handler :-).");
                if (goodCollector.Count != 2)
                {
                    // Forces the display of the messages.
                    string.Join(Environment.NewLine + "-" + Environment.NewLine, goodCollector)
                    .Should().Be("Only 2 messages should have been received.");
                }

                c.Add(new Exception("The-Test-Exception-Message"), "Second call");
                c.WaitOnErrorFromBackgroundThreadsPending();
                goodCollector.Count.Should().Be(3);
                buggyEventHandlerCount.Should().Be(1);
            }
            finally
            {
                c.OnErrorFromBackgroundThreads -= hGood;
                c.OnErrorFromBackgroundThreads -= hBad;
            }
        }
Exemplo n.º 2
0
        public void catching_one_event()
        {
            CriticalErrorCollector c = new CriticalErrorCollector();

            CriticalErrorCollector.ErrorEventArgs catched          = null;
            EventHandler <CriticalErrorCollector.ErrorEventArgs> h = (sender, e) => catched = e;

            c.OnErrorFromBackgroundThreads += h;
            try
            {
                c.Add(new Exception("The-Test-Exception-Message"), "Produced by SimpleTest");
                c.WaitOnErrorFromBackgroundThreadsPending();
                catched.Should().NotBeNull();
                catched.Errors.Should().HaveCount(1);
                catched.Errors[0].Exception.Message.Should().Contain("The-Test-Exception-Message");
                catched.Errors[0].Comment.Should().Contain("Produced by SimpleTest");
            }
            finally
            {
                c.OnErrorFromBackgroundThreads -= h;
            }
        }