Skip to content

Commit

Permalink
merge master -Dorg -Ssuccess-only: PR 290 (Assert connection decorato…
Browse files Browse the repository at this point in the history
…r, see ome#289)
  • Loading branch information
snoopycrimecop committed Jun 5, 2021
2 parents b74356f + 1abfc2c commit dc2d85b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from past.utils import old_div
from builtins import object
import os

from functools import wraps
import warnings
from collections import defaultdict

Expand Down Expand Up @@ -1505,6 +1505,23 @@ def values(self):
return ()


def assertConnected(func):
"""decorator that raises an exception if the decorated function is used when the gateway
is not connected.
Assumes the first argument is a BlitzGateway instance, or the function
is a BlitzGateway method
"""
@wraps(func)
def wrapped(conn, *args, **kwargs):
if not conn._connected:
raise Ice.ConnectionLostException("Trying to call %s while not connected" % func.__name__)

return func(conn, *args, **kwargs)
return wrapped


class _BlitzGateway (object):
"""
Connection wrapper. Handles connecting and keeping the session alive,
Expand Down Expand Up @@ -2308,12 +2325,11 @@ def isConnected(self):
:return: Boolean
"""

return self._connected

########################
# # Connection Stuff # #

@assertConnected
def getEventContext(self):
"""
Returns omero_System_ice.EventContext.
Expand Down
27 changes: 26 additions & 1 deletion test/unit/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import sys

from omero.gateway import BlitzGateway, ImageWrapper, \
WellWrapper, LogicalChannelWrapper, OriginalFileWrapper
WellWrapper, LogicalChannelWrapper, OriginalFileWrapper, assertConnected
from omero.model import ImageI, PixelsI, ExperimenterI, EventI, \
ProjectI, TagAnnotationI, FileAnnotationI, OriginalFileI, \
MapAnnotationI, NamedValue, PlateI, WellI, \
Expand All @@ -40,6 +40,31 @@
from omero.rtypes import rstring, rtime, rlong, rint, rdouble


def test_assertConnected():

class MockGateway:
_connected = False

def connect(self):
self._connected = True
return True

def isConnected(self):
return self._connected

@assertConnected
def foo(self):
return 1

conn = MockGateway()
assert not conn.isConnected()
with pytest.raises(Ice.ConnectionLostException):
conn.foo()

assert conn.connect()
assert conn.foo()


class MockQueryService(object):

def __init__(self, obj_to_be_returned):
Expand Down

0 comments on commit dc2d85b

Please sign in to comment.