Skip to content

Commit

Permalink
Test for pull request #304 (Added try-catch to notification close in …
Browse files Browse the repository at this point in the history
…symbol destructor) (#369)

* Added try-catch to notification close in symbol destructor

* Add test for try-catch to notification close in symbol destructor

* Update changelog

* More elegant way to test for error not raised.

Use self.fail() now instead of self.assert().
Thanks to @RobtertoRoos for pointing it out.

---------

Co-authored-by: Roberto-R <robert.soor@gmail.com>
Co-authored-by: chrisbeardy <20585410+chrisbeardy@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 30, 2024
1 parent 39e9c58 commit 3e08041
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* [#293](https://github.com/stlehmann/pyads/pull/2939) Support WSTRINGS in structures

### Changed
* [#369](https://github.com/stlehmann/pyads/pull/304) Add test for [#304](https://github.com/stlehmann/pyads/pull/304) in `tests/test_testserver.py`
* [#304](https://github.com/stlehmann/pyads/pull/304) Implemented try-catch when closing ADS notifications in AdsSymbol destructor
* [#292](https://github.com/stlehmann/pyads/pull/292) Improve performance of get_value_from_ctype_data for arrays
* [#363](https://github.com/stlehmann/pyads/pull/363) Allow for platform independent builds
Expand Down
36 changes: 36 additions & 0 deletions tests/test_testserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ def test_context(self):

time.sleep(0.1) # Give server a moment to spin down

def test_server_disconnect_then_del_device_notification(self):
"""Test no error thown, when ADS symbol with device_notification is cleaned up after the server went offline.
Tests fix of issue [#303](https://github.com/stlehmann/pyads/issues/303), original pull request: [#304](https://github.com/stlehmann/pyads/pull/304)
"""
# 1. spin up the server
handler = BasicHandler()
test_server = AdsTestServer(handler=handler, logging=False)
test_server.start()
time.sleep(0.1) # Give server a moment to spin up

# 2. open a plc connection to the test server:
plc = pyads.Connection(TEST_SERVER_AMS_NET_ID, TEST_SERVER_AMS_PORT)
plc.open()

# 3. add a variable, register a device notification with auto_update=True
test_int = pyads.AdsSymbol(plc, "TestSymbol", symbol_type=pyads.PLCTYPE_INT)
test_int.plc_type = pyads.PLCTYPE_INT
test_int.auto_update = True
time.sleep(0.1) # Give server a moment

raised_error: str = ""

# 4. stop the test server
test_server.stop()
time.sleep(0.1) # Give server a moment

try:
# some code, where test_int is cleared by the Garbage collector after the server was stopped
# (e.g. the machine with ADS Server disconnected)
# this raised an ADSError up to commit [a7af674](https://github.com/stlehmann/pyads/tree/a7af674b49b1c91966f2bac1f00f86273cbd9af8)
# `clear_device_notifications()` failed, if not wrapped in try-catch as the server is no longer present.
test_int.__del__()
except pyads.ADSError as e:
self.fail(f"Closing server connection raised: {e}")


if __name__ == "__main__":
unittest.main()

0 comments on commit 3e08041

Please sign in to comment.