Skip to content

Commit 52ffa6a

Browse files
committed
test: Add default bus tests
Check that context vars work as expected.
1 parent 737d5d0 commit 52ffa6a

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test/test_default_bus.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# SPDX-License-Identifier: LGPL-2.1-or-later
2+
3+
# Copyright (C) 2025 igo95862
4+
5+
# This file is part of python-sdbus
6+
7+
# This library is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU Lesser General Public
9+
# License as published by the Free Software Foundation; either
10+
# version 2.1 of the License, or (at your option) any later version.
11+
12+
# This library is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
# Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public
18+
# License along with this library; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
from __future__ import annotations
21+
22+
from contextvars import copy_context
23+
from unittest import main
24+
25+
from sdbus.unittest import IsolatedDbusTestCase
26+
27+
from sdbus import get_default_bus, sd_bus_open_user, set_context_default_bus
28+
29+
30+
def return_bus_id() -> int:
31+
return id(get_default_bus())
32+
33+
34+
def set_context_and_return_id() -> int:
35+
set_context_default_bus(sd_bus_open_user())
36+
return id(get_default_bus())
37+
38+
39+
class TestDefaultBus(IsolatedDbusTestCase):
40+
def test_context_bus(self) -> None:
41+
bus_id = id(get_default_bus())
42+
43+
self.assertEqual(
44+
bus_id,
45+
copy_context().run(return_bus_id),
46+
)
47+
48+
self.assertNotEqual(
49+
bus_id,
50+
copy_context().run(set_context_and_return_id),
51+
)
52+
53+
self.assertEqual(
54+
bus_id,
55+
id(get_default_bus()),
56+
)
57+
58+
59+
if __name__ == "__main__":
60+
main()

0 commit comments

Comments
 (0)