[SERVER-6024] unittest assertions (ASSERT_*) should collect stack traces before throwing exceptions. Created: 07/Jun/12  Updated: 07/Mar/18  Resolved: 09/Jan/18

Status: Closed
Project: Core Server
Component/s: Testing Infrastructure
Affects Version/s: None
Fix Version/s: 3.7.1

Type: Improvement Priority: Minor - P4
Reporter: Andy Schwerin Assignee: Kevin Albertson
Resolution: Done Votes: 1
Labels: tig-unittests
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-29041 failing unit tests or dbtests should ... Closed
Backwards Compatibility: Minor Change
Sprint: TIG 2018-1-15, TIG 2018-1-1
Participants:

 Description   

When an ASSERT_* macro fails, it throws a special test exception. It should collect, but not immediately print, a stack trace record, to aid in debugging failed tests.



 Comments   
Comment by Githook User [ 08/Jan/18 ]

Author:

{'name': 'Kevin Albertson', 'username': 'kevinAlbs', 'email': 'kevin.albertson@10gen.com'}

Message: SERVER-6024 add stacktrace to unittests
Branch: master
https://github.com/mongodb/mongo/commit/36eb9e78bc7466cac268c8b2995956a47e946ee5

Comment by Kevin Albertson [ 26/Dec/17 ]

That would be simpler, but Max pointed out that this would show stack traces even when we expect code to throw as in an ASSERT_THROWS. So I'll store the stack trace on the exception and and print it if the exception reaches up to unittest::Suite::run. Also, It'll likely be a much smaller change to store the stringified stack trace, so I'll do that first.

Comment by Andy Schwerin [ 24/Dec/17 ]

Yeah, that would work pretty well. Mathias and Judah suggested a simpler
approach would be to print at throw time. That might be fine, also, and
could be easier to implement.

On Thu, Dec 21, 2017, 2:42 PM Kevin Albertson (JIRA) <jira@mongodb.org>

Comment by Kevin Albertson [ 21/Dec/17 ]

I think we can add the stack trace to TestAssertionFailureException and print out the stack trace in the unit test summary. E.g.

[main] going to run suite: BSONElement
[main]   going to run test: BinDataToString
[main] Throwing exception: Expected 0 == 1 (0 == 1) @src/mongo/bson/bsonelement_test.cpp:42
[main] FAIL: BinDataToString    Expected 0 == 1 (0 == 1) @src/mongo/bson/bsonelement_test.cpp:42
[main]   going to run test: TimestampToString
[main]   DONE running tests
[main] **************************************************
[main] BSONElement                    | tests:    2 | fails:    1 | assert calls:          0 | time secs:  0.000
BinDataToString Expected 0 == 1 (0 == 1) @src/mongo/bson/bsonelement_test.cpp:42
[main] TOTALS                         | tests:    2 | fails:    1 | assert calls:          0 | time secs:  0.000
[main] Failing tests:
[main]   BSONElement/BinDataToString Failed
[main]   Stack trace:
[main]   <stack trace for failed assertion here>
[main] FAILURE - 1 tests in 1 suites failed

Judah mentioned it may be helpful to get the stack traces for other exceptions as well. From talking to Mathias it looks like we can add a stack trace field to Status::ErrorInfo. We can limit this to only getting populated in the context of unit tests since it could impact perf. With this, uasserts/masserts show stack traces the same way as a test assertion.

schwerin does this sound like what you had in mind?

Generated at Thu Feb 08 03:10:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.