<commit_before>﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Xunit;

namespace Simple.MailServer.Tests
{
    public class PortListenerFacts
    {
        [Fact]
        public void port_listener_is_listening_to_port()
        {
            var testPort = GetTestPort();
            using (var portListener = new PortListener(IPAddress.Loopback, testPort))
            {
                var clientConnectedRaised = new ManualResetEventSlim();
                portListener.ClientConnected += (sender, client) => clientConnectedRaised.Set();
                portListener.StartListen();

                using (var client = new TcpClient())
                {
                    client.Connect(IPAddress.Loopback.ToString(), testPort);
                    Assert.True(clientConnectedRaised.Wait(200));
                }
            }
        }

        [Fact]
        public void port_listener_cannot_start_twice()
        {
            var testPort = GetTestPort();
            using (var portListener = new PortListener(IPAddress.Loopback, testPort))
            {
                portListener.StartListen();
                Assert.Throws<InvalidOperationException>(() => portListener.StartListen());
            }
        }

        [Fact]
        public void StartListen_then_StopListen_could_not_be_able_to_connect()
        {
            var testPort = GetTestPort();
            using (var portListener = new PortListener(IPAddress.Loopback, testPort))
            {
                portListener.StartListen();
                portListener.StopListen();

                using (var client = new TcpClient())
                {
                    var task = client.ConnectAsync(IPAddress.Loopback.ToString(), testPort);
                    Assert.False(task.Wait(200));
                }
            }
        }

        #region<commit_msg>modifed test in PortListenerFacts to catch expected SocketException<commit_after>﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Xunit;

namespace Simple.MailServer.Tests
{
    public class PortListenerFacts
    {
        [Fact]
        public void port_listener_is_listening_to_port()
        {
            var testPort = GetTestPort();
            using (var portListener = new PortListener(IPAddress.Loopback, testPort))
            {
                var clientConnectedRaised = new ManualResetEventSlim();
                portListener.ClientConnected += (sender, client) => clientConnectedRaised.Set();
                portListener.StartListen();

                using (var client = new TcpClient())
                {
                    client.Connect(IPAddress.Loopback.ToString(), testPort);
                    Assert.True(clientConnectedRaised.Wait(200));
                }
            }
        }

        [Fact]
        public void port_listener_cannot_start_twice()
        {
            var testPort = GetTestPort();
            using (var portListener = new PortListener(IPAddress.Loopback, testPort))
            {
                portListener.StartListen();
                Assert.Throws<InvalidOperationException>(() => portListener.StartListen());
            }
        }

        [Fact]
        public void StartListen_then_StopListen_could_not_be_able_to_connect()
        {
            var testPort = GetTestPort();
            using (var portListener = new PortListener(IPAddress.Loopback, testPort))
            {
                portListener.StartListen();
                portListener.StopListen();

                using (var client = new TcpClient())
                {
                    try
                    {
                        var task = client.ConnectAsync(IPAddress.Loopback.ToString(), testPort);
                        Assert.False(task.Wait(200));
                    }
                    catch (SocketException)
                    {
                        // this is expected
                    }
                }
            }
        }

        #region
{'fraction_non_alphanumeric': 0.08482142857142858, 'fraction_numerical': 0.002976190476190476, 'mean_word_length': 1.583600256245996, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2640379', 'n_tokens_mistral': 1024, 'n_tokens_neox': 956, 'n_words': 186}