Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Ley
synapse
Commits
5e3ca12b
Unverified
Commit
5e3ca12b
authored
4 years ago
by
Richard van der Hoff
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Create a mechanism for marking tests "logcontext clean" (#8399)
parent
bd715e12
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
changelog.d/8399.misc
+1
-0
1 addition, 0 deletions
changelog.d/8399.misc
synapse/logging/context.py
+23
-20
23 additions, 20 deletions
synapse/logging/context.py
tests/crypto/test_keyring.py
+3
-0
3 additions, 0 deletions
tests/crypto/test_keyring.py
tests/unittest.py
+14
-1
14 additions, 1 deletion
tests/unittest.py
with
41 additions
and
21 deletions
changelog.d/8399.misc
0 → 100644
+
1
−
0
View file @
5e3ca12b
Create a mechanism for marking tests "logcontext clean".
This diff is collapsed.
Click to expand it.
synapse/logging/context.py
+
23
−
20
View file @
5e3ca12b
...
...
@@ -65,6 +65,11 @@ except Exception:
return
None
# a hook which can be set during testing to assert that we aren't abusing logcontexts.
def
logcontext_error
(
msg
:
str
):
logger
.
warning
(
msg
)
# get an id for the current thread.
#
# threading.get_ident doesn't actually return an OS-level tid, and annoyingly,
...
...
@@ -330,10 +335,9 @@ class LoggingContext:
"""
Enters this logging context into thread local storage
"""
old_context
=
set_current_context
(
self
)
if
self
.
previous_context
!=
old_context
:
logger
.
warning
(
"
Expected previous context %r, found %r
"
,
self
.
previous_context
,
old_context
,
logcontext_error
(
"
Expected previous context %r, found %r
"
%
(
self
.
previous_context
,
old_context
,)
)
return
self
...
...
@@ -346,10 +350,10 @@ class LoggingContext:
current
=
set_current_context
(
self
.
previous_context
)
if
current
is
not
self
:
if
current
is
SENTINEL_CONTEXT
:
log
ger
.
warning
(
"
Expected logging context %s was lost
"
,
self
)
log
context_error
(
"
Expected logging context %s was lost
"
%
(
self
,)
)
else
:
log
ger
.
warning
(
"
Expected logging context %s but found %s
"
,
self
,
current
log
context_error
(
"
Expected logging context %s but found %s
"
%
(
self
,
current
)
)
# the fact that we are here suggests that the caller thinks that everything
...
...
@@ -387,16 +391,16 @@ class LoggingContext:
support getrusuage.
"""
if
get_thread_id
()
!=
self
.
main_thread
:
log
ger
.
warning
(
"
Started logcontext %s on different thread
"
,
self
)
log
context_error
(
"
Started logcontext %s on different thread
"
%
(
self
,)
)
return
if
self
.
finished
:
log
ger
.
warning
(
"
Re-starting finished log context %s
"
,
self
)
log
context_error
(
"
Re-starting finished log context %s
"
%
(
self
,)
)
# If we haven't already started record the thread resource usage so
# far
if
self
.
usage_start
:
log
ger
.
warning
(
"
Re-starting already-active log context %s
"
,
self
)
log
context_error
(
"
Re-starting already-active log context %s
"
%
(
self
,)
)
else
:
self
.
usage_start
=
rusage
...
...
@@ -414,7 +418,7 @@ class LoggingContext:
try
:
if
get_thread_id
()
!=
self
.
main_thread
:
log
ger
.
warning
(
"
Stopped logcontext %s on different thread
"
,
self
)
log
context_error
(
"
Stopped logcontext %s on different thread
"
%
(
self
,)
)
return
if
not
rusage
:
...
...
@@ -422,9 +426,9 @@ class LoggingContext:
# Record the cpu used since we started
if
not
self
.
usage_start
:
log
ger
.
warning
(
"
Called stop on logcontext %s without recording a start rusage
"
,
self
,
log
context_error
(
"
Called stop on logcontext %s without recording a start rusage
"
%
(
self
,
)
)
return
...
...
@@ -584,14 +588,13 @@ class PreserveLoggingContext:
if
context
!=
self
.
_new_context
:
if
not
context
:
log
ger
.
warning
(
"
Expected logging context %s was lost
"
,
self
.
_new_context
log
context_error
(
"
Expected logging context %s was lost
"
%
(
self
.
_new_context
,)
)
else
:
logger
.
warning
(
"
Expected logging context %s but found %s
"
,
self
.
_new_context
,
context
,
logcontext_error
(
"
Expected logging context %s but found %s
"
%
(
self
.
_new_context
,
context
,)
)
...
...
This diff is collapsed.
Click to expand it.
tests/crypto/test_keyring.py
+
3
−
0
View file @
5e3ca12b
...
...
@@ -41,6 +41,7 @@ from synapse.storage.keys import FetchKeyResult
from
tests
import
unittest
from
tests.test_utils
import
make_awaitable
from
tests.unittest
import
logcontext_clean
class
MockPerspectiveServer
:
...
...
@@ -67,6 +68,7 @@ class MockPerspectiveServer:
signedjson
.
sign
.
sign_json
(
res
,
self
.
server_name
,
self
.
key
)
@logcontext_clean
class
KeyringTestCase
(
unittest
.
HomeserverTestCase
):
def
check_context
(
self
,
val
,
expected
):
self
.
assertEquals
(
getattr
(
current_context
(),
"
request
"
,
None
),
expected
)
...
...
@@ -309,6 +311,7 @@ class KeyringTestCase(unittest.HomeserverTestCase):
mock_fetcher2
.
get_keys
.
assert_called_once
()
@logcontext_clean
class
ServerKeyFetcherTestCase
(
unittest
.
HomeserverTestCase
):
def
make_homeserver
(
self
,
reactor
,
clock
):
self
.
http_client
=
Mock
()
...
...
This diff is collapsed.
Click to expand it.
tests/unittest.py
+
14
−
1
View file @
5e3ca12b
...
...
@@ -23,7 +23,7 @@ import logging
import
time
from
typing
import
Optional
,
Tuple
,
Type
,
TypeVar
,
Union
from
mock
import
Mock
from
mock
import
Mock
,
patch
from
canonicaljson
import
json
...
...
@@ -169,6 +169,19 @@ def INFO(target):
return
target
def
logcontext_clean
(
target
):
"""
A decorator which marks the TestCase or method as
'
logcontext_clean
'
...
ie
,
any
logcontext
errors
should
cause
a
test
failure
"""
def
logcontext_error
(
msg
):
raise
AssertionError
(
"
logcontext error: %s
"
%
(
msg
))
patcher
=
patch
(
"
synapse.logging.context.logcontext_error
"
,
new
=
logcontext_error
)
return
patcher
(
target
)
class
HomeserverTestCase
(
TestCase
):
"""
A base TestCase that reduces boilerplate for HomeServer-using test cases.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment