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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
import asyncio
import platform
import time
from concurrent.futures import ThreadPoolExecutor
import psycopg
import pytest
from .utils import USE_SUDO
def test_server_lifetime(pg, bouncer):
bouncer.admin(f"set server_lifetime=2")
bouncer.test()
assert pg.connection_count() == 1
time.sleep(3)
assert pg.connection_count() == 0
bouncer.test()
def test_server_lifetime_per_pool(pg, bouncer):
bouncer.test(dbname="p9")
assert pg.connection_count() == 1
time.sleep(3)
assert pg.connection_count() == 0
bouncer.test(dbname="p9")
def test_server_idle_timeout(pg, bouncer):
bouncer.admin(f"set server_idle_timeout=2")
bouncer.test()
assert pg.connection_count() == 1
time.sleep(3)
assert pg.connection_count() == 0
bouncer.test()
def test_user_idle_transaction_timeout_negative(bouncer):
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
admin_users = pgbouncer
auth_type = trust
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
pool_mode = session
[users]
puser1 = pool_mode=transaction idle_transaction_timeout=6
"""
# while configured to be in statement pooling mode
with bouncer.run_with_config(config):
with bouncer.transaction(dbname="postgres", user="puser1") as cur:
time.sleep(3)
cur.execute("select 1")
def test_user_idle_transaction_timeout_override_global(bouncer):
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
admin_users = pgbouncer
auth_type = trust
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
pool_mode = session
idle_transaction_timeout=100000
[users]
puser1 = pool_mode=transaction idle_transaction_timeout=1
"""
# while configured to be in statement pooling mode
with bouncer.run_with_config(config):
with bouncer.transaction(dbname="postgres", user="puser1") as cur:
with bouncer.log_contains(r"idle transaction timeout"):
time.sleep(3)
with pytest.raises(
psycopg.OperationalError,
match=r"idle transaction timeout|Software caused connection abort|server closed the connection unexpectedly",
):
cur.execute("select 1")
def test_user_idle_transaction_timeout(bouncer):
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
admin_users = pgbouncer
auth_type = trust
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
pool_mode = session
[users]
puser1 = pool_mode=transaction idle_transaction_timeout=1
"""
# while configured to be in statement pooling mode
with bouncer.run_with_config(config):
with bouncer.transaction(dbname="postgres", user="puser1") as cur:
with bouncer.log_contains(r"idle transaction timeout"):
time.sleep(3)
with pytest.raises(
psycopg.OperationalError,
match=r"idle transaction timeout|Software caused connection abort|server closed the connection unexpectedly",
):
cur.execute("select 1")
def test_user_query_timeout_override_global(bouncer):
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
admin_users = pgbouncer
auth_type = trust
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
pool_mode = session
query_timeout=100000
[users]
puser1 = pool_mode=statement query_timeout=1
"""
# while configured to be in statement pooling mode
with bouncer.run_with_config(config):
with bouncer.log_contains(r"query timeout"):
with pytest.raises(
psycopg.OperationalError,
match=r"query timeout|server closed the connection unexpectedly",
):
bouncer.sleep(5, user="puser1", dbname="postgres")
def test_user_query_timeout_negative(bouncer):
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
admin_users = pgbouncer
auth_type = trust
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
pool_mode = session
[users]
puser1 = pool_mode=statement query_timeout=10
"""
# while configured to be in statement pooling mode
with bouncer.run_with_config(config):
bouncer.sleep(5, user="puser1", dbname="postgres")
def test_user_query_timeout(bouncer):
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
admin_users = pgbouncer
auth_type = trust
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
pool_mode = session
[users]
puser1 = pool_mode=statement query_timeout=1
"""
# while configured to be in statement pooling mode
with bouncer.run_with_config(config):
with bouncer.log_contains(r"query timeout"):
with pytest.raises(
psycopg.OperationalError,
match=r"query timeout|server closed the connection unexpectedly",
):
bouncer.sleep(5, user="puser1", dbname="postgres")
def test_query_timeout(bouncer):
bouncer.admin(f"set query_timeout=1")
with bouncer.log_contains(r"query timeout"):
with pytest.raises(
psycopg.OperationalError,
match=r"query timeout|server closed the connection unexpectedly",
):
bouncer.sleep(5)
def test_user_level_idle_client_timeout_negative(bouncer):
"""Test user level client_idle_timeout correctly closes connection."""
bouncer.admin("set pool_mode=transaction")
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
auth_type = trust
admin_users = pgbouncer
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
auth_dbname = postgres
pool_mode = session
[users]
puser1 = pool_mode=transaction client_idle_timeout=2
"""
with bouncer.run_with_config(config):
bouncer.admin("RELOAD")
with bouncer.cur(dbname="postgres", user="puser1") as cur:
cur.execute("SELECT 1")
with bouncer.log_contains(r"client_idle_timeout"):
time.sleep(3)
with pytest.raises(
psycopg.OperationalError,
match=r"client_idle_timeout|Software caused connection abort|server closed the connection unexpectedly",
):
cur.execute("SELECT 1")
def test_user_level_idle_client_timeout(bouncer):
"""Test that user level client_idle_timeout allows connection to stay open."""
bouncer.admin("set pool_mode=transaction")
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
auth_type = trust
admin_users = pgbouncer
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
auth_dbname = postgres
pool_mode = session
[users]
puser1 = pool_mode=transaction client_idle_timeout=6
"""
with bouncer.run_with_config(config):
bouncer.admin("RELOAD")
with bouncer.cur(dbname="postgres", user="puser1") as cur:
cur.execute("SELECT 1")
time.sleep(3)
cur.execute("SELECT 1")
def test_user_level_idle_client_timeout_override(bouncer):
"""Test that user level client_idle_timeout overrides global level setting."""
bouncer.admin("set pool_mode=transaction")
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
auth_type = trust
admin_users = pgbouncer
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
auth_dbname = postgres
pool_mode = session
client_idle_timeout=1000000
[users]
puser1 = pool_mode=transaction client_idle_timeout=2
"""
with bouncer.run_with_config(config):
bouncer.admin("RELOAD")
with bouncer.cur(dbname="postgres", user="puser1") as cur:
cur.execute("SELECT 1")
with bouncer.log_contains(r"client_idle_timeout"):
time.sleep(3)
with pytest.raises(
psycopg.OperationalError,
match=r"client_idle_timeout|Software caused connection abort|server closed the connection unexpectedly",
):
cur.execute("SELECT 1")
def test_transaction_timeout_user_overide_global(bouncer):
"""
Test user level transaction timeout correctly overrides global setting.
Procedure:
- Start pgbouncer with config that has
user level transaction timeout of 2 seconds for user psuser1
and a global level setting of 10 seconds
- Start transaction with user puser1
- Wait 1 second
- Test that empty query works
- Wait 2 seconds
- Test that empty query raises psycopg.OperationalError
"""
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
admin_users = pgbouncer
auth_type = trust
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
transaction_timeout=10
pool_mode = session
[users]
puser1 = pool_mode=transaction transaction_timeout=2
"""
with bouncer.run_with_config(config):
with bouncer.transaction(dbname="postgres", user="puser1") as cur:
time.sleep(1)
cur.execute("")
with bouncer.log_contains(r"transaction timeout"):
time.sleep(2)
with pytest.raises(
psycopg.OperationalError,
match=r"transaction timeout|Software caused connection abort",
):
cur.execute("")
def test_transaction_timeout_user(bouncer):
"""
Test user level transaction timeout.
Procedure:
- Start pgbouncer with config that has
user level transaction timeout of 2 seconds for user psuser1.
- Start transaction with user puser1
- Wait 1 second
- Test that empty query works
- Wait 2 seconds
- Test that empty query raises psycopg.OperationalError
"""
config = f"""
[databases]
postgres = host={bouncer.pg.host} port={bouncer.pg.port}
[pgbouncer]
listen_addr = {bouncer.host}
admin_users = pgbouncer
auth_type = trust
auth_file = {bouncer.auth_path}
listen_port = {bouncer.port}
logfile = {bouncer.log_path}
pool_mode = session
[users]
puser1 = pool_mode=transaction transaction_timeout=2
"""
with bouncer.run_with_config(config):
with bouncer.transaction(dbname="postgres", user="puser1") as cur:
time.sleep(1)
cur.execute("")
with bouncer.log_contains(r"transaction timeout"):
time.sleep(2)
with pytest.raises(
psycopg.OperationalError,
match=r"transaction timeout|Software caused connection abort",
):
cur.execute("")
def test_transaction_timeout(bouncer):
"""
Test pgbouncer level transaction timeout.
Procedure:
- Set pool_mode=transaction in admin console (default is statement)
- Set transaction_timeout=2
- start transaction.
- Wait one second
- Execute empty query. Test that no error is raised
- Wait 2 seconds
- Execute emtpty query. Test that psycopg.OperationalError is raised
"""
bouncer.admin("SET pool_mode=transaction")
bouncer.admin("SET transaction_timeout=2")
with bouncer.transaction() as cur:
time.sleep(1)
cur.execute("")
with bouncer.log_contains(r"transaction timeout"):
time.sleep(2)
with pytest.raises(
psycopg.OperationalError,
match=r"transaction timeout|Software caused connection abort",
):
cur.execute("")
def test_idle_transaction_timeout(bouncer):
bouncer.admin(f"set pool_mode=transaction")
bouncer.admin(f"set idle_transaction_timeout=2")
with bouncer.transaction() as cur:
with bouncer.log_contains(r"idle transaction timeout"):
time.sleep(3)
with pytest.raises(
psycopg.OperationalError,
match=r"idle transaction timeout|Software caused connection abort|server closed the connection unexpectedly",
):
cur.execute("select 1")
# test for GH issue #125
with bouncer.transaction() as cur:
cur.execute("select pg_sleep(2)").fetchone()
time.sleep(1)
cur.execute("select 1")
def test_client_idle_timeout(bouncer):
bouncer.admin(f"set client_idle_timeout=2")
with bouncer.cur() as cur:
cur.execute("select 1")
with bouncer.log_contains(r"client_idle_timeout"):
time.sleep(3)
with pytest.raises(
psycopg.OperationalError,
match=r"client_idle_timeout|Software caused connection abort|server closed the connection unexpectedly",
):
cur.execute("select 1")
async def test_server_login_retry(pg, bouncer):
bouncer.admin(f"set query_timeout=10")
bouncer.admin(f"set server_login_retry=3")
# Disable tls to get more consistent timings
bouncer.admin("set server_tls_sslmode = disable")
pg.stop()
if platform.system() == "FreeBSD":
# XXX: For some reason FreeBSD logs don't contain connect failed
# For now we simply remove this check. But this warants further
# investigation.
await asyncio.gather(
bouncer.atest(connect_timeout=10),
pg.delayed_start(1),
)
else:
with bouncer.log_contains("connect failed"):
await asyncio.gather(
bouncer.atest(connect_timeout=10),
pg.delayed_start(1),
)
def test_server_connect_timeout_establish(pg, bouncer):
pg.configure("pre_auth_delay to '5s'")
pg.reload()
bouncer.admin("set query_timeout=3")
bouncer.admin("set server_connect_timeout=2")
with bouncer.log_contains(r"closing because: connect timeout"):
with pytest.raises(psycopg.errors.OperationalError, match="query_timeout"):
bouncer.test(connect_timeout=10)
@pytest.mark.skipif("not USE_SUDO")
def test_server_connect_timeout_drop_traffic(pg, bouncer):
bouncer.admin("set query_timeout=3")
bouncer.admin("set server_connect_timeout=2")
with bouncer.log_contains(r"closing because: connect failed"):
with pg.drop_traffic():
with pytest.raises(psycopg.errors.OperationalError, match="query_timeout"):
bouncer.test(connect_timeout=10)
@pytest.mark.skipif("not USE_SUDO")
@pytest.mark.skipif(
"platform.system() != 'Linux'", reason="tcp_user_timeout is only supported on Linux"
)
def test_tcp_user_timeout(pg, bouncer):
bouncer.admin("set tcp_user_timeout=1000")
bouncer.admin("set query_timeout=5")
# Make PgBouncer cache a connection to Postgres
bouncer.test()
# without tcp_user_timeout, you get a different error message
# about "query timeout" instead
with bouncer.log_contains(r"closing because: server conn crashed?"):
with pg.reject_traffic():
with pytest.raises(
psycopg.OperationalError,
match=r"query timeout|Software caused connection abort|server closed the connection unexpectedly",
):
bouncer.test(connect_timeout=10)
@pytest.mark.skipif("not USE_SUDO")
async def test_server_check_delay(pg, bouncer):
bouncer.admin("set server_check_delay=2")
bouncer.admin("set server_login_retry=3")
bouncer.admin("set query_timeout=10")
with pg.drop_traffic():
time.sleep(3)
query_task = bouncer.atest(connect_timeout=10)
# We wait for 1 second to show that the query is blocked while traffic
# is dropped.
done, pending = await asyncio.wait([query_task], timeout=1)
assert done == set()
assert pending == {query_task}
await query_task
@pytest.mark.skipif("not USE_SUDO")
def test_cancel_wait_timeout(pg, bouncer):
bouncer.admin("set cancel_wait_timeout=1")
with bouncer.cur() as cur:
with ThreadPoolExecutor(max_workers=2) as pool:
query = pool.submit(cur.execute, "select pg_sleep(3)")
time.sleep(1)
with pg.drop_traffic():
with bouncer.log_contains(r"closing because: cancel_wait_timeout"):
cancel = pool.submit(cur.connection.cancel)
cancel.result()
query.result()
|