# Path: server/websockets/compatibility.py

# 
# Path: server/websockets/exceptions.py
# class ConnectionClosed(InvalidState):
#     """
#     Exception raised when trying to read or write on a closed connection.
# 
#     Provides the connection close code and reason in its ``code`` and
#     ``reason`` attributes respectively.
# 
#     """
#     def __init__(self, code, reason):
#         self.code = code
#         self.reason = reason
#         message = 'WebSocket connection is closed: '
#         message += 'code = {}, '.format(code) if code else 'no code, '
#         message += 'reason = {}.'.format(reason) if reason else 'no reason.'
#         super().__init__(message)
# 
# class InvalidState(Exception):
#     """
#     Exception raised when an operation is forbidden in the current state.
# 
#     """
# 
# Path: server/websockets/protocol.py
# CONNECTING, OPEN, CLOSING, CLOSED = range(4)
# class WebSocketCommonProtocol(asyncio.StreamReaderProtocol):
#     def __init__(self, *,
#                  host=None, port=None, secure=None,
#                  timeout=10, max_size=2 ** 20, max_queue=2 ** 5,
#                  loop=None, legacy_recv=False):
#     def local_address(self):
#     def remote_address(self):
#     def open(self):
#     def state_name(self):
#     def close(self, code=1000, reason=''):
#     def recv(self):
#     def send(self, data):
#     def ping(self, data=None):
#     def pong(self, data=b''):
#     def encode_data(self, data):
#     def ensure_open(self):
#     def run(self):
#     def read_message(self):
#                 def append(frame):
#                 def append(frame):
#                 def append(frame):
#                 def append(frame):
#     def read_data_frame(self, max_size):
#     def read_frame(self, max_size):
#     def write_frame(self, opcode, data=b''):
#     def close_connection(self, force=False):
#     def fail_connection(self, code=1011, reason=''):
#     def client_connected(self, reader, writer):
#     def eof_received(self):
#     def connection_lost(self, exc):


import asyncio
import contextlib
import functools
import os
import time
import unittest
import unittest.mock
from .compatibility import asyncio_ensure_future
from .exceptions import ConnectionClosed, InvalidState
from .framing import *
from .protocol import CLOSED, CONNECTING, WebSocketCommonProtocol
        with self.assertRaises(ConnectionClosed):
            self.loop.run_until_complete(self.protocol.recv())

        # The closing handshake was completed by fail_connection.
        self.assertConnectionClosed(1011, '')
        self.assertOneFrameSent(*self.remote_close)

    def test_local_close_during_recv(self):
        recv = self.async(self.protocol.recv())

        self.receive_frame(self.close_frame)
        self.receive_eof_if_client()

        self.loop.run_until_complete(self.protocol.close(reason='close'))

        with self.assertRaises(ConnectionClosed):
            self.loop.run_until_complete(recv)

        self.assertConnectionClosed(1000, 'close')

    # There is no test_remote_close_during_recv because it would be identical
    # to test_remote_close.

    def test_remote_close_during_send(self):
        self.make_drain_slow()
        send = self.async(self.protocol.send('hello'))

        self.receive_frame(self.close_frame)
        self.receive_eof()


# Path: server/websockets/compatibility.py

# 
# Path: server/websockets/exceptions.py
# class ConnectionClosed(InvalidState):
#     """
#     Exception raised when trying to read or write on a closed connection.
# 
#     Provides the connection close code and reason in its ``code`` and
#     ``reason`` attributes respectively.
# 
#     """
#     def __init__(self, code, reason):
#         self.code = code
#         self.reason = reason
#         message = 'WebSocket connection is closed: '
#         message += 'code = {}, '.format(code) if code else 'no code, '
#         message += 'reason = {}.'.format(reason) if reason else 'no reason.'
#         super().__init__(message)
# 
# class InvalidState(Exception):
#     """
#     Exception raised when an operation is forbidden in the current state.
# 
#     """
# 
# Path: server/websockets/protocol.py
# CONNECTING, OPEN, CLOSING, CLOSED = range(4)
# class WebSocketCommonProtocol(asyncio.StreamReaderProtocol):
#     def __init__(self, *,
#                  host=None, port=None, secure=None,
#                  timeout=10, max_size=2 ** 20, max_queue=2 ** 5,
#                  loop=None, legacy_recv=False):
#     def local_address(self):
#     def remote_address(self):
#     def open(self):
#     def state_name(self):
#     def close(self, code=1000, reason=''):
#     def recv(self):
#     def send(self, data):
#     def ping(self, data=None):
#     def pong(self, data=b''):
#     def encode_data(self, data):
#     def ensure_open(self):
#     def run(self):
#     def read_message(self):
#                 def append(frame):
#                 def append(frame):
#                 def append(frame):
#                 def append(frame):
#     def read_data_frame(self, max_size):
#     def read_frame(self, max_size):
#     def write_frame(self, opcode, data=b''):
#     def close_connection(self, force=False):
#     def fail_connection(self, code=1011, reason=''):
#     def client_connected(self, reader, writer):
#     def eof_received(self):
#     def connection_lost(self, exc):


# Path: server/websockets/test_protocol.py
import asyncio
import contextlib
import functools
import os
import time
import unittest
import unittest.mock
from .compatibility import asyncio_ensure_future
from .exceptions import ConnectionClosed, InvalidState
from .framing import *
from .protocol import CLOSED, CONNECTING, WebSocketCommonProtocol

        with self.assertRaises(ConnectionClosed):
            self.loop.run_until_complete(self.protocol.recv())

        # The closing handshake was completed by fail_connection.
        self.assertConnectionClosed(1011, '')
        self.assertOneFrameSent(*self.remote_close)

    def test_local_close_during_recv(self):
        recv = self.async(self.protocol.recv())

        self.receive_frame(self.close_frame)
        self.receive_eof_if_client()

        self.loop.run_until_complete(self.protocol.close(reason='close'))

        with self.assertRaises(ConnectionClosed):
            self.loop.run_until_complete(recv)

        self.assertConnectionClosed(1000, 'close')

    # There is no test_remote_close_during_recv because it would be identical
    # to test_remote_close.

    def test_remote_close_during_send(self):
        self.make_drain_slow()
        send = self.async(self.protocol.send('hello'))

        self.receive_frame(self.close_frame)
        self.receive_eof()


        with self.assertRaises(ConnectionClosed):