Skip to content

Commit 2693b11

Browse files
author
Juan Tirado
committed
Revisiting tests.
1 parent eb73a48 commit 2693b11

7 files changed

Lines changed: 107 additions & 83 deletions

File tree

.github/workflows/test.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ jobs:
1616
- name: Check out code
1717
uses: actions/checkout@v3
1818
- name: Setup Python
19-
uses: actions/setup-python@v2
19+
uses: actions/setup-python@v4
2020
with:
2121
python-version: ${{ matrix.python }}
2222
- name: Install dependencies
2323
run: pip install tox
2424
- name: Run unit tests
25-
run: tox -e py3,serial
25+
run: tox -e py3
2626

2727

2828
integration:
@@ -43,7 +43,7 @@ jobs:
4343
provider: lxd
4444
juju-channel: 3.0/stable
4545
- name: Setup Python
46-
uses: actions/setup-python@v2
46+
uses: actions/setup-python@v4
4747
with:
4848
python-version: ${{ matrix.python }}
4949
- name: Install dependencies

juju/jasyncio.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ def _task_result_exp_handler(task, task_name=task_name, logger=logger):
8383
task.add_done_callback(functools.partial(_task_result_exp_handler, task_name=task_name, logger=logger))
8484
return task
8585

86+
class SingletonEventLoop(object):
87+
"""
88+
Single instance containing an event loop to be reused.
89+
"""
90+
loop = None
91+
def __new__(cls):
92+
if not hasattr(cls, 'instance'):
93+
cls.instance = super(SingletonEventLoop, cls).__new__(cls)
94+
cls.instance.loop = asyncio.new_event_loop()
95+
96+
return cls.instance
8697

8798
def run(*steps):
8899
"""
@@ -96,7 +107,8 @@ def run(*steps):
96107

97108
task = None
98109
run._sigint = False # function attr to allow setting from closure
99-
loop = asyncio.new_event_loop()
110+
# Use a singleton class to force a single event loop instance
111+
loop = SingletonEventLoop().loop
100112

101113
def abort():
102114
task.cancel()

tests/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
from juju.client.jujudata import FileJujuData
1111
from juju.controller import Controller
1212

13+
from juju.jasyncio import SingletonEventLoop
14+
15+
@pytest.fixture(scope="session")
16+
def event_loop():
17+
"""
18+
This fixture forces all the asyncio tests
19+
to use the same events loop
20+
"""
21+
loop = SingletonEventLoop().loop
22+
yield loop
23+
loop.close()
24+
1325

1426
def is_bootstrapped():
1527
try:

tests/integration/test_application.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@ async def test_action(event_loop):
5656
@pytest.mark.asyncio
5757
async def test_get_set_config(event_loop):
5858
async with base.CleanModel() as model:
59-
ubuntu_app = await model.deploy(
60-
'percona-cluster',
61-
application_name='mysql',
62-
series='xenial',
59+
app = await model.deploy(
60+
'hello-juju',
61+
application_name='hello-juju',
62+
series='focal',
6363
channel='stable',
6464
config={
65-
'tuning-level': 'safest',
65+
'application-repo': 'http://my-juju.com',
6666
},
6767
constraints={
6868
'arch': 'amd64',
6969
'mem': 256 * MB,
7070
},
7171
)
7272

73-
config = await ubuntu_app.get_config()
74-
await ubuntu_app.set_config(config)
73+
config = await app.get_config()
74+
await app.set_config(config)
7575

76-
config2 = await ubuntu_app.get_config()
76+
config2 = await app.get_config()
7777
assert config == config2
7878

7979

@@ -83,9 +83,9 @@ async def test_get_set_config(event_loop):
8383
async def test_status_is_not_unset(event_loop):
8484
async with base.CleanModel() as model:
8585
app = await model.deploy(
86-
'cs:ubuntu-0',
86+
'ubuntu-0',
8787
application_name='ubuntu',
88-
series='trusty',
88+
series='jammy',
8989
channel='stable',
9090
)
9191

@@ -143,10 +143,10 @@ async def test_deploy_charmstore_charm(event_loop):
143143
@pytest.mark.asyncio
144144
async def test_deploy_charmhub_charm(event_loop):
145145
async with base.CleanModel() as model:
146-
app = await model.deploy('ch:hello-kubecon')
146+
app = await model.deploy('hello-juju')
147147
await model.block_until(lambda: (len(app.units) > 0 and
148148
app.units[0].machine))
149-
assert 'hello-kubecon' in app.data['charm-url']
149+
assert 'hello-juju' in app.data['charm-url']
150150

151151

152152
@base.bootstrapped

tests/integration/test_machine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
@base.bootstrapped
10-
@pytest.mark.serial
10+
@pytest.mark.asyncio
1111
@pytest.mark.skip('Update charm')
1212
async def test_status(event_loop):
1313
async with base.CleanModel() as model:
@@ -38,7 +38,7 @@ async def test_status(event_loop):
3838

3939

4040
@base.bootstrapped
41-
@pytest.mark.serial
41+
@pytest.mark.asyncio
4242
async def test_scp(event_loop):
4343
# ensure that asyncio.subprocess will work;
4444
try:

0 commit comments

Comments
 (0)