<commit_before>import unittest

from tornado import testing

from stormed.connection import Connection
from stormed.channel import Consumer
from stormed.message import Message
from stormed.method import queue
from stormed.frame import status
from stormed.util import AmqpStatusError


class TestChannel(testing.AsyncTestCase):

    def test_open(self):
        conn = Connection('localhost', io_loop=self.io_loop)

        def clean_up(**kargs):
            conn.close(self.stop)

        def on_connect():
            ch = conn.channel(callback=clean_up)

        conn.connect(on_connect)
        self.wait()

    def test_close(self):
        conn = Connection('localhost', io_loop=self.io_loop)

        def on_connect():
            self.ch = conn.channel(callback=on_channel)

        def on_channel():
            self.ch.close(when_channel_close)

        def when_channel_close():
            try:
                self.ch.get('foo', callback=lambda msg: None)
            except AmqpStatusError, e:
                clean_up()
            else:
                assert False, "Expected AmqpStatusError"

        def clean_up():
            conn.close(self.stop)

        conn.connect(on_connect)
        self.wait()

    def test_publish(self):
        conn = Connection('localhost', io_loop=self.io_loop)
        test_msg = Message('test')

        def on_connect():
            ch = conn.channel()
            ch.exchange_declare('test_exchange', durable=False)
            ch.publish(test_msg, exchange='test_exchange', routing_key='test')
            conn.close(self.stop)

        conn.connect(on_connect)
        self.wait()

    def test_publish_huge(self):
        conn = Connection('localhost', io_loop=self.io_loop)
        msg = "test message" * 151343
        test_msg = Message(msg)

        def on_connect():
            ch = conn.channel()
            def callback(msg):
                conn.close(self.stop)

            def on_creation(q):
                ch.publish(test_msg, exchange='test_exchange', routing_key='test')
                ch.consume(q.queue, callback)

            ch.exchange_declare('test_exchange', durable=False)
            ch.queue_declare('test_queue', durable=False, callback=on_creation)
            ch.queue_bind('test_queue', 'test_exchange', 'test')

        conn.connect(on_connect)
        self.wait(timeout=1)

    def test_queue(self):
        conn = Connection('localhost', io_loop=self.io_loop)

        def on_creation(qinfo):
            assert qinfo.queue == 'test_queue'
            assert qinfo.message_count == 0
            assert qinfo.consumer_count == 0

        def on_connect():
            ch = conn.channel()
            ch.queue_delete('test_queue')
            ch.queue_declare('test_queue', durable=False,<commit_msg>COSMIT: teawks to test_publish_huge<commit_after>import unittest

from tornado import testing

from stormed.connection import Connection
from stormed.channel import Consumer
from stormed.message import Message
from stormed.method import queue
from stormed.frame import status
from stormed.util import AmqpStatusError


class TestChannel(testing.AsyncTestCase):

    def test_open(self):
        conn = Connection('localhost', io_loop=self.io_loop)

        def clean_up(**kargs):
            conn.close(self.stop)

        def on_connect():
            ch = conn.channel(callback=clean_up)

        conn.connect(on_connect)
        self.wait()

    def test_close(self):
        conn = Connection('localhost', io_loop=self.io_loop)

        def on_connect():
            self.ch = conn.channel(callback=on_channel)

        def on_channel():
            self.ch.close(when_channel_close)

        def when_channel_close():
            try:
                self.ch.get('foo', callback=lambda msg: None)
            except AmqpStatusError, e:
                clean_up()
            else:
                assert False, "Expected AmqpStatusError"

        def clean_up():
            conn.close(self.stop)

        conn.connect(on_connect)
        self.wait()

    def test_publish(self):
        conn = Connection('localhost', io_loop=self.io_loop)
        test_msg = Message('test')

        def on_connect():
            ch = conn.channel()
            ch.exchange_declare('test_exchange', durable=False)
            ch.publish(test_msg, exchange='test_exchange', routing_key='test')
            conn.close(self.stop)

        conn.connect(on_connect)
        self.wait()

    def test_publish_huge(self):
        conn = Connection('localhost', io_loop=self.io_loop)
        msg = "test message" * 151343
        test_msg = Message(msg)

        def on_connect():
            ch = conn.channel()
            def callback(msg):
                conn.close(self.stop)

            def on_creation(q):
                ch.publish(test_msg, exchange='test_exchange',
                                     routing_key='test')
                ch.consume(q.queue, callback)

            ch.exchange_declare('test_exchange', durable=False)
            ch.queue_delete('test_queue')
            ch.queue_declare('test_queue', durable=False, callback=on_creation)
            ch.queue_bind('test_queue', 'test_exchange', 'test')

        conn.connect(on_connect)
        self.wait(timeout=1)

    def test_queue(self):
        conn = Connection('localhost', io_loop=self.io_loop)

        def on_creation(qinfo):
            assert qinfo.queue == 'test_queue'
            assert qinfo.message_count == 0
            assert qinfo.consumer_count == 0

        def on_connect():
            ch = conn.channel()
            ch.queue_delete('test_queue')
            ch.queue_declare('test_queue', durable=False,
{'fraction_non_alphanumeric': 0.10237467018469656, 'fraction_numerical': 0.0031662269129287598, 'mean_word_length': 2.2033802816901407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1509083', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1562, 'n_words': 315}