<commit_before>###############################################################################
#
# Copyright 2012 Pants Developers (see AUTHORS.txt)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################

import errno
import socket
import sys
import unittest

from mock import MagicMock

from pants.engine import Engine
from pants._channel import _Channel, HAS_UNIX, HAS_IPV6, InvalidAddressFormatError
import pants._channel

class TestChannelConstructorArguments(unittest.TestCase):
    def test_channel_constructor_no_args(self):
        channel = _Channel()
        self.assertTrue(channel.engine is Engine.instance())
        self.assertTrue(channel._socket is None)

    def test_channel_constructor_socket_arg(self):
        sock = socket.socket()
        channel = _Channel(socket=sock)
        self.assertTrue(channel._socket is sock)

    def test_channel_constructor_engine_arg(self):
        engine = Engine()
        channel = _Channel(engine=engine)
        self.assertTrue(channel.engine is engine)

class TestChannelEngineInteraction(unittest.TestCase):
    def test_channel_gets_added_to_engine(self):
        engine = Engine()
        engine.add_channel = MagicMock()
        channel = _Channel(socket=socket.socket(), engine=engine)
        engine.add_channel.assert_called_once_with(channel)

    def test_channel_gets_removed_from_engine(self):
        engine = Engine()
        engine.remove_channel = MagicMock()
        channel = _Channel(socket=socket.socket(), engine=engine)
        channel.close()
        engine.remove_channel.assert_called_once_with(channel)

class TestChannelFileno(unittest.TestCase):
    def test_channel_fileno_with_no_socket(self):
        channel = _Channel()
        self.assertTrue(channel.fileno is None)

    def test_channel_fileno_with_socket(self):
        sock = socket.socket()
        channel = _Channel(socket=sock)
        self.assertTrue(channel.fileno is sock.fileno())

class TestChannelClose(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()

    def test_channel_close_does_not_raise_an_exception_with_no_socket(self):
        try:
            self.channel.close()
        except TypeError:
            self.fail("Attempted to remove a socketless channel from the engine.")

    def test_channel_close_does_not_call_on_close(self):
        self.channel.on_close = MagicMock()
        self.channel.close()
        self.assertRaises(AssertionError, self.channel.on_close.assert_any_call)

class TestChannelSocketSet(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()

    def test_socket_set_with_acceptable_socket(self):
        sock = MagicMock()
        sock.family = socket.AF_INET
        sock.type = socket.SOCK_STREAM
        sock.setblocking = MagicMock()
        self.channel._socket_set(sock)
        self.assertTrue(self.channel._socket is sock)
        sock.setblocking.assert_called_once_with(False)

    def test_socket_set_with_preexisting_socket(self):
        self.channel._socket = MagicMock()
        self.assertRaises(RuntimeError, self.channel._socket_set, None)

    def test_socket_set_with_unsupported_family(self):
        sock = MagicMock()
        sock.family = 9001
        self.assertRaises(ValueError, self.channel._socket_set, sock)

    def test_socket_set_with_unsupported_type(self):
        sock = MagicMock()
        sock.family = socket.AF_INET
        sock.type = 9001
        self.assertRaises(ValueError, self.channel._socket_set, sock)

class TestChannelSocketConnect(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()
        self.sock = MagicMock()
        self.channel._socket = self.sock

    def test_connect_ex_returns_success(self):
        self.sock.connect_ex = MagicMock(return_value=0)
        self.assertTrue(self.channel._socket_connect(None))

    def test_connect_ex_returns_EISCONN(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EISCONN)
        self.assertTrue(self.channel._socket_connect(None))

    def test_connect_ex_returns_EAGAIN(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EAGAIN)
        self.assertFalse(self.channel._socket_connect(None))

    def test_connect_ex_returns_EWOULDBLOCK(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EWOULDBLOCK)
        self.assertFalse(self.channel._socket_connect(None))

    def test_connect_ex_returns_EINPROGRESS(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EINPROGRESS)
        self.assertFalse(self.channel._socket_connect(None))

    def test_connect_ex_returns_EALREADY(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EALREADY)
        self.assertFalse(self.channel._socket_connect(None))

    def test_connect_ex_returns_unknown(self):
        self.sock.connect_ex = MagicMock(return_value=-1)
        self.assertRaises(socket.error, self.channel._socket_connect, None)

    def test_connect_ex_raises_unknown(self):
        self.sock.connect_ex = MagicMock(side_effect=Exception)
        self.assertRaises(Exception, self.channel._socket_connect, None)

    def test_reraises_unknown_socket_error(self):
        self.sock.connect_ex = MagicMock(side_effect=socket.error(-1))
        self.assertRaises(socket.error, self.channel._socket_connect, None)

class TestChannelSocketBind(unittest.TestCase):
    def test_bind_is_called(self):
        channel = _Channel()
        channel._socket = MagicMock()
        channel._socket.bind = MagicMock()
        channel._socket_bind(None)
        channel._socket.bind.assert_called_once_with(None)

class TestChannelSocketListen(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()
        self.sock = MagicMock()
        self.sock.listen = MagicMock()
        self.channel._socket = self.sock

    def test_listen_is_called(self):
        self.channel._socket_listen(1)
        self.sock.listen.assert_called_once_with(1)

    @unittest.skipUnless(sys.platform.startswith("win"), "Windows-specific functionality.")
    def test_listen_backlog_is_corrected_on_windows(self):
        self.channel._socket.listen(socket.SOMAXCONN+1)
        self.sock.listen.assert_called_once_with(socket.SOMAXCONN)
    
    @unittest.skipIf(sys.platform.startswith("win"), "Non-Windows-specific functionality.")
    def test_listen_backlog_is_not_corrected_on_other_platforms(self):
        self.channel._socket.listen(socket.SOMAXCONN+1)
        self.sock.listen.assert_called_once_with(socket.SOMAXCONN+1)

class TestChannelSocketClose(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()
        self.sock = MagicMock()
        self.channel._socket = self.sock

    def test_socket_close(self):
        self.channel._socket_close()
        self.assertTrue(self.channel._socket is None)
        self.assertTrue(self.channel._closed)

    def test_shutdown_is_called(self):
        shutdown = MagicMock()
        self.sock.shutdown = shutdown<commit_msg>Fixed three broken tests in test_channel.py - they should work correctly now.<commit_after>###############################################################################
#
# Copyright 2012 Pants Developers (see AUTHORS.txt)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################

import errno
import socket
import sys
import unittest

from mock import MagicMock

from pants.engine import Engine
from pants._channel import _Channel, HAS_UNIX, HAS_IPV6, InvalidAddressFormatError
import pants._channel

class TestChannelConstructorArguments(unittest.TestCase):
    def test_channel_constructor_no_args(self):
        channel = _Channel()
        self.assertTrue(channel.engine is Engine.instance())
        self.assertTrue(channel._socket is None)

    def test_channel_constructor_socket_arg(self):
        sock = socket.socket()
        channel = _Channel(socket=sock)
        self.assertTrue(channel._socket is sock)

    def test_channel_constructor_engine_arg(self):
        engine = Engine()
        channel = _Channel(engine=engine)
        self.assertTrue(channel.engine is engine)

class TestChannelEngineInteraction(unittest.TestCase):
    def test_channel_gets_added_to_engine(self):
        engine = Engine()
        engine.add_channel = MagicMock()
        channel = _Channel(socket=socket.socket(), engine=engine)
        engine.add_channel.assert_called_once_with(channel)

    def test_channel_gets_removed_from_engine(self):
        engine = Engine()
        engine.remove_channel = MagicMock()
        channel = _Channel(socket=socket.socket(), engine=engine)
        channel.close()
        engine.remove_channel.assert_called_once_with(channel)

class TestChannelFileno(unittest.TestCase):
    def test_channel_fileno_with_no_socket(self):
        channel = _Channel()
        self.assertTrue(channel.fileno is None)

    def test_channel_fileno_with_socket(self):
        sock = socket.socket()
        channel = _Channel(socket=sock)
        self.assertTrue(channel.fileno == sock.fileno())

class TestChannelClose(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()

    def test_channel_close_does_not_raise_an_exception_with_no_socket(self):
        try:
            self.channel.close()
        except TypeError:
            self.fail("Attempted to remove a socketless channel from the engine.")

    def test_channel_close_does_not_call_on_close(self):
        self.channel.on_close = MagicMock()
        self.channel.close()
        self.assertRaises(AssertionError, self.channel.on_close.assert_any_call)

class TestChannelSocketSet(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()

    def test_socket_set_with_acceptable_socket(self):
        sock = MagicMock()
        sock.family = socket.AF_INET
        sock.type = socket.SOCK_STREAM
        sock.setblocking = MagicMock()
        self.channel._socket_set(sock)
        self.assertTrue(self.channel._socket is sock)
        sock.setblocking.assert_called_once_with(False)

    def test_socket_set_with_preexisting_socket(self):
        self.channel._socket = MagicMock()
        self.assertRaises(RuntimeError, self.channel._socket_set, None)

    def test_socket_set_with_unsupported_family(self):
        sock = MagicMock()
        sock.family = 9001
        self.assertRaises(ValueError, self.channel._socket_set, sock)

    def test_socket_set_with_unsupported_type(self):
        sock = MagicMock()
        sock.family = socket.AF_INET
        sock.type = 9001
        self.assertRaises(ValueError, self.channel._socket_set, sock)

class TestChannelSocketConnect(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()
        self.sock = MagicMock()
        self.channel._socket = self.sock

    def test_connect_ex_returns_success(self):
        self.sock.connect_ex = MagicMock(return_value=0)
        self.assertTrue(self.channel._socket_connect(None))

    def test_connect_ex_returns_EISCONN(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EISCONN)
        self.assertTrue(self.channel._socket_connect(None))

    def test_connect_ex_returns_EAGAIN(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EAGAIN)
        self.assertFalse(self.channel._socket_connect(None))

    def test_connect_ex_returns_EWOULDBLOCK(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EWOULDBLOCK)
        self.assertFalse(self.channel._socket_connect(None))

    def test_connect_ex_returns_EINPROGRESS(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EINPROGRESS)
        self.assertFalse(self.channel._socket_connect(None))

    def test_connect_ex_returns_EALREADY(self):
        self.sock.connect_ex = MagicMock(return_value=errno.EALREADY)
        self.assertFalse(self.channel._socket_connect(None))

    def test_connect_ex_returns_unknown(self):
        self.sock.connect_ex = MagicMock(return_value=-1)
        self.assertRaises(socket.error, self.channel._socket_connect, None)

    def test_connect_ex_raises_unknown(self):
        self.sock.connect_ex = MagicMock(side_effect=Exception)
        self.assertRaises(Exception, self.channel._socket_connect, None)

    def test_reraises_unknown_socket_error(self):
        self.sock.connect_ex = MagicMock(side_effect=socket.error(-1))
        self.assertRaises(socket.error, self.channel._socket_connect, None)

class TestChannelSocketBind(unittest.TestCase):
    def test_bind_is_called(self):
        channel = _Channel()
        channel._socket = MagicMock()
        channel._socket.bind = MagicMock()
        channel._socket_bind(None)
        channel._socket.bind.assert_called_once_with(None)

class TestChannelSocketListen(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()
        self.sock = MagicMock()
        self.sock.listen = MagicMock()
        self.channel._socket = self.sock

    def test_listen_is_called(self):
        self.channel._socket_listen(1)
        self.sock.listen.assert_called_once_with(1)

    @unittest.skipUnless(sys.platform.startswith("win"), "Windows-specific functionality.")
    def test_listen_backlog_is_corrected_on_windows(self):
        self.channel._socket_listen(socket.SOMAXCONN+1)
        self.sock.listen.assert_called_once_with(socket.SOMAXCONN)
    
    @unittest.skipIf(sys.platform.startswith("win"), "Non-Windows-specific functionality.")
    def test_listen_backlog_is_not_corrected_on_other_platforms(self):
        self.channel._socket_listen(socket.SOMAXCONN+1)
        self.sock.listen.assert_called_once_with(socket.SOMAXCONN+1)

class TestChannelSocketClose(unittest.TestCase):
    def setUp(self):
        self.channel = _Channel()
        self.sock = MagicMock()
        self.channel._socket = self.sock

    def test_socket_close(self):
        self.channel._socket_close()
        self.assertTrue(self.channel._socket is None)
        self.assertTrue(self.channel._closed)

    def test_shutdown_is_called(self):
        shutdown = MagicMock()
        self.sock.shutdown = shutdown
{'fraction_non_alphanumeric': 0.10884757383966245, 'fraction_numerical': 0.0032964135021097047, 'mean_word_length': 3.7536822312754623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '633688', 'n_tokens_mistral': 4454, 'n_tokens_neox': 4371, 'n_words': 774}