Commit 67a0eba7 authored by Jibril Mammeri's avatar Jibril Mammeri
Browse files

Remove duplicates of _send_limit, define it in controllers.regulator, add test

parent 73248ecc
Pipeline #35166 failed with stages
in 4 minutes and 45 seconds
......@@ -70,7 +70,7 @@ import sys
from bliss import global_map
from bliss.comm.util import get_comm
from bliss.common.logtools import log_info, log_debug, log_debug_data, log_warning
from bliss.controllers.regulator import Controller
from bliss.controllers.regulator import send_limit
# --- patch the Input, Output and Loop classes
from bliss.controllers.regulation.temperature.lakeshore.lakeshore import ( # noqa: F401
......@@ -83,28 +83,6 @@ from bliss.controllers.regulation.temperature.lakeshore.lakeshore import ( # no
LakeshoreLoop as Loop
)
_last_call = time.time()
def _send_limit(func):
"""
limit number of commands per second.
lakeshore 331 supports at most 20 commands per second
"""
def f(*args, **kwargs):
global _last_call
delta_t = time.time() - _last_call
if delta_t <= 0.15:
time.sleep(0.15 - delta_t)
try:
return func(*args, **kwargs)
finally:
_last_call = time.time()
return f
class LakeShore331(Controller):
......@@ -543,7 +521,7 @@ class LakeShore331(Controller):
# ----- controller specific methods -----------------
@_send_limit
@send_limit
def send_cmd(self, command, *args, channel=None):
""" Send a command to the controller
Args:
......
......@@ -84,29 +84,6 @@ from bliss.controllers.regulation.temperature.lakeshore.lakeshore import ( # no
)
_last_call = time.time()
def _send_limit(func):
"""
Limit number of commands per second
lakeshore 332 supports at most 20 commands per second
"""
def f(*args, **kwargs):
global _last_call
delta_t = time.time() - _last_call
if delta_t <= 0.15:
time.sleep(0.15 - delta_t)
try:
return func(*args, **kwargs)
finally:
_last_call = time.time()
return f
class LakeShore332(LakeShore331):
@enum.unique
class SensorTypes(enum.IntEnum):
......
......@@ -88,29 +88,6 @@ from bliss.controllers.regulation.temperature.lakeshore.lakeshore import ( # no
)
_last_call = time.time()
def _send_limit(func):
"""
Limit number of commands per second
lakeshore 335 supports at most 20 commands per second
"""
def f(*args, **kwargs):
global _last_call
delta_t = time.time() - _last_call
if delta_t <= 0.15:
time.sleep(0.15 - delta_t)
try:
return func(*args, **kwargs)
finally:
_last_call = time.time()
return f
class Input(LakeshoreInput):
@lazy_init
def set_sensor_type(
......
......@@ -91,28 +91,6 @@ from bliss.controllers.regulation.temperature.lakeshore.lakeshore import ( # no
)
_last_call = time.time()
def _send_limit(func):
"""
Limit number of commands per second
lakeshore 336 supports at most 20 commands per second
"""
def f(*args, **kwargs):
global _last_call
delta_t = time.time() - _last_call
if delta_t <= 0.15:
time.sleep(0.15 - delta_t)
try:
return func(*args, **kwargs)
finally:
_last_call = time.time()
return f
class LakeShore336(LakeShore335):
NUMINPUT = {1: "A", 2: "B", 3: "C", 4: "D"}
......
......@@ -85,29 +85,6 @@ from bliss.controllers.regulation.temperature.lakeshore.lakeshore import ( # no
)
_last_call = time.time()
def _send_limit(func):
"""
Limit number of commands per second
lakeshore 340 supports at most 20 commands per second
"""
def f(*args, **kwargs):
global _last_call
delta_t = time.time() - _last_call
if delta_t <= 0.15:
time.sleep(0.15 - delta_t)
try:
return func(*args, **kwargs)
finally:
_last_call = time.time()
return f
class Input(LakeshoreInput):
@lazy_init
def set_sensor_type(
......
......@@ -41,7 +41,7 @@ import enum
from bliss import global_map
from bliss.comm.util import get_comm
from bliss.common.logtools import log_info, log_debug
from bliss.controllers.regulator import Controller
from bliss.controllers.regulator import send_limit
# --- patch the Input, Output and Loop classes with their Linkam equivalent -----------
from bliss.controllers.regulation.temperature.linkam.linkam import ( # noqa: F401
......@@ -55,27 +55,6 @@ from bliss.controllers.regulation.temperature.linkam.linkam import ( # noqa: F4
)
_last_call = time.time()
def _send_limit(func):
"""
Limit number of commands per second
"""
def f(*args, **kwargs):
global _last_call
delta_t = time.time() - _last_call
if delta_t <= 0.30:
time.sleep(0.30 - delta_t)
try:
return func(*args, **kwargs)
finally:
_last_call = time.time()
return f
class LinkamTms94(Controller):
"""
Linkam TMS94 controller class
......@@ -483,7 +462,7 @@ class LinkamTms94(Controller):
# ----- controller specific methods --------------------------
@_send_limit
@send_limit
def send_cmd(self, command, arg=None):
""" Send a command to the controller
Args:
......
......@@ -57,6 +57,7 @@ from gevent import lock
from bliss.common.regulation import Input, Output, Loop
from bliss.common.utils import set_custom_members
from bliss.common.logtools import log_info
import time
class Controller:
......@@ -682,3 +683,26 @@ class Controller:
"""
log_info(self, "Controller:get_output_ramprate: %s" % (toutput))
raise NotImplementedError
def send_limit(func):
"""
Limit number of commands per second
lakeshore 331/340 supports at most 20 commands per second
"""
last_call = time.time()
def f(*args, **kwargs):
nonlocal last_call
delta_t = time.time() - last_call
if delta_t <= 0.15:
time.sleep(0.15 - delta_t)
try:
return func(*args, **kwargs)
finally:
last_call = time.time()
return f
import pytest
import time
from bliss.controllers.regulator import send_limit
@send_limit
def test_send_limit():
return 0
@send_limit
def test_send_limit_bis():
return 0
def test_command_per_second():
"""Test it's not possible to have more than 10 commands per second"""
start = time.time()
for i in range(10):
test_send_limit()
end = time.time()
total = end - start
assert total >= 1
def test_different_instance():
"""Test if the decorator uses its own _last_call"""
time.sleep(0.15)
start = time.time()
test_send_limit()
test_send_limit_bis()
end = time.time()
total = end - start
assert total < 0.15
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment