Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ on:
branches:
- main
tags:
- '*'
- "*"
pull_request:

jobs:

lint:
uses: ./.github/workflows/_tox.yml
with:
Expand All @@ -19,7 +18,7 @@ jobs:
strategy:
matrix:
runs-on: ["ubuntu-latest"] # can add windows-latest, macos-latest
python-version: ["3.11", "3.12", "3.13", "3.14"]
python-version: ["3.11", "3.12", "3.13"]
fail-fast: false
uses: ./.github/workflows/_test.yml
with:
Expand Down Expand Up @@ -47,7 +46,7 @@ jobs:

dist:
uses: ./.github/workflows/_dist.yml

release:
needs: [dist, test]
if: github.ref_type == 'tag'
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
]
description = "EPICS driver for the OceanOptics flame mini spectrometer"
dependencies = [
Expand Down
70 changes: 46 additions & 24 deletions tests/spectrometer_telecommunicator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,25 @@ async def spec_tel_coroutine():
try:
yield spec_tel_object
finally:
if spec_tel_object.socket_obj is not None:
spec_tel_object.socket_obj.close()
await close_non_blocking_socket(spec_tel_object.socket_obj)


async def close_non_blocking_socket(socket_obj: socket | None):
"""
Makes sure non blocking sockets close properly

Improperly closed sockets can lead to future tests failing as the port they tried
to bind to is already in use (by the previous, finished test)
"""
if socket_obj is None:
return
socket_obj.close()
# This method is quite crude but it seems to have a high success rate
# Ideally you would run recv from the socket until a b'' is recieved
# This would also require a timeout incase nothing is ever recieved
# And im not sure what you would even do in this case when you already
# tried to close it??
await asyncio.sleep(0.5)


@pytest.mark.asyncio
Expand All @@ -66,11 +83,13 @@ async def test_bad_socket():

spec_tel_object = SpecTel("127.0.0.1", 7016)

with pytest.raises(ConnectionRefusedError):
await spec_tel_object.connect()

if spec_tel_object.socket_obj is not None:
spec_tel_object.socket_obj.close()
try:
# Can raise either of these exceptions??
# Doesnt seem to act deterministically
with pytest.raises((ConnectionRefusedError, ConnectionResetError)):
await spec_tel_object.connect()
finally:
await close_non_blocking_socket(spec_tel_object.socket_obj)


@pytest.mark.asyncio
Expand All @@ -88,25 +107,28 @@ async def test_bad_initial_connection_message():
await asyncio.sleep(1)

spec_tel_object = SpecTel("127.0.0.1", 7016)
with pytest.raises(UnexpectedResponseError):
await spec_tel_object.connect()
if spec_tel_object.socket_obj is not None:
spec_tel_object.socket_obj.close()
try:
with pytest.raises(UnexpectedResponseError):
await spec_tel_object.connect()
finally:
await close_non_blocking_socket(spec_tel_object.socket_obj)


@pytest.mark.asyncio
async def test_device_already_connected():
async with spec_tel_coroutine() as spec_tel_object:
socket_obj = socket()
socket_obj.connect(("127.0.0.1", 7016))
socket_obj.setblocking(False)
try:
socket_obj.connect(("127.0.0.1", 7016))
socket_obj.setblocking(False)

loop = asyncio.get_event_loop()
await loop.sock_recv(socket_obj, 1024)
loop = asyncio.get_event_loop()
await loop.sock_recv(socket_obj, 1024)

with pytest.raises(TimeoutError):
await spec_tel_object.connect()
socket_obj.close()
with pytest.raises(TimeoutError):
await spec_tel_object.connect()
finally:
await close_non_blocking_socket(socket_obj)


@pytest.mark.asyncio
Expand All @@ -131,13 +153,13 @@ async def test_invalid_response():
await asyncio.sleep(1)

spec_tel_object = SpecTel("127.0.0.1", 7016)
await spec_tel_object.connect()

with pytest.raises(UnexpectedResponseError):
await spec_tel_object.get_version()
try:
await spec_tel_object.connect()

if spec_tel_object.socket_obj is not None:
spec_tel_object.socket_obj.close()
with pytest.raises(UnexpectedResponseError):
await spec_tel_object.get_version()
finally:
await close_non_blocking_socket(spec_tel_object.socket_obj)


@pytest.mark.asyncio
Expand Down
Loading