Skip to content

Commit

Permalink
test: speed up some profiling tests (#3134)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Jul 7, 2023
1 parent 9acdca2 commit 596ccc1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Tests/SentryProfilerTests/SentryBacktraceTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ - (void)testCollectsMultiThreadBacktrace
break;
}
std::this_thread::sleep_for(
std::chrono::milliseconds(static_cast<long long>(std::pow(2, i + 1)) * 1000));
std::chrono::milliseconds(static_cast<long long>(std::pow(2, i + 1))));
}

XCTAssertEqual(pthread_cancel(thread1), 0);
Expand Down
8 changes: 5 additions & 3 deletions Tests/SentryProfilerTests/SentrySamplingProfilerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ - (void)testProfiling
profiler->startSampling([&start] { start = getAbsoluteTime(); });
XCTAssertTrue(profiler->isSampling());

std::this_thread::sleep_for(std::chrono::seconds(3));
// sleep long enough for 2 samples to be collected
const auto sleep = (uint64_t)(2.0 / samplingRateHz * 1000);
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));

profiler->stopSampling();

XCTAssertFalse(profiler->isSampling());

const auto duration = std::chrono::nanoseconds(getDurationNs(start, getAbsoluteTime()));
XCTAssertGreaterThan(start, static_cast<std::uint64_t>(0));
XCTAssertGreaterThan(std::chrono::duration_cast<std::chrono::seconds>(duration).count(), 0);
XCTAssertGreaterThan(getDurationNs(start, getAbsoluteTime()), 0ULL);
XCTAssertGreaterThan(profiler->numSamples(), static_cast<std::uint64_t>(0));
XCTAssertGreaterThan(numIdleSamples, 0);
}
Expand Down
16 changes: 11 additions & 5 deletions Tests/SentryProfilerTests/SentryThreadMetadataCacheTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ - (void)testRetrievesThreadMetadata
SENTRY_PROF_LOG_ERROR_RETURN(pthread_setschedparam(thread, policy, &param));
}

std::this_thread::sleep_for(std::chrono::seconds(1));
// give the other thread a little time to spawn, otherwise its name comes back as an empty
// string and the isSentryOwnedThreadName check will fail
std::this_thread::sleep_for(std::chrono::milliseconds(10));

const auto cache = std::make_shared<ThreadMetadataCache>();
ThreadHandle handle(pthread_mach_thread_np(thread));
Expand All @@ -72,7 +74,9 @@ - (void)testReturnsCachedThreadMetadata
SENTRY_PROF_LOG_ERROR_RETURN(pthread_setschedparam(thread, policy, &param));
}

std::this_thread::sleep_for(std::chrono::seconds(1));
// give the other thread a little time to spawn, otherwise its metadata doesn't come back as
// expected
std::this_thread::sleep_for(std::chrono::milliseconds(10));

const auto cache = std::make_shared<ThreadMetadataCache>();
ThreadHandle handle(pthread_mach_thread_np(thread));
Expand All @@ -94,11 +98,13 @@ - (void)testIgnoresSentryOwnedThreads
char name[] = "io.sentry.SentryThreadMetadataCacheTests";
XCTAssertEqual(pthread_create(&thread, nullptr, threadSpin, reinterpret_cast<void *>(name)), 0);

std::this_thread::sleep_for(std::chrono::seconds(1));
// give the other thread a little time to spawn, otherwise its name comes back as an empty
// string and the isSentryOwnedThreadName check will fail
std::this_thread::sleep_for(std::chrono::milliseconds(10));

const auto cache = std::make_shared<ThreadMetadataCache>();
ThreadHandle handle(pthread_mach_thread_np(thread));
XCTAssertEqual(cache->metadataForThread(handle).threadID, static_cast<unsigned long long>(0));
XCTAssertEqual(cache->metadataForThread(handle).threadID, 0ULL);

XCTAssertEqual(pthread_cancel(thread), 0);
XCTAssertEqual(pthread_join(thread, nullptr), 0);
Expand All @@ -120,7 +126,7 @@ - (void)testNonexistentQueueAddressReturnsNoMetadata
const auto cache = std::make_shared<ThreadMetadataCache>();
const auto metadata = cache->metadataForQueue(0);

XCTAssertEqual(metadata.address, static_cast<unsigned long long>(0));
XCTAssertEqual(metadata.address, 0ULL);
XCTAssertEqual(metadata.label, nullptr);
}

Expand Down

0 comments on commit 596ccc1

Please sign in to comment.