@@ -25,44 +25,75 @@ class BacnetOnAttributeUpdatesTestCase(BacnetBaseTestCase):
25
25
async def asyncSetUp (self ):
26
26
await super ().asyncSetUp ()
27
27
self .connector ._AsyncBACnetConnector__application = AsyncMock ()
28
- self ._loop_thread = Thread (target = self .connector .loop .run_forever , daemon = True )
29
- self ._loop_thread .start ()
30
28
31
29
async def asyncTearDown (self ):
32
- self .connector .loop .call_soon_threadsafe (self .connector .loop .stop )
33
- self ._loop_thread .join (timeout = 1 )
34
-
35
30
await super ().asyncTearDown ()
36
31
37
- async def test_retrieve_device_obj_by_name_success (self ):
38
- payload = {'device' : self .DEVICE_NAME , 'data' : {'binaryValue2' : True }}
39
- device = self .connector ._AsyncBACnetConnector__get_device_by_name (payload = payload )
32
+ async def test_retrieve_device_by_name_success (self ):
33
+ payload = {"device" : self .DEVICE_NAME , "data" : {"binaryValue2" : True }}
34
+
35
+ fake_task = MagicMock ()
36
+ fake_task .done .return_value = True
37
+ fake_task .result .return_value = self .device # success
38
+
39
+ with patch .object (self .connector .loop , "create_task" , return_value = fake_task ) as ct_mock :
40
+ device = self .connector ._AsyncBACnetConnector__get_device_by_name (payload = payload )
41
+
42
+ ct_mock .assert_called_once ()
40
43
self .assertIsInstance (device , Device )
41
44
self .assertIs (device , self .device )
42
45
self .assertEqual (device .name , self .DEVICE_NAME )
43
46
44
47
async def test_get_device_by_name_missing_device_key (self ):
45
- payload = {'data' : {'binaryValue2' : True }}
48
+ payload = {"data" : {"binaryValue2" : True }}
49
+ fake_task = MagicMock ()
50
+ fake_task .done .return_value = True
51
+ fake_task .result .return_value = None
46
52
47
- device = self .connector ._AsyncBACnetConnector__get_device_by_name (payload = payload )
53
+ with patch .object (self .connector .loop , "create_task" , return_value = fake_task ), \
54
+ self .assertLogs ("Bacnet test" , level = "ERROR" ) as logcap :
55
+ device = self .connector ._AsyncBACnetConnector__get_device_by_name (payload = payload )
48
56
49
57
self .assertIsNone (device )
58
+ self .assertTrue (any ("does not contain a device name" in m for m in logcap .output ))
50
59
51
- async def test_retrieve_device_obj_on_non_existent_device (self ):
52
- payload = {'device' : 'test emulator device22' , 'data' : {'binaryValue2' : True }}
53
- device = self .connector ._AsyncBACnetConnector__get_device_by_name (payload = payload )
60
+ async def test_retrieve_device_on_non_existent_device (self ):
61
+ payload = {"device" : "non-existent-device" , "data" : {"binaryValue2" : True }}
62
+
63
+ fake_task = MagicMock ()
64
+ fake_task .done .return_value = True
65
+ fake_task .result .return_value = None
66
+
67
+ with patch .object (self .connector .loop , "create_task" , return_value = fake_task ) as ct_mock :
68
+ device = self .connector ._AsyncBACnetConnector__get_device_by_name (payload = payload )
69
+
70
+ ct_mock .assert_called_once ()
54
71
self .assertIsNone (device )
55
72
56
73
async def test_get_device_by_name_timeout (self ):
57
- payload = {'device' : self .DEVICE_NAME , 'data' : {'binaryValue2' : True }}
58
- with patch ("asyncio.run_coroutine_threadsafe" ) as timeout_mock_task :
59
- fake_future = MagicMock ()
60
- fake_future .result .side_effect = TimeoutError
61
- fake_future .done .return_value = False
62
- timeout_mock_task .return_value = fake_future
74
+ payload = {"device" : self .DEVICE_NAME , "data" : {"binaryValue2" : True }}
75
+ fake_task = MagicMock ()
76
+ fake_task .done .return_value = False
77
+
78
+ with patch .object (self .connector .loop , "create_task" , return_value = fake_task ) as ct_mock , \
79
+ patch .object (self .connector , "_AsyncBACnetConnector__wait_task_with_timeout" ,
80
+ return_value = (False , None )) as waiter_mock , \
81
+ self .assertLogs ("Bacnet test" , level = "DEBUG" ) as logcap :
63
82
device = self .connector ._AsyncBACnetConnector__get_device_by_name (payload = payload )
83
+
84
+ ct_mock .assert_called_once ()
85
+ waiter_mock .assert_called_once ()
64
86
self .assertIsNone (device )
65
- fake_future .cancel .assert_called_once ()
87
+ self .assertTrue (any ("look up task failed on timeout" in m for m in logcap .output ))
88
+
89
+ async def test_update_attribute_update_with_device_not_found (self ):
90
+ self .connector ._AsyncBACnetConnector__get_device_by_name = MagicMock ()
91
+ self .connector ._AsyncBACnetConnector__get_device_by_name .return_value = None
92
+ payload = {'device' : 'test emulator device22' , 'data' : {'binaryValue2' : True }}
93
+ with patch .object (self .connector , "_AsyncBACnetConnector__create_task" ) as create_task_mock , \
94
+ self .assertLogs ("Bacnet test" , level = "ERROR" ) as logcap :
95
+ self .connector .on_attributes_update (payload )
96
+ create_task_mock .assert_not_called ()
66
97
67
98
async def test_update_when_device_has_no_mapping (self ):
68
99
payload = {"device" : self .device .name , "data" : {"binaryValue2" : True }}
@@ -71,6 +102,8 @@ async def test_update_when_device_has_no_mapping(self):
71
102
'attribute_updates/on_attribute_updates_bacnet_config_empty_section.json'
72
103
)
73
104
await self .connector ._AsyncBACnetConnector__devices .add (self .device )
105
+ self .connector ._AsyncBACnetConnector__get_device_by_name = MagicMock ()
106
+ self .connector ._AsyncBACnetConnector__get_device_by_name .return_value = self .device
74
107
75
108
with patch .object (self .connector , "_AsyncBACnetConnector__create_task" ) as create_task_mock , \
76
109
self .assertLogs ("Bacnet test" , level = "ERROR" ) as logcap :
@@ -86,6 +119,8 @@ async def test_update_incorrect_object_id_mixed(self):
86
119
"attribute_updates/on_attribute_updates_bacnet_config_incorrect_object_id.json"
87
120
)
88
121
await self .connector ._AsyncBACnetConnector__devices .add (self .device )
122
+ self .connector ._AsyncBACnetConnector__get_device_by_name = MagicMock ()
123
+ self .connector ._AsyncBACnetConnector__get_device_by_name .return_value = self .device
89
124
90
125
with patch .object (self .connector , "_AsyncBACnetConnector__create_task" ) as ct_mock , \
91
126
self .assertLogs ("Bacnet test" , level = "ERROR" ) as logcap :
@@ -109,6 +144,8 @@ async def test_update_multiple_correct_attribute(self):
109
144
"attribute_updates/on_attribute_updates_bacnet_connector_multiple_updates.json"
110
145
)
111
146
await self .connector ._AsyncBACnetConnector__devices .add (self .device )
147
+ self .connector ._AsyncBACnetConnector__get_device_by_name = MagicMock ()
148
+ self .connector ._AsyncBACnetConnector__get_device_by_name .return_value = self .device
112
149
113
150
with patch .object (self .connector , "_AsyncBACnetConnector__create_task" ) as ct_mock :
114
151
self .connector .on_attributes_update (content = payload )
@@ -122,6 +159,8 @@ async def test_update_attribute_update_with_incorrect_object_id_datatype(self):
122
159
"attribute_updates/on_attribute_updates_bacnet_config_invalid_data_type.json"
123
160
)
124
161
await self .connector ._AsyncBACnetConnector__devices .add (self .device )
162
+ self .connector ._AsyncBACnetConnector__get_device_by_name = MagicMock ()
163
+ self .connector ._AsyncBACnetConnector__get_device_by_name .return_value = self .device
125
164
126
165
with patch .object (self .connector , "_AsyncBACnetConnector__create_task" ) as ct_mock , \
127
166
self .assertLogs ("Bacnet test" , level = "ERROR" ) as logcap :
@@ -130,17 +169,18 @@ async def test_update_attribute_update_with_incorrect_object_id_datatype(self):
130
169
ct_mock .assert_not_called ()
131
170
self .assertTrue (any ("for key" in m for m in logcap .output ))
132
171
133
- async def test_update_continues_when_first_task_raises (self ):
172
+ async def test_update_continues_when_first_task_failed (self ):
134
173
payload = {"device" : self .device .name , "data" : {"binaryValue2" : True , "binaryInput1" : 56 }}
135
174
await self .connector ._AsyncBACnetConnector__devices .remove (self .device )
136
175
self .device = self .create_fake_device (
137
176
"attribute_updates/on_attribute_updates_bacnet_connector_multiple_updates.json"
138
177
)
139
178
await self .connector ._AsyncBACnetConnector__devices .add (self .device )
179
+ self .connector ._AsyncBACnetConnector__get_device_by_name = MagicMock ()
180
+ self .connector ._AsyncBACnetConnector__get_device_by_name .return_value = self .device
140
181
141
182
with patch .object (self .connector , "_AsyncBACnetConnector__create_task" ) as ct_mock , \
142
183
self .assertLogs ("Bacnet test" , level = "ERROR" ) as logcap :
143
184
ct_mock .side_effect = [Exception ("" ), None ]
144
185
self .connector .on_attributes_update (payload )
145
186
self .assertEqual (ct_mock .call_count , 2 )
146
-
0 commit comments