Skip to content

Commit 596ccc1

Browse files
authored
test: speed up some profiling tests (#3134)
1 parent 9acdca2 commit 596ccc1

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

Tests/SentryProfilerTests/SentryBacktraceTests.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ - (void)testCollectsMultiThreadBacktrace
235235
break;
236236
}
237237
std::this_thread::sleep_for(
238-
std::chrono::milliseconds(static_cast<long long>(std::pow(2, i + 1)) * 1000));
238+
std::chrono::milliseconds(static_cast<long long>(std::pow(2, i + 1))));
239239
}
240240

241241
XCTAssertEqual(pthread_cancel(thread1), 0);

Tests/SentryProfilerTests/SentrySamplingProfilerTests.mm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ - (void)testProfiling
4444
profiler->startSampling([&start] { start = getAbsoluteTime(); });
4545
XCTAssertTrue(profiler->isSampling());
4646

47-
std::this_thread::sleep_for(std::chrono::seconds(3));
47+
// sleep long enough for 2 samples to be collected
48+
const auto sleep = (uint64_t)(2.0 / samplingRateHz * 1000);
49+
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
50+
4851
profiler->stopSampling();
4952

5053
XCTAssertFalse(profiler->isSampling());
5154

52-
const auto duration = std::chrono::nanoseconds(getDurationNs(start, getAbsoluteTime()));
5355
XCTAssertGreaterThan(start, static_cast<std::uint64_t>(0));
54-
XCTAssertGreaterThan(std::chrono::duration_cast<std::chrono::seconds>(duration).count(), 0);
56+
XCTAssertGreaterThan(getDurationNs(start, getAbsoluteTime()), 0ULL);
5557
XCTAssertGreaterThan(profiler->numSamples(), static_cast<std::uint64_t>(0));
5658
XCTAssertGreaterThan(numIdleSamples, 0);
5759
}

Tests/SentryProfilerTests/SentryThreadMetadataCacheTests.mm

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ - (void)testRetrievesThreadMetadata
4747
SENTRY_PROF_LOG_ERROR_RETURN(pthread_setschedparam(thread, policy, &param));
4848
}
4949

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

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

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

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

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

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

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

123-
XCTAssertEqual(metadata.address, static_cast<unsigned long long>(0));
129+
XCTAssertEqual(metadata.address, 0ULL);
124130
XCTAssertEqual(metadata.label, nullptr);
125131
}
126132

0 commit comments

Comments
 (0)