BioImager  3.9.1
A .NET microscopy imaging library. Supports various microscopes by using imported libraries & GUI automation. Supported libraries include PriorĀ® & ZeissĀ® & all devices supported by Micromanager 2.0 and python-microscope.
Loading...
Searching...
No Matches
test_client.py
1#!/usr/bin/env python3
2
3## Copyright (C) 2020 David Miguel Susano Pinto <carandraug@gmail.com>
4##
5## This file is part of Microscope.
6##
7## Microscope is free software: you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation, either version 3 of the License, or
10## (at your option) any later version.
11##
12## Microscope 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
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with Microscope. If not, see <http://www.gnu.org/licenses/>.
19
20import threading
21import unittest
22
23import Pyro4
24
26import microscope.testsuite.devices as dummies
27
28
29@Pyro4.expose
31 """Simple class to test serving via Pyro.
32
33 We can use one of our own test devices but the idea is to have
34 this tests independent from the devices. We should be able to
35 test the Client with any Python object and weird cases, even if we
36 don't yet make use of them in the devices.
37 """
38
39 def __init__(self):
40 self._value = 42 # not exposed
41
42 @property
43 def attr(self): # exposed as 'proxy.attr' remote attribute
44 return self._value
45
46 @attr.setter
47 def attr(self, value): # exposed as 'proxy.attr' writable
48 self._value = value
49
50
51@Pyro4.expose
52class ExposedDeformableMirror(dummies.TestDeformableMirror):
53 """
54 Microscope device server is configure to not require @expose but
55 this is to test our client with Pyro4's own Daemon. We need to
56 subclass and have the passthrough because the property comes from
57 the Abstract Base class, not the TestDeformableMirror class.
58 """
59
60 @property
61 def n_actuators(self):
62 return super().n_actuators
63
64
65class TestClient(unittest.TestCase):
66 def setUp(self):
67 self.daemon = Pyro4.Daemon()
68 self.thread = threading.Thread(target=self.daemon.requestLoop)
69
70 def tearDown(self):
71 self.daemon.shutdown()
72 self.thread.join()
73
74 def _serve_objs(self, objs):
75 uris = [self.daemon.register(obj) for obj in objs]
76 self.thread.start()
77 clients = [microscope.clients.Client(uri) for uri in uris]
78 return clients
79
81 """Test we can read properties via the Client"""
82 # list of (object-to-serve, property-name-to-test)
83 objs2prop = [
84 (PyroService(), "attr"),
85 (ExposedDeformableMirror(10), "n_actuators"),
86 ]
87 clients = self._serve_objs([x[0] for x in objs2prop])
88 for client, obj_prop in zip(clients, objs2prop):
89 obj = obj_prop[0]
90 name = obj_prop[1]
91 self.assertTrue(getattr(client, name), getattr(obj, name))
92
94 """Test we can write properties via the Client"""
95 obj = PyroService()
96 client = (self._serve_objs([obj]))[0]
97 self.assertTrue(client.attr, 42)
98 client.attr = 10
99 self.assertTrue(client.attr, 10)
100 self.assertTrue(obj.attr, 10)
101
102
103if __name__ == "__main__":
104 unittest.main()