[SERVER-34304] matchers for use in assertion conditions Created: 04/Apr/18  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Billy Donahue Assignee: Backlog - Service Architecture
Resolution: Unresolved Votes: 1
Labels: sa-remove-fv-backlog-22
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-37517 Decompose expressions in ASSERT() to ... Closed
related to SERVER-51888 Add TS2's std::is_detected etc API to... Closed
is related to SERVER-57956 richer invariant failure Backlog
Assigned Teams:
Service Arch
Participants:

 Description   

In the statement:
uassert(errcode, message, expression);

We evaluate the truth of expression and its stringified form, but the values that went into it are lost. Currently I see the 'message' argument is often trying to encode a redundant representation of the 'expression' argument.

src/mongo/bson/util/bson_check.h-104-
inline void checkBSONType(BSONType expectedType, const BSONElement& elem) {
    uassert(elem.type() == BSONType::EOO ? ErrorCodes::NoSuchKey : ErrorCodes::TypeMismatch,
            str::stream() << "Wrong type for '" << elem.fieldNameStringData() << "'. Expected a "
                          << typeName(expectedType)
                          << ", got a "
                          << typeName(elem.type())
                          << '.',
            elem.type() == expectedType);
}

Or just letting diagnostic info go to waste:

src/mongo/bson/ordering.h:81:
    uassert(13103, "too many compound keys", n <= 31);

A simple library of matchers similar to google mock matchers could improve the situation considerably.

src/mongo/bson/util/bson_check.h-104-
inline void checkBSONType(BSONType expectedType, const BSONElement& elem) {
    uassert(elem.type() == BSONType::EOO ? ErrorCodes::NoSuchKey : ErrorCodes::TypeMismatch,
            str::stream() << "Wrong type for '" << elem.fieldNameStringData(),
            Arg(elem.type()) == Arg(expectedType));
}

src/mongo/bson/ordering.h:81:
    uassert(13103, "too many compound keys", Arg(n) <= Arg(31));



 Comments   
Comment by Billy Donahue [ 17/Jul/19 ]

skunkworks

Comment by Billy Donahue [ 04/Apr/18 ]

Alternate formulation, closer to gmock matchers. ExpectThat takes an actual and a matcher to apply to it.

... ExpectThat(actual, matcher);
 
src/mongo/bson/util/bson_check.h-104-
uassert(elem.type() == BSONType::EOO ? ErrorCodes::NoSuchKey : ErrorCodes::TypeMismatch,
        str::stream() << "Wrong type for '" << elem.fieldNameStringData(),
        ExpectThat(elem.type(), Eq(expectedType)));
 
src/mongo/bson/ordering.h:81:
uassert(13103,
        "too many compound keys",
        ExpectThat(n, Le(31)));

Generated at Thu Feb 08 04:36:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.