|
| 1 | +#import <XCTest/XCTest.h> |
| 2 | + |
| 3 | +#import "SentryCrashMachineContext.h" |
| 4 | +#import "SentryCrashMachineContext_Apple.h" |
| 5 | +#import "TestThread.h" |
| 6 | +#import <mach/mach.h> |
| 7 | + |
| 8 | +@interface SentryCrashMachineContextTests : XCTestCase |
| 9 | +@end |
| 10 | + |
| 11 | +@implementation SentryCrashMachineContextTests |
| 12 | + |
| 13 | +- (void)testGetContextForThread_NonCrashedContext_DoesNotPopulateThreadList |
| 14 | +{ |
| 15 | + // Create a test thread |
| 16 | + NSObject *notificationObject = [[NSObject alloc] init]; |
| 17 | + TestThread *thread = [[TestThread alloc] init]; |
| 18 | + thread.notificationObject = notificationObject; |
| 19 | + |
| 20 | + XCTestExpectation *exp = [self expectationWithDescription:@"thread started"]; |
| 21 | + [NSNotificationCenter.defaultCenter |
| 22 | + addObserverForName:@"io.sentry.test.TestThread.main" |
| 23 | + object:notificationObject |
| 24 | + queue:nil |
| 25 | + usingBlock:^(NSNotification *_Nonnull __unused notification) { |
| 26 | + [NSNotificationCenter.defaultCenter |
| 27 | + removeObserver:self |
| 28 | + name:@"io.sentry.test.TestThread.main" |
| 29 | + object:notificationObject]; |
| 30 | + [exp fulfill]; |
| 31 | + }]; |
| 32 | + |
| 33 | + [thread start]; |
| 34 | + [self waitForExpectationsWithTimeout:5 handler:nil]; |
| 35 | + |
| 36 | + kern_return_t kr; |
| 37 | + kr = thread_suspend(thread.thread); |
| 38 | + XCTAssertTrue(kr == KERN_SUCCESS, @"Thread suspension failed"); |
| 39 | + |
| 40 | + // Get context for a non-crashed thread |
| 41 | + SentryCrashMC_NEW_CONTEXT(machineContext); |
| 42 | + bool result = sentrycrashmc_getContextForThread(thread.thread, machineContext, NO); |
| 43 | + |
| 44 | + XCTAssertTrue(result, @"Failed to get context for thread"); |
| 45 | + XCTAssertFalse( |
| 46 | + sentrycrashmc_isCrashedContext(machineContext), @"Should not be marked as crashed context"); |
| 47 | + |
| 48 | + // For non-crashed contexts, thread list should not be populated (will be 0) |
| 49 | + int threadCount = sentrycrashmc_getThreadCount(machineContext); |
| 50 | + XCTAssertEqual( |
| 51 | + threadCount, 0, @"Thread count should be 0 for non-crashed context, got %d", threadCount); |
| 52 | + |
| 53 | + thread_resume(thread.thread); |
| 54 | + XCTestExpectation *expectation = |
| 55 | + [[XCTestExpectation alloc] initWithDescription:@"Wait for thread to cancel"]; |
| 56 | + thread.endExpectation = expectation; |
| 57 | + [thread cancel]; |
| 58 | + [self waitForExpectations:@[ expectation ] timeout:5]; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)testGetContextForThread_WithManyThreads |
| 62 | +{ |
| 63 | + NSInteger numberOfThreads = 10; |
| 64 | + NSMutableArray<TestThread *> *threads = [NSMutableArray arrayWithCapacity:numberOfThreads]; |
| 65 | + NSMutableArray<XCTestExpectation *> *expectations = |
| 66 | + [NSMutableArray arrayWithCapacity:numberOfThreads]; |
| 67 | + |
| 68 | + for (int i = 0; i < numberOfThreads; i++) { |
| 69 | + NSObject *notificationObject = [[NSObject alloc] init]; |
| 70 | + TestThread *thread = [[TestThread alloc] init]; |
| 71 | + thread.notificationObject = notificationObject; |
| 72 | + |
| 73 | + XCTestExpectation *exp = |
| 74 | + [self expectationWithDescription:[NSString stringWithFormat:@"thread %d started", i]]; |
| 75 | + [expectations addObject:exp]; |
| 76 | + |
| 77 | + [NSNotificationCenter.defaultCenter |
| 78 | + addObserverForName:@"io.sentry.test.TestThread.main" |
| 79 | + object:notificationObject |
| 80 | + queue:nil |
| 81 | + usingBlock:^(NSNotification *_Nonnull __unused notification) { |
| 82 | + [NSNotificationCenter.defaultCenter |
| 83 | + removeObserver:self |
| 84 | + name:@"io.sentry.test.TestThread.main" |
| 85 | + object:notificationObject]; |
| 86 | + [exp fulfill]; |
| 87 | + }]; |
| 88 | + |
| 89 | + [threads addObject:thread]; |
| 90 | + [thread start]; |
| 91 | + } |
| 92 | + |
| 93 | + [self waitForExpectations:expectations timeout:5]; |
| 94 | + |
| 95 | + // Suspend the first thread and get its context |
| 96 | + TestThread *firstThread = threads[0]; |
| 97 | + kern_return_t kr = thread_suspend(firstThread.thread); |
| 98 | + XCTAssertTrue(kr == KERN_SUCCESS, @"Thread suspension failed"); |
| 99 | + |
| 100 | + // Get context for the crashed thread |
| 101 | + SentryCrashMC_NEW_CONTEXT(machineContext); |
| 102 | + bool result = sentrycrashmc_getContextForThread(firstThread.thread, machineContext, YES); |
| 103 | + |
| 104 | + XCTAssertTrue(result, @"Failed to get context for thread"); |
| 105 | + |
| 106 | + // Verify that thread list includes all our test threads |
| 107 | + int threadCount = sentrycrashmc_getThreadCount(machineContext); |
| 108 | + XCTAssertTrue( |
| 109 | + threadCount >= 10, @"Thread count should include all test threads, got %d", threadCount); |
| 110 | + XCTAssertTrue(threadCount <= SENTRY_CRASH_MAX_THREADS, |
| 111 | + @"Thread count should not exceed maximum of SENTRY_CRASH_MAX_THREADS, got %d", threadCount); |
| 112 | + |
| 113 | + // Verify that all our threads are in the list |
| 114 | + for (TestThread *thread in threads) { |
| 115 | + int threadIndex = sentrycrashmc_indexOfThread(machineContext, thread.thread); |
| 116 | + XCTAssertTrue(threadIndex >= 0, @"Thread should be found in the list"); |
| 117 | + } |
| 118 | + |
| 119 | + // Clean up |
| 120 | + thread_resume(firstThread.thread); |
| 121 | + NSMutableArray<XCTestExpectation *> *finishExpectations = |
| 122 | + [NSMutableArray arrayWithCapacity:threads.count]; |
| 123 | + for (TestThread *thread in threads) { |
| 124 | + thread.endExpectation = |
| 125 | + [[XCTestExpectation alloc] initWithDescription:@"Wait for thread to cancel"]; |
| 126 | + [finishExpectations addObject:thread.endExpectation]; |
| 127 | + [thread cancel]; |
| 128 | + } |
| 129 | + |
| 130 | + // Wait for all threads to finish (up to 10 seconds) |
| 131 | + [self waitForExpectations:finishExpectations timeout:10]; |
| 132 | +} |
| 133 | + |
| 134 | +@end |
0 commit comments