Skip to content

Commit

Permalink
Removed dependency on unittest.TestCase base class
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Nov 24, 2024
1 parent 46c6a70 commit abf336e
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 79 deletions.
40 changes: 20 additions & 20 deletions tests/async/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import threading
import time
from unittest import mock
import unittest
import pytest
try:
from engineio.async_socket import AsyncSocket as EngineIOSocket
Expand Down Expand Up @@ -38,13 +37,13 @@ def connect(sid, environ, auth):
pass

async def shutdown():
await instrumented_server.shutdown()
await self.isvr.shutdown()
await sio.shutdown()

if 'server_stats_interval' not in ikwargs:
ikwargs['server_stats_interval'] = 0.25

instrumented_server = sio.instrument(auth=auth, **ikwargs)
self.isvr = sio.instrument(auth=auth, **ikwargs)
server = SocketIOWebServer(sio, on_shutdown=shutdown)
server.start()

Expand All @@ -56,10 +55,11 @@ async def shutdown():
EngineIOSocket.schedule_ping = mock.MagicMock()

try:
ret = f(self, instrumented_server, *args, **kwargs)
ret = f(self, *args, **kwargs)
finally:
server.stop()
instrumented_server.uninstrument()
self.isvr.uninstrument()
self.isvr = None

EngineIOSocket.schedule_ping = original_schedule_ping

Expand All @@ -80,12 +80,12 @@ async def _async_custom_auth(auth):
return auth == {'foo': 'bar'}


class TestAsyncAdmin(unittest.TestCase):
def setUp(self):
class TestAsyncAdmin:
def setup_method(self):
print('threads at start:', threading.enumerate())
self.thread_count = threading.active_count()

def tearDown(self):
def teardown_method(self):
print('threads at end:', threading.enumerate())
assert self.thread_count == threading.active_count()

Expand All @@ -107,15 +107,15 @@ def test_missing_auth(self):
sio.instrument()

@with_instrumented_server(auth=False)
def test_admin_connect_with_no_auth(self, isvr):
def test_admin_connect_with_no_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin')
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})

@with_instrumented_server(auth={'foo': 'bar'})
def test_admin_connect_with_dict_auth(self, isvr):
def test_admin_connect_with_dict_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})
Expand All @@ -131,7 +131,7 @@ def test_admin_connect_with_dict_auth(self, isvr):

@with_instrumented_server(auth=[{'foo': 'bar'},
{'u': 'admin', 'p': 'secret'}])
def test_admin_connect_with_list_auth(self, isvr):
def test_admin_connect_with_list_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})
Expand All @@ -148,7 +148,7 @@ def test_admin_connect_with_list_auth(self, isvr):
namespace='/admin')

@with_instrumented_server(auth=_custom_auth)
def test_admin_connect_with_function_auth(self, isvr):
def test_admin_connect_with_function_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})
Expand All @@ -162,7 +162,7 @@ def test_admin_connect_with_function_auth(self, isvr):
namespace='/admin')

@with_instrumented_server(auth=_async_custom_auth)
def test_admin_connect_with_async_function_auth(self, isvr):
def test_admin_connect_with_async_function_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})
Expand All @@ -176,7 +176,7 @@ def test_admin_connect_with_async_function_auth(self, isvr):
namespace='/admin')

