From 03c8b0fc1339e84a589ae4795994a45358650c81 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 7 Feb 2023 21:44:09 +0100 Subject: [PATCH] [DEV] add a timeout in the mock of karanage connection --- client/python/karanage/karanage/connection.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/client/python/karanage/karanage/connection.py b/client/python/karanage/karanage/connection.py index 190ae49..23346ac 100644 --- a/client/python/karanage/karanage/connection.py +++ b/client/python/karanage/karanage/connection.py @@ -11,6 +11,7 @@ from abc import ABC, abstractmethod import copy import enum import json +from datetime import datetime, timedelta from pathlib import Path from typing import ( Dict, @@ -18,7 +19,7 @@ from typing import ( NamedTuple, Optional, ) - +import time import requests @@ -240,15 +241,24 @@ class KaranageMock(KaranageConnectionInterface): self.request.append(MockData("GET", service, url_offset, None, headers, params)) return KaranageResponse(f"{service}/{url_offset}", 200, "{}") - def get_values(self) -> List[KaranageResponse]: + def get_values(self, time_out: Optional[float] = None) -> List[KaranageResponse]: """get the list of last added values + :param time_out: Timeout before exiting in error :returns: all collected values. """ + if time_out is not None and time_out > 0: + start_time = datetime.now() + while len(self.request) == 0: + time.sleep(0.1) + now = datetime.now() + if now - start_time > timedelta(seconds=time_out): + return [] out = self.request self.request = [] return out + def clear_values(self) -> None: """Clear all the received data.""" self.request = []