<commit_before>from satella.channels import LockSignalledChannel, DataNotAvailable, ChannelFailure, ChannelClosed
from satella.channels.sockets import Socket, ServerSocket, SelectHandlingLayer

from socket import AF_INET, SOCK_STREAM, socket
from threading import Thread
from time import sleep

import unittest

class SelectHandlingLayerTest(unittest.TestCase):
    def test_3_clients(self):
        class ConnectorThread(Thread):
            def run(self):
                sleep(0.2)
                sck = socket(AF_INET, SOCK_STREAM)
                sck.connect(('127.0.0.1', 50000))
                sck = Socket(sck)
                sck.write('Hello World')
                k = sck.read(1)
                sck.close()

        class MySelectHandlingLayer(SelectHandlingLayer):
            def __init__(self, utc):
                SelectHandlingLayer.__init__(self)
                self.packets_to_go = 3
                self.utc = utc
                self.can_iterate = False

            def on_iteration(self):
                # so that on_iteration also gets tested
                self.can_iterate = True

            def on_readable(self, channel):
                self.utc.assertEquals(self.can_iterate, True)
                self.can_iterate = False

                if isinstance(channel, ServerSocket):
                    self.register_channel(channel.read())
                else:
                    if channel.rxlen < 11: return
                    self.utc.assertEquals(channel.read(11), 'Hello World')
                    self.packets_to_go -= 1
                    channel.write('1')
        
        shl = MySelectHandlingLayer(self)

        sck = socket(AF_INET, SOCK_STREAM)
        sck.bind(('127.0.0.1', 50000))
        sck.listen(10)

        shl.register_channel(ServerSocket(sck))

        for x in xrange(0, 3): ConnectorThread().start()

        while shl.packets_to_go != 0:
            shl.select()


class SocketsTest(unittest.TestCase):<commit_msg>test will now check for closing sockets<commit_after>from satella.channels import LockSignalledChannel, DataNotAvailable, ChannelFailure, ChannelClosed
from satella.channels.sockets import Socket, ServerSocket, SelectHandlingLayer

from socket import AF_INET, SOCK_STREAM, socket
from threading import Thread
from time import sleep

import unittest

class SelectHandlingLayerTest(unittest.TestCase):
    def test_3_clients(self):
        class ConnectorThread(Thread):
            def run(self):
                sleep(0.2)
                sck = socket(AF_INET, SOCK_STREAM)
                sck.connect(('127.0.0.1', 50000))
                sck = Socket(sck)
                sck.write('Hello World')
                k = sck.read(1)
                sck.close()

        class MySelectHandlingLayer(SelectHandlingLayer):
            def __init__(self, utc):
                SelectHandlingLayer.__init__(self)
                self.packets_to_go = 3
                self.sockets_to_close = 3
                self.utc = utc
                self.can_iterate = False

            def on_iteration(self):
                # so that on_iteration also gets tested
                self.can_iterate = True

            def on_closed(self, channel):
                self.sockets_to_close -= 1

            def on_readable(self, channel):
                self.utc.assertEquals(self.can_iterate, True)
                self.can_iterate = False

                if isinstance(channel, ServerSocket):
                    self.register_channel(channel.read())
                else:
                    if channel.rxlen < 11: return
                    self.utc.assertEquals(channel.read(11), 'Hello World')
                    self.packets_to_go -= 1
                    channel.write('1')


        
        shl = MySelectHandlingLayer(self)

        sck = socket(AF_INET, SOCK_STREAM)
        sck.bind(('127.0.0.1', 50000))
        sck.listen(10)

        shl.register_channel(ServerSocket(sck))

        for x in xrange(0, 3): ConnectorThread().start()

        while (shl.packets_to_go != 0) or (shl.sockets_to_close != 0):
            shl.select()


class SocketsTest(unittest.TestCase):
{'fraction_non_alphanumeric': 0.08620274500361184, 'fraction_numerical': 0.019022393450517698, 'mean_word_length': 1.8316291751874574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7222218', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1140, 'n_words': 245}