From 0f5f04b409dd09653b0477f056063ead1208cff7 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 12 Dec 2018 14:09:50 +0530 Subject: [PATCH] fix(tests): one more attempt to fix the erratic test_scheduler.py (#6612) * debug(test_scheduler): please fail * fix(tests): use flags instead of globals since cache is not trhead-safe --- frappe/cache_manager.py | 6 ++++-- frappe/tests/test_scheduler.py | 16 +++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/frappe/cache_manager.py b/frappe/cache_manager.py index f2728122ef..a3864a9d0c 100644 --- a/frappe/cache_manager.py +++ b/frappe/cache_manager.py @@ -17,6 +17,10 @@ def clear_user_cache(user=None): "defaults", "user_permissions", "home_page", "linked_with", "desktop_icons", 'portal_menu_items') + # this will automatically reload the global cache + # so it is important to clear this first + clear_notifications(user) + if user: for name in groups: cache.hdel(name, user) @@ -28,8 +32,6 @@ def clear_user_cache(user=None): clear_defaults_cache() clear_global_cache() - clear_notifications(user) - def clear_global_cache(): from frappe.website.render import clear_cache as clear_website_cache diff --git a/frappe/tests/test_scheduler.py b/frappe/tests/test_scheduler.py index 6c72a49d61..a10e911b04 100644 --- a/frappe/tests/test_scheduler.py +++ b/frappe/tests/test_scheduler.py @@ -3,14 +3,14 @@ from __future__ import unicode_literals from unittest import TestCase from dateutil.relativedelta import relativedelta from frappe.utils.scheduler import (enqueue_applicable_events, restrict_scheduler_events_if_dormant, - get_enabled_scheduler_events, disable_scheduler_on_expiry) + get_enabled_scheduler_events, disable_scheduler_on_expiry) from frappe import _dict from frappe.utils.background_jobs import enqueue from frappe.utils import now_datetime, today, add_days, add_to_date from frappe.limits import update_limits, clear_limit import frappe -import json, time +import time def test_timeout(): '''This function needs to be pickleable''' @@ -49,12 +49,12 @@ class TestScheduler(TestCase): frappe.flags.enabled_events = None def test_enabled_events_day_change(self): - val = json.dumps(["daily", "daily_long", "weekly", "weekly_long", - "monthly", "monthly_long"]) - frappe.db.set_global('enabled_scheduler_events', val) - # TEMP for debug: this test fails randomly - print('Setting enabled_scheduler_events {0}'.format(val)) + # use flags instead of globals as this test fails intermittently + # the root cause has not been identified but the culprit seems cache + # since cache is mutable, it maybe be changed by a parallel process + frappe.flags.enabled_events = ["daily", "daily_long", "weekly", "weekly_long", + "monthly", "monthly_long"] # maintain last_event and next_event on different days next_event = now_datetime().replace(hour=0, minute=0, second=0, microsecond=0) @@ -65,6 +65,8 @@ class TestScheduler(TestCase): self.assertTrue("all" in frappe.flags.ran_schedulers) self.assertFalse("hourly" in frappe.flags.ran_schedulers) + frappe.flags.enabled_events = None + def test_restrict_scheduler_events(self): frappe.set_user("Administrator")