[DEV] add a timeout in the mock of karanage connection

This commit is contained in:
Edouard DUPIN 2023-02-07 21:44:09 +01:00
parent f95621f6bb
commit 03c8b0fc13

View File

@ -11,6 +11,7 @@ from abc import ABC, abstractmethod
import copy import copy
import enum import enum
import json import json
from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
from typing import ( from typing import (
Dict, Dict,
@ -18,7 +19,7 @@ from typing import (
NamedTuple, NamedTuple,
Optional, Optional,
) )
import time
import requests import requests
@ -240,15 +241,24 @@ class KaranageMock(KaranageConnectionInterface):
self.request.append(MockData("GET", service, url_offset, None, headers, params)) self.request.append(MockData("GET", service, url_offset, None, headers, params))
return KaranageResponse(f"{service}/{url_offset}", 200, "{}") 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 """get the list of last added values
:param time_out: Timeout before exiting in error
:returns: all collected values. :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 out = self.request
self.request = [] self.request = []
return out return out
def clear_values(self) -> None: def clear_values(self) -> None:
"""Clear all the received data.""" """Clear all the received data."""
self.request = [] self.request = []