diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-05 14:04:41 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-05 14:04:41 +0200 |
| commit | 6100010d6873845413aed22744470616992df603 (patch) | |
| tree | 5cefcd46851d06e0d9e9b09f578c13649582d075 /src/philo/philo.py | |
| parent | 8a351c6d267c55445de7b7b40b1de3b16305bd02 (diff) | |
| download | philosophers_test-6100010d6873845413aed22744470616992df603.tar.gz philosophers_test-6100010d6873845413aed22744470616992df603.tar.bz2 philosophers_test-6100010d6873845413aed22744470616992df603.zip | |
Readded should be dead test
Diffstat (limited to 'src/philo/philo.py')
| -rw-r--r-- | src/philo/philo.py | 68 |
1 files changed, 31 insertions, 37 deletions
diff --git a/src/philo/philo.py b/src/philo/philo.py index 0f75589..3319a0b 100644 --- a/src/philo/philo.py +++ b/src/philo/philo.py @@ -6,13 +6,13 @@ # By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/01 10:52:56 by cacharle #+# #+# # -# Updated: 2020/10/01 16:39:30 by cacharle ### ########.fr # +# Updated: 2020/10/05 14:03:07 by cacharle ### ########.fr # # # # ############################################################################ # import itertools -from . import error +from philo import error from philo.event import Event @@ -32,20 +32,6 @@ class Philo: self._timeout_sleep = timeout_sleep self._meal_num = meal_num - @property - def used_forks(self): - """ The number of forks currently used by the philosopher """ - if len(self.logs) < 1: - return 0 - if self.logs[-1].event is Event.EAT: - return 2 - if self.logs[-1].event is Event.FORK: - if len(self.logs) > 1 and self.logs[-2].event is Event.FORK: - return 2 - else: - return 1 - return 0 - def check(self): """ Check log for errors @@ -104,32 +90,40 @@ class Philo: if e1 is Event.EAT: self._check_time_range(t1, t2, self._timeout_eat, "Ate") - - # if l1.event is Event.EATING and l2.event is Event.EATING: - # if l2.timestamp - l1.timestamp > self._timeout_eat: - # raise ValueError - # if timeout is not None and l2.timestamp - l1.timestamp > timeout: - # raise ValueError - # check if should be dead - # last_eat = None - # for log in reversed(self.logs): - # if log.event is Event.EATING: - # last_eat = log - # break - # last = self.logs[-1] - # if last_eat is not None and last_eat is not last: - # if last.timestamp - last_eat.timestamp > self._timeout_die + 10: - # self._raise("{} should be dead {} - {} > {}".format(self.id, last.timestamp, last_eat.timestamp, self._timeout_die + 10)) + last_eat = next( + (log for log in reversed(self.logs) if log.event is Event.EAT), + None + ) + last = self.logs[-1] + if last_eat is not None and last_eat is not last: + if last.timestamp - last_eat.timestamp > self._timeout_die + 10: + self._raise( + "{} should be dead {} - {} > {}" + .format(self.id, last.timestamp, + last_eat.timestamp, self._timeout_die + 10) + ) def _check_time_range(self, t1, t2, timeout, verb): - lo = timeout - 10 - hi = timeout + 10 - if not (lo <= t2 - t1 <= hi): - self._raise("{} {}ms expected {}-{}ms".format(verb, t2 - t1, lo, hi)) - + start = timeout - 10 + end = timeout + 10 + if not (start <= t2 - t1 <= end): + self._raise("{} {}ms expected {}-{}ms".format(verb, t2 - t1, start, end)) def _raise(self, msg): """ Helper to raise Log errrors""" raise error.Log(self.logs, msg) + @property + def used_forks(self): + """ The number of forks currently used by the philosopher """ + if len(self.logs) < 1: + return 0 + if self.logs[-1].event is Event.EAT: + return 2 + if self.logs[-1].event is Event.FORK: + if len(self.logs) > 1 and self.logs[-2].event is Event.FORK: + return 2 + else: + return 1 + return 0 |
