<commit_before># -*- coding: utf-8 -*-
import json
import unittest
from random import randint
from jsonrpc.jsonrpc2 import JSONRPC20Request
from rocketws.exceptions import ImproperlyConfigured

from rocketws.registry import ChannelRegistry, SocketRegistry
from geventwebsocket.handler import Client
from jsonrpc import JSONRPCResponseManager
from rocketws.rpc import ui_dispatcher, ms_dispatcher, registry
import mock
from rocketws.messages_sources.base import BaseMessagesSource


def get_ws_client(address='127.0.0.1', port=None):
    if port is None:
        port = randint(10000, 65535)
    client = Client(address=tuple([address, port]), ws=mock.MagicMock())
    client.ws.send = mock.MagicMock(return_value=True)
    return client


class ModelsTestCase(unittest.TestCase):
    def test_base_message_source(self):
        class ConcreteMessagesSource(BaseMessagesSource):
            pass

        # Can't instantiate abstract class with abstract methods start, stop
        self.assertRaises(TypeError, ConcreteMessagesSource)

        class ConcreteMessagesSource2(BaseMessagesSource):
            def start(self):
                pass

            def stop(self):
                pass

        # Can't instantiate MessagesSource with un-callable callback
        self.assertRaises(
            ValueError, ConcreteMessagesSource2, 'not a function')


class CommonMethodsTestCase(unittest.TestCase):
    def test_get_configured_messages_source(self):
        from rocketws.server import get_configured_messages_source

        # currently supported `http` and `rabbitmq` sources
        http_source = get_configured_messages_source(name='http')
        self.assertIsInstance(http_source, BaseMessagesSource)

        rabbitmq_source = get_configured_messages_source(name='rabbitmq')
        self.assertIsInstance(rabbitmq_source, BaseMessagesSource)

        self.assertRaises(
            ImproperlyConfigured,
            get_configured_messages_source,
            name='unknown'
        )


class ChannelRegistryTestCase(unittest.TestCase):
    def setUp(self):
        self.registry = ChannelRegistry()

    def test_singleton(self):
        same_registry = ChannelRegistry()
        self.assertEqual(self.registry, same_registry)

    def test_subscribe(self):
        self.registry.flush_all()
        channel = 'john'
        client = get_ws_client()
        self.registry.subscribe(channel, client)

        self.assertEqual(len(self.registry.subscribers), 1)
        registered_client = self.registry.subscribers[0]
        self.assertEqual(registered_client.address, client.address)
        del client

        # Expected NoneType reference still in sessions
        self.assertEqual(len(self.registry.subscribers), 1)<commit_msg>fixed tests<commit_after># -*- coding: utf-8 -*-
import json
import unittest
from random import randint
from jsonrpc.jsonrpc2 import JSONRPC20Request
from rocketws.exceptions import ImproperlyConfigured

from rocketws.registry import ChannelRegistry, SocketRegistry
from geventwebsocket.handler import Client
from jsonrpc import JSONRPCResponseManager
from rocketws.rpc import ui_dispatcher, ms_dispatcher, registry
import mock
from rocketws.messages_sources.base import BaseMessagesSource


def get_ws_client(address='127.0.0.1', port=None):
    if port is None:
        port = randint(10000, 65535)
    client = Client(address=tuple([address, port]), ws=mock.MagicMock())
    client.ws.send = mock.MagicMock(return_value=True)
    return client


class ModelsTestCase(unittest.TestCase):
    def test_base_message_source(self):
        class ConcreteMessagesSource(BaseMessagesSource):
            pass

        # Can't instantiate abstract class with abstract methods start, stop
        self.assertRaises(TypeError, ConcreteMessagesSource)

        class ConcreteMessagesSource2(BaseMessagesSource):
            def start(self):
                pass

            def stop(self):
                pass

        # Can't instantiate MessagesSource with un-callable callback
        self.assertRaises(
            ValueError, ConcreteMessagesSource2, 'not a function')


class CommonMethodsTestCase(unittest.TestCase):
    def test_get_configured_messages_source(self):
        from rocketws.server import get_configured_messages_source

        http_source = get_configured_messages_source(name='http')
        self.assertIsInstance(http_source, BaseMessagesSource)

        self.assertRaises(
            ImproperlyConfigured,
            get_configured_messages_source,
            name='unknown'
        )


class ChannelRegistryTestCase(unittest.TestCase):
    def setUp(self):
        self.registry = ChannelRegistry()

    def test_singleton(self):
        same_registry = ChannelRegistry()
        self.assertEqual(self.registry, same_registry)

    def test_subscribe(self):
        self.registry.flush_all()
        channel = 'john'
        client = get_ws_client()
        self.registry.subscribe(channel, client)

        self.assertEqual(len(self.registry.subscribers), 1)
        registered_client = self.registry.subscribers[0]
        self.assertEqual(registered_client.address, client.address)
        del client

        # Expected NoneType reference still in sessions
        self.assertEqual(len(self.registry.subscribers), 1)
{'fraction_non_alphanumeric': 0.07423498299962221, 'fraction_numerical': 0.009444654325651681, 'mean_word_length': 3.3832781456953644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1422441', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1376, 'n_words': 316}