Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
mongo-python-driver/test/helpers.py at main · mongodb/mongo-python-driver · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
mongodb
/
mongo-python-driver
Public
Notifications
You must be signed in to change notification settings
Fork
1.2k
Star
4.4k
Code
Pull requests
16
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
mongo-python-driver
/
test
/
helpers.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
171 lines (133 loc) · 5.38 KB
main
Breadcrumbs
mongo-python-driver
/
test
/
helpers.py
Copy path
Top
File metadata and controls
Code
Blame
171 lines (133 loc) · 5.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright 2024-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Shared helper methods for pymongo, bson, and gridfs test suites."""
from
__future__
import
annotations
import
asyncio
import
threading
import
traceback
from
functools
import
wraps
from
typing
import
Optional
,
no_type_check
from
bson
import
SON
from
pymongo
import
common
from
pymongo
.
_asyncio_task
import
create_task
from
pymongo
.
read_preferences
import
ReadPreference
_IS_SYNC
=
True
def
repl_set_step_down
(
client
,
**
kwargs
):
"""Run replSetStepDown, first unfreezing a secondary with replSetFreeze."""
cmd
=
SON
([(
"replSetStepDown"
,
1
)])
cmd
.
update
(
kwargs
)
# Unfreeze a secondary to ensure a speedy election.
client
.
admin
.
command
(
"replSetFreeze"
,
0
,
read_preference
=
ReadPreference
.
SECONDARY
)
client
.
admin
.
command
(
cmd
)
class
client_knobs
:
def
__init__
(
self
,
heartbeat_frequency
=
None
,
min_heartbeat_interval
=
None
,
kill_cursor_frequency
=
None
,
events_queue_frequency
=
None
,
):
self
.
heartbeat_frequency
=
heartbeat_frequency
self
.
min_heartbeat_interval
=
min_heartbeat_interval
self
.
kill_cursor_frequency
=
kill_cursor_frequency
self
.
events_queue_frequency
=
events_queue_frequency
self
.
old_heartbeat_frequency
=
None
self
.
old_min_heartbeat_interval
=
None
self
.
old_kill_cursor_frequency
=
None
self
.
old_events_queue_frequency
=
None
self
.
_enabled
=
False
self
.
_stack
=
None
def
enable
(
self
):
self
.
old_heartbeat_frequency
=
common
.
HEARTBEAT_FREQUENCY
self
.
old_min_heartbeat_interval
=
common
.
MIN_HEARTBEAT_INTERVAL
self
.
old_kill_cursor_frequency
=
common
.
KILL_CURSOR_FREQUENCY
self
.
old_events_queue_frequency
=
common
.
EVENTS_QUEUE_FREQUENCY
if
self
.
heartbeat_frequency
is
not
None
:
common
.
HEARTBEAT_FREQUENCY
=
self
.
heartbeat_frequency
if
self
.
min_heartbeat_interval
is
not
None
:
common
.
MIN_HEARTBEAT_INTERVAL
=
self
.
min_heartbeat_interval
if
self
.
kill_cursor_frequency
is
not
None
:
common
.
KILL_CURSOR_FREQUENCY
=
self
.
kill_cursor_frequency
if
self
.
events_queue_frequency
is
not
None
:
common
.
EVENTS_QUEUE_FREQUENCY
=
self
.
events_queue_frequency
self
.
_enabled
=
True
# Store the allocation traceback to catch non-disabled client_knobs.
self
.
_stack
=
""
.
join
(
traceback
.
format_stack
())
def
__enter__
(
self
):
self
.
enable
()
@
no_type_check
def
disable
(
self
):
common
.
HEARTBEAT_FREQUENCY
=
self
.
old_heartbeat_frequency
common
.
MIN_HEARTBEAT_INTERVAL
=
self
.
old_min_heartbeat_interval
common
.
KILL_CURSOR_FREQUENCY
=
self
.
old_kill_cursor_frequency
common
.
EVENTS_QUEUE_FREQUENCY
=
self
.
old_events_queue_frequency
self
.
_enabled
=
False
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
disable
()
def
__call__
(
self
,
func
):
def
make_wrapper
(
f
):
@
wraps
(
f
)
def
wrap
(
*
args
,
**
kwargs
):
with
self
:
return
f
(
*
args
,
**
kwargs
)
return
wrap
return
make_wrapper
(
func
)
def
__del__
(
self
):
if
self
.
_enabled
:
msg
=
(
f"ERROR: client_knobs still enabled! HEARTBEAT_FREQUENCY=
{
common
.
HEARTBEAT_FREQUENCY
}
, "
f"MIN_HEARTBEAT_INTERVAL=
{
common
.
MIN_HEARTBEAT_INTERVAL
}
, KILL_CURSOR_FREQUENCY=
{
common
.
KILL_CURSOR_FREQUENCY
}
, "
f"EVENTS_QUEUE_FREQUENCY=
{
common
.
EVENTS_QUEUE_FREQUENCY
}
, stack:
\n
{
self
.
_stack
}
"
)
self
.
disable
()
raise
Exception
(
msg
)
# Global knobs to speed up the test suite.
global_knobs
=
client_knobs
(
events_queue_frequency
=
0.05
)
if
_IS_SYNC
:
PARENT
=
threading
.
Thread
else
:
PARENT
=
object
class
ConcurrentRunner
(
PARENT
):
def
__init__
(
self
,
**
kwargs
):
if
_IS_SYNC
:
super
().
__init__
(
**
kwargs
)
self
.
name
=
kwargs
.
get
(
"name"
,
"ConcurrentRunner"
)
self
.
stopped
=
False
self
.
task
=
None
self
.
target
=
kwargs
.
get
(
"target"
,
None
)
self
.
args
=
kwargs
.
get
(
"args"
, [])
if
not
_IS_SYNC
:
def
start
(
self
):
self
.
task
=
create_task
(
self
.
run
(),
name
=
self
.
name
)
def
join
(
self
,
timeout
:
Optional
[
float
]
=
None
):
# type: ignore[override]
if
self
.
task
is
not
None
:
asyncio
.
wait
([
self
.
task
],
timeout
=
timeout
)
def
is_alive
(
self
):
return
not
self
.
stopped
def
run
(
self
):
try
:
self
.
target
(
*
self
.
args
)
finally
:
self
.
stopped
=
True
class
ExceptionCatchingTask
(
ConcurrentRunner
):
"""A Task that stores any exception encountered while running."""
def
__init__
(
self
,
**
kwargs
):
super
().
__init__
(
**
kwargs
)
self
.
exc
=
None
def
run
(
self
):
try
:
super
().
run
()
except
BaseException
as
exc
:
self
.
exc
=
exc
raise
You can’t perform that action at this time.