From 596ccc1396714bd5d6daa893912a008d94ba8c10 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Fri, 7 Jul 2023 11:44:10 -0800 Subject: [PATCH] test: speed up some profiling tests (#3134) --- .../SentryProfilerTests/SentryBacktraceTests.mm | 2 +- .../SentrySamplingProfilerTests.mm | 8 +++++--- .../SentryThreadMetadataCacheTests.mm | 16 +++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Tests/SentryProfilerTests/SentryBacktraceTests.mm b/Tests/SentryProfilerTests/SentryBacktraceTests.mm index 5670dd0c97c..2246c610f5b 100644 --- a/Tests/SentryProfilerTests/SentryBacktraceTests.mm +++ b/Tests/SentryProfilerTests/SentryBacktraceTests.mm @@ -235,7 +235,7 @@ - (void)testCollectsMultiThreadBacktrace break; } std::this_thread::sleep_for( - std::chrono::milliseconds(static_cast(std::pow(2, i + 1)) * 1000)); + std::chrono::milliseconds(static_cast(std::pow(2, i + 1)))); } XCTAssertEqual(pthread_cancel(thread1), 0); diff --git a/Tests/SentryProfilerTests/SentrySamplingProfilerTests.mm b/Tests/SentryProfilerTests/SentrySamplingProfilerTests.mm index fc83df86f87..a053ad44315 100644 --- a/Tests/SentryProfilerTests/SentrySamplingProfilerTests.mm +++ b/Tests/SentryProfilerTests/SentrySamplingProfilerTests.mm @@ -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(0)); - XCTAssertGreaterThan(std::chrono::duration_cast(duration).count(), 0); + XCTAssertGreaterThan(getDurationNs(start, getAbsoluteTime()), 0ULL); XCTAssertGreaterThan(profiler->numSamples(), static_cast(0)); XCTAssertGreaterThan(numIdleSamples, 0); } diff --git a/Tests/SentryProfilerTests/SentryThreadMetadataCacheTests.mm b/Tests/SentryProfilerTests/SentryThreadMetadataCacheTests.mm index 567306b4fb6..a606592497c 100644 --- a/Tests/SentryProfilerTests/SentryThreadMetadataCacheTests.mm +++ b/Tests/SentryProfilerTests/SentryThreadMetadataCacheTests.mm @@ -47,7 +47,9 @@ - (void)testRetrievesThreadMetadata SENTRY_PROF_LOG_ERROR_RETURN(pthread_setschedparam(thread, policy, ¶m)); } - 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(); ThreadHandle handle(pthread_mach_thread_np(thread)); @@ -72,7 +74,9 @@ - (void)testReturnsCachedThreadMetadata SENTRY_PROF_LOG_ERROR_RETURN(pthread_setschedparam(thread, policy, ¶m)); } - 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(); ThreadHandle handle(pthread_mach_thread_np(thread)); @@ -94,11 +98,13 @@ - (void)testIgnoresSentryOwnedThreads char name[] = "io.sentry.SentryThreadMetadataCacheTests"; XCTAssertEqual(pthread_create(&thread, nullptr, threadSpin, reinterpret_cast(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(); ThreadHandle handle(pthread_mach_thread_np(thread)); - XCTAssertEqual(cache->metadataForThread(handle).threadID, static_cast(0)); + XCTAssertEqual(cache->metadataForThread(handle).threadID, 0ULL); XCTAssertEqual(pthread_cancel(thread), 0); XCTAssertEqual(pthread_join(thread, nullptr), 0); @@ -120,7 +126,7 @@ - (void)testNonexistentQueueAddressReturnsNoMetadata const auto cache = std::make_shared(); const auto metadata = cache->metadataForQueue(0); - XCTAssertEqual(metadata.address, static_cast(0)); + XCTAssertEqual(metadata.address, 0ULL); XCTAssertEqual(metadata.label, nullptr); }