diff --git a/tardis/utilities/asynccachemap.py b/tardis/utilities/asynccachemap.py index 2b1733bb..d05427c0 100644 --- a/tardis/utilities/asynccachemap.py +++ b/tardis/utilities/asynccachemap.py @@ -55,3 +55,15 @@ def __getitem__(self, item): def __len__(self): return len(self._data) + + def __eq__(self, other): + if not isinstance(other, AsyncCacheMap): + return False + + return ( + self._update_coroutine == other._update_coroutine + and self._max_age == other._max_age + and self._last_update == other._last_update + and self._data == other._data + and self._lock == other._lock + ) diff --git a/tests/utilities_t/test_asynccachemap.py b/tests/utilities_t/test_asynccachemap.py index e67825bd..bad5f3ac 100644 --- a/tests/utilities_t/test_asynccachemap.py +++ b/tests/utilities_t/test_asynccachemap.py @@ -70,3 +70,30 @@ def test_last_update(self): self.assertTrue( datetime.now() - self.async_cache_map.last_update < timedelta(seconds=1) ) + + def test_eq_async_cache_map(self): + test_cache_map = AsyncCacheMap( + update_coroutine=self.async_cache_map._update_coroutine + ) + # Since both objects have been recently initialized, all values (self._max_age, + # self._last_update, self._data and self._lock) are still the defaults + self.assertTrue(self.async_cache_map == test_cache_map) + + # Test the opposite + self.assertFalse(self.async_cache_map != test_cache_map) + + # change default values + run_async(self.async_cache_map.update_status) + self.assertFalse(self.async_cache_map == test_cache_map) + + # update default values, self._last_update, self._lock still differ + run_async(test_cache_map.update_status) + self.assertFalse(self.async_cache_map == test_cache_map) + + # Assimilate lock, self._last_update still differs + test_cache_map._lock = self.async_cache_map._lock + self.assertFalse(self.async_cache_map == test_cache_map) + + # Make them equal again + test_cache_map._last_update = self.async_cache_map._last_update + self.assertTrue(self.async_cache_map == test_cache_map)