@with_instrumented_server()
def test_admin_connect_only_admin(self, isvr):
def test_admin_connect_only_admin(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin')
sid = admin_client.sid
Expand All @@ -201,7 +201,7 @@ def test_admin_connect_only_admin(self, isvr):
events['server_stats']['namespaces']

@with_instrumented_server()
def test_admin_connect_with_others(self, isvr):
def test_admin_connect_with_others(self):
with socketio.SimpleClient() as client1, \
socketio.SimpleClient() as client2, \
socketio.SimpleClient() as client3, \
Expand All @@ -210,12 +210,12 @@ def test_admin_connect_with_others(self, isvr):
client1.emit('enter_room', 'room')
sid1 = client1.sid

saved_check_for_upgrade = isvr._check_for_upgrade
isvr._check_for_upgrade = AsyncMock()
saved_check_for_upgrade = self.isvr._check_for_upgrade
self.isvr._check_for_upgrade = AsyncMock()
client2.connect('http://localhost:8900', namespace='/foo',
transports=['polling'])
sid2 = client2.sid
isvr._check_for_upgrade = saved_check_for_upgrade
self.isvr._check_for_upgrade = saved_check_for_upgrade

client3.connect('http://localhost:8900', namespace='/admin')
sid3 = client3.sid
Expand Down Expand Up @@ -251,7 +251,7 @@ def test_admin_connect_with_others(self, isvr):
assert socket['rooms'] == [sid3]

@with_instrumented_server(mode='production', read_only=True)
def test_admin_connect_production(self, isvr):
def test_admin_connect_production(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin')
events = self._expect({'config': 1, 'server_stats': 2},
Expand All @@ -272,7 +272,7 @@ def test_admin_connect_production(self, isvr):
events['server_stats']['namespaces']

@with_instrumented_server()
def test_admin_features(self, isvr):
def test_admin_features(self):
with socketio.SimpleClient() as client1, \
socketio.SimpleClient() as client2, \
socketio.SimpleClient() as admin_client:
Expand Down
3 changes: 1 addition & 2 deletions tests/async/test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import unittest
from unittest import mock

import pytest
Expand All @@ -12,7 +11,7 @@
from .helpers import AsyncMock, _run


class TestAsyncClient(unittest.TestCase):
class TestAsyncClient:
def test_is_asyncio_based(self):
c = async_client.AsyncClient()
assert c.is_asyncio_based()
Expand Down
5 changes: 2 additions & 3 deletions tests/async/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import unittest
from unittest import mock

from socketio import async_manager
from socketio import packet
from .helpers import AsyncMock, _run


class TestAsyncManager(unittest.TestCase):
def setUp(self):
class TestAsyncManager:
def setup_method(self):
id = 0

def generate_id():
Expand Down
5 changes: 1 addition & 4 deletions tests/async/test_namespace.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import sys
import unittest
from unittest import mock

from socketio import async_namespace
from .helpers import AsyncMock, _run


@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')
class TestAsyncNamespace(unittest.TestCase):
class TestAsyncNamespace:
def test_connect_event(self):
result = {}

Expand Down
5 changes: 2 additions & 3 deletions tests/async/test_pubsub_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import functools
import unittest
from unittest import mock

import pytest
Expand All @@ -11,8 +10,8 @@
from .helpers import AsyncMock, _run


class TestAsyncPubSubManager(unittest.TestCase):
def setUp(self):
class TestAsyncPubSubManager:
def setup_method(self):
id = 0

def generate_id():
Expand Down
5 changes: 2 additions & 3 deletions tests/async/test_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import logging
import unittest
from unittest import mock

from engineio import json
Expand All @@ -18,8 +17,8 @@
@mock.patch('socketio.server.engineio.AsyncServer', **{
'return_value.generate_id.side_effect': [str(i) for i in range(1, 10)],
'return_value.send_packet': AsyncMock()})
class TestAsyncServer(unittest.TestCase):
def tearDown(self):
class TestAsyncServer:
def teardown_method(self):
# restore JSON encoder, in case a test changed it
packet.Packet.json = json

Expand Down
3 changes: 1 addition & 2 deletions tests/async/test_simple_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import unittest
from unittest import mock
import pytest

Expand All @@ -8,7 +7,7 @@
from .helpers import AsyncMock, _run


class TestAsyncAsyncSimpleClient(unittest.TestCase):
class TestAsyncAsyncSimpleClient:
def test_constructor(self):
client = AsyncSimpleClient(1, '2', a='3', b=4)
assert client.client_args == (1, '2')
Expand Down
38 changes: 19 additions & 19 deletions tests/common/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import threading
import time
from unittest import mock
import unittest
import pytest
from engineio.socket import Socket as EngineIOSocket
import socketio
Expand Down Expand Up @@ -36,7 +35,7 @@ def connect(sid, environ, auth):
if 'server_stats_interval' not in ikwargs:
ikwargs['server_stats_interval'] = 0.25

instrumented_server = sio.instrument(auth=auth, **ikwargs)
self.isvr = sio.instrument(auth=auth, **ikwargs)
server = SocketIOWebServer(sio)
server.start()

Expand All @@ -48,11 +47,12 @@ def connect(sid, environ, auth):
EngineIOSocket.schedule_ping = mock.MagicMock()

try:
ret = f(self, instrumented_server, *args, **kwargs)
ret = f(self, *args, **kwargs)
finally:
server.stop()
instrumented_server.shutdown()
instrumented_server.uninstrument()
self.isvr.shutdown()
self.isvr.uninstrument()
self.isvr = None

EngineIOSocket.schedule_ping = original_schedule_ping

Expand All @@ -69,12 +69,12 @@ def _custom_auth(auth):
return auth == {'foo': 'bar'}


class TestAdmin(unittest.TestCase):
def setUp(self):
class TestAdmin:
def setup_method(self):
print('threads at start:', threading.enumerate())
self.thread_count = threading.active_count()

def tearDown(self):
def teardown_method(self):
print('threads at end:', threading.enumerate())
assert self.thread_count == threading.active_count()

Expand All @@ -96,15 +96,15 @@ def test_missing_auth(self):
sio.instrument()

@with_instrumented_server(auth=False)
def test_admin_connect_with_no_auth(self, isvr):
def test_admin_connect_with_no_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin')
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})

@with_instrumented_server(auth={'foo': 'bar'})
def test_admin_connect_with_dict_auth(self, isvr):
def test_admin_connect_with_dict_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})
Expand All @@ -120,7 +120,7 @@ def test_admin_connect_with_dict_auth(self, isvr):

