-
-
Notifications
You must be signed in to change notification settings - Fork 368
chore: Add tests for SentryCrashContext #6373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
Tests/SentryTests/SentryCrash/SentryCrashMachineContextTests.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#import <XCTest/XCTest.h> | ||
|
||
#import "SentryCrashMachineContext.h" | ||
#import "SentryCrashMachineContext_Apple.h" | ||
#import "TestThread.h" | ||
#import <mach/mach.h> | ||
|
||
@interface SentryCrashMachineContextTests : XCTestCase | ||
@end | ||
|
||
@implementation SentryCrashMachineContextTests | ||
|
||
- (void)testGetContextForThread_NonCrashedContext_DoesNotPopulateThreadList | ||
{ | ||
// Create a test thread | ||
NSObject *notificationObject = [[NSObject alloc] init]; | ||
TestThread *thread = [[TestThread alloc] init]; | ||
thread.notificationObject = notificationObject; | ||
|
||
XCTestExpectation *exp = [self expectationWithDescription:@"thread started"]; | ||
[NSNotificationCenter.defaultCenter | ||
addObserverForName:@"io.sentry.test.TestThread.main" | ||
object:notificationObject | ||
queue:nil | ||
usingBlock:^(NSNotification *_Nonnull __unused notification) { | ||
[NSNotificationCenter.defaultCenter | ||
removeObserver:self | ||
name:@"io.sentry.test.TestThread.main" | ||
object:notificationObject]; | ||
[exp fulfill]; | ||
}]; | ||
|
||
[thread start]; | ||
[self waitForExpectationsWithTimeout:1 handler:nil]; | ||
itaybre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
kern_return_t kr; | ||
kr = thread_suspend(thread.thread); | ||
XCTAssertTrue(kr == KERN_SUCCESS, @"Thread suspension failed"); | ||
|
||
// Get context for a non-crashed thread | ||
SentryCrashMC_NEW_CONTEXT(machineContext); | ||
bool result = sentrycrashmc_getContextForThread(thread.thread, machineContext, NO); | ||
|
||
XCTAssertTrue(result, @"Failed to get context for thread"); | ||
XCTAssertFalse( | ||
sentrycrashmc_isCrashedContext(machineContext), @"Should not be marked as crashed context"); | ||
|
||
// For non-crashed contexts, thread list should not be populated (will be 0) | ||
int threadCount = sentrycrashmc_getThreadCount(machineContext); | ||
XCTAssertEqual( | ||
threadCount, 0, @"Thread count should be 0 for non-crashed context, got %d", threadCount); | ||
|
||
thread_resume(thread.thread); | ||
XCTestExpectation *expectation = | ||
[[XCTestExpectation alloc] initWithDescription:@"Wait for thread to cancel"]; | ||
thread.endExpectation = expectation; | ||
[thread cancel]; | ||
[self waitForExpectations:@[ expectation ] timeout:1]; | ||
} | ||
|
||
- (void)testGetContextForThread_WithManyThreads | ||
{ | ||
NSInteger numberOfThreads = 10; | ||
NSMutableArray<TestThread *> *threads = [NSMutableArray arrayWithCapacity:numberOfThreads]; | ||
NSMutableArray<XCTestExpectation *> *expectations = | ||
[NSMutableArray arrayWithCapacity:numberOfThreads]; | ||
|
||
for (int i = 0; i < numberOfThreads; i++) { | ||
NSObject *notificationObject = [[NSObject alloc] init]; | ||
TestThread *thread = [[TestThread alloc] init]; | ||
thread.notificationObject = notificationObject; | ||
|
||
XCTestExpectation *exp = | ||
[self expectationWithDescription:[NSString stringWithFormat:@"thread %d started", i]]; | ||
[expectations addObject:exp]; | ||
|
||
[NSNotificationCenter.defaultCenter | ||
addObserverForName:@"io.sentry.test.TestThread.main" | ||
object:notificationObject | ||
queue:nil | ||
usingBlock:^(NSNotification *_Nonnull __unused notification) { | ||
[NSNotificationCenter.defaultCenter | ||
removeObserver:self | ||
name:@"io.sentry.test.TestThread.main" | ||
object:notificationObject]; | ||
[exp fulfill]; | ||
itaybre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}]; | ||
|
||
[threads addObject:thread]; | ||
[thread start]; | ||
} | ||
|
||
[self waitForExpectations:expectations timeout:2]; | ||
itaybre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Suspend the first thread and get its context | ||
TestThread *firstThread = threads[0]; | ||
kern_return_t kr = thread_suspend(firstThread.thread); | ||
XCTAssertTrue(kr == KERN_SUCCESS, @"Thread suspension failed"); | ||
|
||
// Get context for the crashed thread | ||
SentryCrashMC_NEW_CONTEXT(machineContext); | ||
bool result = sentrycrashmc_getContextForThread(firstThread.thread, machineContext, YES); | ||
|
||
XCTAssertTrue(result, @"Failed to get context for thread"); | ||
|
||
// Verify that thread list includes all our test threads | ||
int threadCount = sentrycrashmc_getThreadCount(machineContext); | ||
XCTAssertTrue( | ||
threadCount >= 10, @"Thread count should include all test threads, got %d", threadCount); | ||
XCTAssertTrue(threadCount <= SENTRY_CRASH_MAX_THREADS, | ||
@"Thread count should not exceed maximum of SENTRY_CRASH_MAX_THREADS, got %d", threadCount); | ||
|
||
// Verify that all our threads are in the list | ||
for (TestThread *thread in threads) { | ||
int threadIndex = sentrycrashmc_indexOfThread(machineContext, thread.thread); | ||
XCTAssertTrue(threadIndex >= 0, @"Thread should be found in the list"); | ||
itaybre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Clean up | ||
thread_resume(firstThread.thread); | ||
NSMutableArray<XCTestExpectation *> *finishExpectations = | ||
[NSMutableArray arrayWithCapacity:threads.count]; | ||
for (TestThread *thread in threads) { | ||
thread.endExpectation = | ||
[[XCTestExpectation alloc] initWithDescription:@"Wait for thread to cancel"]; | ||
[finishExpectations addObject:thread.endExpectation]; | ||
[thread cancel]; | ||
itaybre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Wait for all threads to finish (up to 10 seconds) | ||
[self waitForExpectations:finishExpectations timeout:10]; | ||
} | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.