<commit_before>﻿using NUnit.Framework;
using SMTPileIt.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SMTPileIt.Tests
{
    [TestFixture]
    class EventTests
    {
        SMTPServer Server { get; set; }

        [SetUp]
        public void SetupServer()
        {
            Server = GetServer();
        }

        [TearDown]
        public void TearDownServer()
        {
            Server.Stop();
        }

        [Test]
        public void ClientConnectedEventFires()
        {
            Server.ClientConnected += TestConnectMethod;
            Server.Start();
            try
            {
                SendTestEmail();
            }
            catch (Exception) /* This is going to throw since the server will abort */
            { }
        }

        [Test]
        public void ClientDisconnectedEventFires()
        {
            Server.ClientDisconnected += TestDisconnectMethod;
            Server.Start();
            SendTestEmail();
        }

        [Test]
        public void MailSentEventFires()
        {
            Server.EmailProcessed += TestMailSentMethod;
            Server.Start();
            SendTestEmail();
        }

        private void TestMailSentMethod(object sender, EmailProcessedEventArgs eventArgs)
        {
            Assert.Pass("Successfully fired the mail sent method");
        }

        private void TestDisconnectMethod(object sender, MailClientDisconnectedEventArgs eventArgs)
        {
            Assert.Pass("Successfully fired the client disconnected method");
        }

        private void TestConnectMethod(object sender, MailClientConnectedEventArgs eventArgs)
        {
            Assert.Pass("Successfully fired the client connected method");
        }

        private SMTPServer GetServer()
        {
            var server = new SMTPServer("127.0.0.1", 25026);<commit_msg>Added three tests for exception behavior within events.  Events with exceptions thrown should not interrupt the server.  This may be alterable by configuration in a later commit.<commit_after>﻿using NUnit.Framework;
using SMTPileIt.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SMTPileIt.Tests
{
    [TestFixture]
    class EventTests
    {
        SMTPServer Server { get; set; }

        [SetUp]
        public void SetupServer()
        {
            Server = GetServer();
        }

        [TearDown]
        public void TearDownServer()
        {
            Server.Stop();
        }

        [Test]
        public void ClientConnectedEventFires()
        {
            Server.ClientConnected += TestConnectMethod;
            Server.Start();
            try
            {
                SendTestEmail();
            }
            catch (Exception) /* This is going to throw since the server will abort */
            { }
        }

        [Test]
        public void ClientDisconnectedEventFires()
        {
            Server.ClientDisconnected += TestDisconnectMethod;
            Server.Start();
            SendTestEmail();
        }

        [Test]
        public void MailSentEventFires()
        {
            Server.EmailProcessed += TestMailSentMethod;
            Server.Start();
            SendTestEmail();
        }

        [Test]
        public void ExceptionInConnectedEventDoesNotInterruptServer()
        {
            Server.ClientConnected += ExceptionEventMethod;
            Assert.DoesNotThrow(() =>
            {
                Server.Start();
                SendTestEmail();
            });
        }

        [Test]
        public void ExceptionInDisconnectedEventDoesNotInterruptServer()
        {
            Server.ClientDisconnected += ExceptionEventMethod;
            Assert.DoesNotThrow(() =>
            {
                Server.Start();
                SendTestEmail();
            });
        }

        [Test]
        public void ExceptionInMailEventDoesNotInterruptServer()
        {
            Server.EmailProcessed += ExceptionEventMethod;
            Assert.DoesNotThrow(() =>
            {
                Server.Start();
                SendTestEmail();
            });
        }

        private void ExceptionEventMethod(object sender, MailClientEventArgs eventArgs)
        {
            throw new InvalidOperationException("We done bad.");
        }

        private void TestMailSentMethod(object sender, EmailProcessedEventArgs eventArgs)
        {
            Assert.Pass("Successfully fired the mail sent method");
        }

        private void TestDisconnectMethod(object sender, MailClientDisconnectedEventArgs eventArgs)
        {
            Assert.Pass("Successfully fired the client disconnected method");
        }

        private void TestConnectMethod(object sender, MailClientConnectedEventArgs eventArgs)
        {
            Assert.Pass("Successfully fired the client connected method");
        }

        private SMTPServer GetServer()
        {
            var server = new SMTPServer("127.0.0.1", 25026);
{'fraction_non_alphanumeric': 0.07965116279069767, 'fraction_numerical': 0.004263565891472868, 'mean_word_length': 1.7628479657387581, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7641771', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1224, 'n_words': 297}