Skip to content

Commit bd78c1b

Browse files
Add $connPoolStats tests (documentdb#242)
Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
1 parent 4167234 commit bd78c1b

4 files changed

Lines changed: 177 additions & 0 deletions

File tree

documentdb_tests/compatibility/tests/system/diagnostic/commands/connPoolStats/__init__.py

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Tests for connPoolStats command argument validation.
2+
3+
Verifies that connPoolStats accepts all BSON types for the command field
4+
value and ignores unrecognized fields.
5+
"""
6+
7+
import pytest
8+
9+
from documentdb_tests.framework.assertions import assertSuccessPartial
10+
from documentdb_tests.framework.bson_type_validator import (
11+
BsonTypeTestCase,
12+
generate_bson_acceptance_test_cases,
13+
)
14+
from documentdb_tests.framework.executor import execute_admin_command
15+
from documentdb_tests.framework.test_constants import BsonType
16+
17+
pytestmark = pytest.mark.admin
18+
19+
# connPoolStats ignores the command field value — all types should succeed
20+
CONNPOOLSTATS_PARAMS = [
21+
BsonTypeTestCase(
22+
id="connPoolStats_value",
23+
msg="connPoolStats should accept all BSON types",
24+
keyword="connPoolStats",
25+
valid_types=list(BsonType),
26+
),
27+
]
28+
29+
ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(CONNPOOLSTATS_PARAMS)
30+
31+
32+
@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES)
33+
def test_connPoolStats_accepts_any_type(collection, bson_type, sample_value, spec):
34+
"""Test connPoolStats accepts all BSON types for command field value."""
35+
result = execute_admin_command(collection, {"connPoolStats": sample_value})
36+
assertSuccessPartial(result, {"ok": 1.0}, msg=f"connPoolStats should accept {bson_type.value}")
37+
38+
39+
def test_connPoolStats_unrecognized_field(collection):
40+
"""Test connPoolStats with unrecognized extra field succeeds."""
41+
result = execute_admin_command(collection, {"connPoolStats": 1, "foo": 1})
42+
assertSuccessPartial(result, {"ok": 1.0}, msg="Unrecognized field should be ignored")
43+
44+
45+
def test_connPoolStats_multiple_unrecognized_fields(collection):
46+
"""Test connPoolStats with multiple unrecognized fields succeeds."""
47+
result = execute_admin_command(
48+
collection, {"connPoolStats": 1, "foo": 1, "bar": "baz", "qux": []}
49+
)
50+
assertSuccessPartial(result, {"ok": 1.0}, msg="Multiple unrecognized fields should be ignored")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""Tests for connPoolStats consistency and database context.
2+
3+
Verifies connection count invariants (totalCreated is non-decreasing,
4+
totalCreated >= totalInUse + totalAvailable), repeated call stability,
5+
and that the command succeeds regardless of database context.
6+
"""
7+
8+
import pytest
9+
10+
from documentdb_tests.framework.assertions import (
11+
assertResult,
12+
assertSuccessPartial,
13+
)
14+
from documentdb_tests.framework.executor import execute_admin_command, execute_command
15+
from documentdb_tests.framework.property_checks import Gte
16+
from documentdb_tests.framework.test_constants import FLOAT_INFINITY
17+
18+
pytestmark = pytest.mark.admin
19+
20+
21+
def test_connPoolStats_repeated_calls(collection):
22+
"""Verify connPoolStats can be called repeatedly without error."""
23+
for _ in range(5):
24+
result = execute_admin_command(collection, {"connPoolStats": 1})
25+
assertSuccessPartial(result, {"ok": 1.0}, msg="Repeated call should succeed")
26+
27+
28+
def test_connPoolStats_totalCreated_non_decreasing(collection):
29+
"""Verify totalCreated is monotonically non-decreasing across calls."""
30+
r1 = execute_admin_command(collection, {"connPoolStats": 1})
31+
created1 = r1.get("totalCreated", FLOAT_INFINITY)
32+
r2 = execute_admin_command(collection, {"connPoolStats": 1})
33+
assertResult(
34+
r2,
35+
expected={"totalCreated": Gte(created1)},
36+
raw_res=True,
37+
msg="totalCreated should not decrease",
38+
)
39+
40+
41+
def test_connPoolStats_totalCreated_gte_inUse_plus_available(collection):
42+
"""Verify totalCreated >= totalInUse + totalAvailable."""
43+
result = execute_admin_command(collection, {"connPoolStats": 1})
44+
in_use = result.get("totalInUse", FLOAT_INFINITY)
45+
available = result.get("totalAvailable", FLOAT_INFINITY)
46+
minimum = in_use + available
47+
assertResult(
48+
result,
49+
expected={"totalCreated": Gte(minimum)},
50+
raw_res=True,
51+
msg="totalCreated should be >= totalInUse + totalAvailable",
52+
)
53+
54+
55+
def test_connPoolStats_succeeds_on_nonexistent_database(collection):
56+
"""Verify connPoolStats succeeds when run on a non-existent database."""
57+
other_db = f"{collection.name}_nonexistent_db"
58+
other_col = collection.database.client[other_db][collection.name]
59+
result = execute_command(other_col, {"connPoolStats": 1})
60+
assertSuccessPartial(result, {"ok": 1.0}, msg="Should succeed on non-existent database")
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Tests for connPoolStats command core behavior.
2+
3+
Verifies that each response field exists and has the expected type or value.
4+
"""
5+
6+
import pytest
7+
8+
from documentdb_tests.compatibility.tests.system.diagnostic.utils.diagnostic_test_case import (
9+
DiagnosticTestCase,
10+
)
11+
from documentdb_tests.framework.assertions import assertProperties
12+
from documentdb_tests.framework.executor import execute_admin_command
13+
from documentdb_tests.framework.parametrize import pytest_params
14+
from documentdb_tests.framework.property_checks import Gte, IsType
15+
16+
pytestmark = pytest.mark.admin
17+
18+
19+
RESPONSE_PROPERTY_TESTS: list[DiagnosticTestCase] = [
20+
DiagnosticTestCase(
21+
id="totalInUse_gte_0",
22+
checks={"totalInUse": Gte(0)},
23+
msg="totalInUse should be >= 0",
24+
),
25+
DiagnosticTestCase(
26+
id="totalAvailable_gte_0",
27+
checks={"totalAvailable": Gte(0)},
28+
msg="totalAvailable should be >= 0",
29+
),
30+
DiagnosticTestCase(
31+
id="totalCreated_gte_0",
32+
checks={"totalCreated": Gte(0)},
33+
msg="totalCreated should be >= 0",
34+
),
35+
DiagnosticTestCase(
36+
id="totalRefreshing_gte_0",
37+
checks={"totalRefreshing": Gte(0)},
38+
msg="totalRefreshing should be >= 0",
39+
),
40+
DiagnosticTestCase(
41+
id="pools_is_object",
42+
checks={"pools": IsType("object")},
43+
msg="pools should be a document",
44+
),
45+
DiagnosticTestCase(
46+
id="hosts_is_object",
47+
checks={"hosts": IsType("object")},
48+
msg="hosts should be a document",
49+
),
50+
DiagnosticTestCase(
51+
id="numClientConnections_gte_0",
52+
checks={"numClientConnections": Gte(0)},
53+
msg="numClientConnections should be >= 0",
54+
),
55+
DiagnosticTestCase(
56+
id="numAScopedConnections_gte_0",
57+
checks={"numAScopedConnections": Gte(0)},
58+
msg="numAScopedConnections should be >= 0",
59+
),
60+
]
61+
62+
63+
@pytest.mark.parametrize("test", pytest_params(RESPONSE_PROPERTY_TESTS))
64+
def test_connPoolStats_response_properties(collection, test):
65+
"""Verify a connPoolStats response field exists and has the expected type or value."""
66+
result = execute_admin_command(collection, {"connPoolStats": 1})
67+
assertProperties(result, test.checks, msg=test.msg, raw_res=True)

0 commit comments

Comments
 (0)