@with_instrumented_server(auth=[{'foo': 'bar'},
{'u': 'admin', 'p': 'secret'}])
def test_admin_connect_with_list_auth(self, isvr):
def test_admin_connect_with_list_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})
Expand All @@ -137,7 +137,7 @@ def test_admin_connect_with_list_auth(self, isvr):
namespace='/admin')

@with_instrumented_server(auth=_custom_auth)
def test_admin_connect_with_function_auth(self, isvr):
def test_admin_connect_with_function_auth(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin',
auth={'foo': 'bar'})
Expand All @@ -151,7 +151,7 @@ def test_admin_connect_with_function_auth(self, isvr):
namespace='/admin')

@with_instrumented_server()
def test_admin_connect_only_admin(self, isvr):
def test_admin_connect_only_admin(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin')
sid = admin_client.sid
Expand All @@ -176,7 +176,7 @@ def test_admin_connect_only_admin(self, isvr):
events['server_stats']['namespaces']

@with_instrumented_server()
def test_admin_connect_with_others(self, isvr):
def test_admin_connect_with_others(self):
with socketio.SimpleClient() as client1, \
socketio.SimpleClient() as client2, \
socketio.SimpleClient() as client3, \
Expand All @@ -185,12 +185,12 @@ def test_admin_connect_with_others(self, isvr):
client1.emit('enter_room', 'room')
sid1 = client1.sid

saved_check_for_upgrade = isvr._check_for_upgrade
isvr._check_for_upgrade = mock.MagicMock()
saved_check_for_upgrade = self.isvr._check_for_upgrade
self.isvr._check_for_upgrade = mock.MagicMock()
client2.connect('http://localhost:8900', namespace='/foo',
transports=['polling'])
sid2 = client2.sid
isvr._check_for_upgrade = saved_check_for_upgrade
self.isvr._check_for_upgrade = saved_check_for_upgrade

client3.connect('http://localhost:8900', namespace='/admin')
sid3 = client3.sid
Expand Down Expand Up @@ -226,7 +226,7 @@ def test_admin_connect_with_others(self, isvr):
assert socket['rooms'] == [sid3]

@with_instrumented_server(mode='production', read_only=True)
def test_admin_connect_production(self, isvr):
def test_admin_connect_production(self):
with socketio.SimpleClient() as admin_client:
admin_client.connect('http://localhost:8900', namespace='/admin')
events = self._expect({'config': 1, 'server_stats': 2},
Expand All @@ -247,7 +247,7 @@ def test_admin_connect_production(self, isvr):
events['server_stats']['namespaces']

@with_instrumented_server()
def test_admin_features(self, isvr):
def test_admin_features(self):
with socketio.SimpleClient() as client1, \
socketio.SimpleClient() as client2, \
socketio.SimpleClient() as admin_client:
Expand Down
Loading

0 comments on commit abf336e

Please sign in to comment.