Skip to content
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

ref: profile sample mocking #3133

Merged
merged 16 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@
7DC83100239826280043DD9A /* SentryIntegrationProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DC830FF239826280043DD9A /* SentryIntegrationProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
7DC8310A2398283C0043DD9A /* SentryCrashIntegration.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DC831082398283C0043DD9A /* SentryCrashIntegration.h */; };
7DC8310C2398283C0043DD9A /* SentryCrashIntegration.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DC831092398283C0043DD9A /* SentryCrashIntegration.m */; };
84281C432A578E5600EE88F2 /* SentryProfilerState.mm in Sources */ = {isa = PBXBuildFile; fileRef = 84281C422A578E5600EE88F2 /* SentryProfilerState.mm */; };
84281C462A57905700EE88F2 /* SentrySample.h in Headers */ = {isa = PBXBuildFile; fileRef = 84281C442A57905700EE88F2 /* SentrySample.h */; };
84281C472A57905700EE88F2 /* SentrySample.m in Sources */ = {isa = PBXBuildFile; fileRef = 84281C452A57905700EE88F2 /* SentrySample.m */; };
84281C622A579D0700EE88F2 /* SentryProfilerMocksSwiftCompatible.mm in Sources */ = {isa = PBXBuildFile; fileRef = 84281C4D2A579A0C00EE88F2 /* SentryProfilerMocksSwiftCompatible.mm */; };
84281C632A579D0700EE88F2 /* SentryProfilerMocks.mm in Sources */ = {isa = PBXBuildFile; fileRef = 84281C492A57933600EE88F2 /* SentryProfilerMocks.mm */; };
8431EE5B29ADB8EA00D8DC56 /* SentryTimeTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8431EE5A29ADB8EA00D8DC56 /* SentryTimeTests.m */; };
8431EFD129B27B1100D8DC56 /* Sentry.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63AA759B1EB8AEF500D153DE /* Sentry.framework */; settings = {ATTRIBUTES = (Required, ); }; };
8431EFD329B27B1100D8DC56 /* Resources in Resources */ = {isa = PBXBuildFile; fileRef = 630C01951EC341D600C52CEF /* Resources */; };
Expand Down Expand Up @@ -1521,6 +1526,14 @@
7DC831082398283C0043DD9A /* SentryCrashIntegration.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryCrashIntegration.h; path = include/SentryCrashIntegration.h; sourceTree = "<group>"; };
7DC831092398283C0043DD9A /* SentryCrashIntegration.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryCrashIntegration.m; sourceTree = "<group>"; };
8419C0C328C1889D001C8259 /* SentryProfilerSwiftTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryProfilerSwiftTests.swift; sourceTree = "<group>"; };
84281C422A578E5600EE88F2 /* SentryProfilerState.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryProfilerState.mm; sourceTree = "<group>"; };
84281C442A57905700EE88F2 /* SentrySample.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentrySample.h; path = ../include/SentrySample.h; sourceTree = "<group>"; };
84281C452A57905700EE88F2 /* SentrySample.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentrySample.m; sourceTree = "<group>"; };
84281C482A57933600EE88F2 /* SentryProfilerMocks.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryProfilerMocks.h; sourceTree = "<group>"; };
84281C492A57933600EE88F2 /* SentryProfilerMocks.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryProfilerMocks.mm; sourceTree = "<group>"; };
84281C4C2A579A0C00EE88F2 /* SentryProfilerMocksSwiftCompatible.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryProfilerMocksSwiftCompatible.h; sourceTree = "<group>"; };
84281C4D2A579A0C00EE88F2 /* SentryProfilerMocksSwiftCompatible.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryProfilerMocksSwiftCompatible.mm; sourceTree = "<group>"; };
84281C642A57D36100EE88F2 /* SentryProfilerState+ObjCpp.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentryProfilerState+ObjCpp.h"; path = "../include/SentryProfilerState+ObjCpp.h"; sourceTree = "<group>"; };
8431EE5A29ADB8EA00D8DC56 /* SentryTimeTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryTimeTests.m; sourceTree = "<group>"; };
8431EFD929B27B1100D8DC56 /* SentryProfilerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SentryProfilerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
8431EFDA29B27B1200D8DC56 /* SentryTests copy-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "SentryTests copy-Info.plist"; path = "/Users/andrewmcknight/Code/organization/getsentry/repos/public/sentry-cocoa/SentryTests copy-Info.plist"; sourceTree = "<absolute>"; };
Expand Down Expand Up @@ -1556,7 +1569,6 @@
844EDC74294144DB00C86F34 /* SentrySystemWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentrySystemWrapper.h; path = include/SentrySystemWrapper.h; sourceTree = "<group>"; };
844EDC75294144DB00C86F34 /* SentrySystemWrapper.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentrySystemWrapper.mm; sourceTree = "<group>"; };
844EDC7829415AB300C86F34 /* TestSentrySystemWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSentrySystemWrapper.swift; sourceTree = "<group>"; };
844EDC7B2942843400C86F34 /* SentryProfiler+SwiftTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentryProfiler+SwiftTest.h"; path = "Tests/SentryTests/Helper/SentryProfiler+SwiftTest.h"; sourceTree = SOURCE_ROOT; };
844EDCE32947DC3100C86F34 /* SentryNSTimerFactory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryNSTimerFactory.h; path = include/SentryNSTimerFactory.h; sourceTree = "<group>"; };
844EDCE42947DC3100C86F34 /* SentryNSTimerFactory.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryNSTimerFactory.m; sourceTree = "<group>"; };
844EDCE72947DCD700C86F34 /* TestSentryNSTimerFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSentryNSTimerFactory.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2012,6 +2024,7 @@
84C47B2B2A09239100DAEB8A /* .codecov.yml */,
844DA80628246D5000E6B62E /* .craft.yml */,
844DA80A28246D5000E6B62E /* .swiftlint.yml */,
84281C552A579C2B00EE88F2 /* SentryTestUtilsObjc */,
6304360C1EC05CEF00C4D3FA /* Frameworks */,
6327C5D41EB8A783004E799B /* Products */,
63AA756E1EB8AEDB00D153DE /* Sources */,
Expand Down Expand Up @@ -3086,41 +3099,52 @@
8405A517279906EF001B38A1 /* Profiling */ = {
isa = PBXGroup;
children = (
84354E0F29BF944900CDBB8B /* SentryProfileTimeseries.h */,
84354E1029BF944900CDBB8B /* SentryProfileTimeseries.mm */,
03F84D1327DD414C008FE43F /* SentryAsyncSafeLogging.h */,
03F84D1227DD414C008FE43F /* SentryBacktrace.hpp */,
03F84D3127DD4191008FE43F /* SentryBacktrace.cpp */,
03F84D1227DD414C008FE43F /* SentryBacktrace.hpp */,
03F84D1827DD414C008FE43F /* SentryCompiler.h */,
03F84D1C27DD414C008FE43F /* SentryCPU.h */,
03F84D1B27DD414C008FE43F /* SentryMachLogging.hpp */,
03F84D2C27DD4191008FE43F /* SentryMachLogging.cpp */,
03F84D1127DD414C008FE43F /* SentryProfiler.h */,
03F84D1B27DD414C008FE43F /* SentryMachLogging.hpp */,
844EDD6B2949387000C86F34 /* SentryMetricProfiler.h */,
8454CF8B293EAF9A006AC140 /* SentryMetricProfiler.mm */,
0354A22A2A134D9C003C3A04 /* SentryProfilerState.h */,
84A888FC28D9B11700C51DFD /* SentryProfiler+Test.h */,
03F84D1127DD414C008FE43F /* SentryProfiler.h */,
03F84D2B27DD4191008FE43F /* SentryProfiler.mm */,
84A888FC28D9B11700C51DFD /* SentryProfiler+Test.h */,
0354A22A2A134D9C003C3A04 /* SentryProfilerState.h */,
84281C642A57D36100EE88F2 /* SentryProfilerState+ObjCpp.h */,
84281C422A578E5600EE88F2 /* SentryProfilerState.mm */,
0356A56E288B4612008BF593 /* SentryProfilesSampler.h */,
0356A56F288B4612008BF593 /* SentryProfilesSampler.m */,
84354E0F29BF944900CDBB8B /* SentryProfileTimeseries.h */,
84354E1029BF944900CDBB8B /* SentryProfileTimeseries.mm */,
03BCC38D27E2A377003232C7 /* SentryProfilingConditionals.h */,
03F84D2927DD416B008FE43F /* SentryProfilingLogging.hpp */,
03F84D2F27DD4191008FE43F /* SentryProfilingLogging.mm */,
03F84D1527DD414C008FE43F /* SentrySamplingProfiler.hpp */,
84281C442A57905700EE88F2 /* SentrySample.h */,
84281C452A57905700EE88F2 /* SentrySample.m */,
03F84D3027DD4191008FE43F /* SentrySamplingProfiler.cpp */,
03F84D1727DD414C008FE43F /* SentryThreadHandle.hpp */,
03F84D2E27DD4191008FE43F /* SentryThreadHandle.cpp */,
03F84D1A27DD414C008FE43F /* SentryThreadMetadataCache.hpp */,
03F84D2D27DD4191008FE43F /* SentryThreadMetadataCache.cpp */,
03F84D1527DD414C008FE43F /* SentrySamplingProfiler.hpp */,
03F84D1427DD414C008FE43F /* SentryStackBounds.hpp */,
03F84D1627DD414C008FE43F /* SentryStackFrame.hpp */,
03F84D2E27DD4191008FE43F /* SentryThreadHandle.cpp */,
03F84D1727DD414C008FE43F /* SentryThreadHandle.hpp */,
03F84D2D27DD4191008FE43F /* SentryThreadMetadataCache.cpp */,
03F84D1A27DD414C008FE43F /* SentryThreadMetadataCache.hpp */,
03F84D1927DD414C008FE43F /* SentryThreadState.hpp */,
03BCC38927E1BF49003232C7 /* SentryTime.h */,
03BCC38B27E1C01A003232C7 /* SentryTime.mm */,
0356A56E288B4612008BF593 /* SentryProfilesSampler.h */,
0356A56F288B4612008BF593 /* SentryProfilesSampler.m */,
);
path = Profiling;
sourceTree = "<group>";
};
84281C552A579C2B00EE88F2 /* SentryTestUtilsObjc */ = {
isa = PBXGroup;
children = (
);
path = SentryTestUtilsObjc;
sourceTree = "<group>";
};
8431EFDB29B27B3D00D8DC56 /* SentryProfilerTests */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3153,6 +3177,10 @@
children = (
7BD47B4C268F0B080076A663 /* ClearTestState.swift */,
84AC61D829F7643B009EEF61 /* TestDispatchFactory.swift */,
84281C482A57933600EE88F2 /* SentryProfilerMocks.h */,
84281C492A57933600EE88F2 /* SentryProfilerMocks.mm */,
84281C4C2A579A0C00EE88F2 /* SentryProfilerMocksSwiftCompatible.h */,
84281C4D2A579A0C00EE88F2 /* SentryProfilerMocksSwiftCompatible.mm */,
84AC61DA29F7654A009EEF61 /* TestDispatchSourceWrapper.swift */,
84A5D75A29D5170700388BFA /* TimeInterval+Sentry.swift */,
7B30B68126527C55006B2752 /* TestDisplayLinkWrapper.swift */,
Expand Down Expand Up @@ -3564,6 +3592,7 @@
D8603DD8284F894C000E1227 /* SentryBaggage.h in Headers */,
03F84D2127DD414C008FE43F /* SentrySamplingProfiler.hpp in Headers */,
84AC61D229F7541E009EEF61 /* SentryDispatchSourceWrapper.h in Headers */,
84281C462A57905700EE88F2 /* SentrySample.h in Headers */,
63FE712B20DA4C1100CDBAE8 /* SentryCrashStackCursor.h in Headers */,
D8C67E9C28000E24007E326E /* SentryScreenshot.h in Headers */,
7BA61CBF247CEA8100C130A8 /* SentryFormatter.h in Headers */,
Expand Down Expand Up @@ -4091,6 +4120,7 @@
7BB65501253DC1B500887E87 /* SentryUserFeedback.m in Sources */,
7D5C441A237C2E1F00DAB0A3 /* SentrySDK.m in Sources */,
7D65260E237F649E00113EA2 /* SentryScope.m in Sources */,
84281C472A57905700EE88F2 /* SentrySample.m in Sources */,
84AC61D329F7541E009EEF61 /* SentryDispatchSourceWrapper.m in Sources */,
63FE712D20DA4C1100CDBAE8 /* SentryCrashJSONCodecObjC.m in Sources */,
7BBD18932449BEDD00427C76 /* SentryDefaultRateLimits.m in Sources */,
Expand Down Expand Up @@ -4131,6 +4161,7 @@
7BC9A20428F4166D001E7C4C /* SentryMeasurementValue.m in Sources */,
D859696B27BECD8F0036A46E /* SentryCoreDataTrackingIntegration.m in Sources */,
7BD86EC7264A641D005439DB /* SentrySysctl.m in Sources */,
84281C432A578E5600EE88F2 /* SentryProfilerState.mm in Sources */,
D859697327BECDD20036A46E /* SentryCoreDataSwizzling.m in Sources */,
639889BD1EDED18400EA7442 /* SentrySwizzle.m in Sources */,
D8ABB0BC29264275005D1E24 /* Sentry.swift in Sources */,
Expand Down Expand Up @@ -4431,6 +4462,7 @@
84AC61D929F7643B009EEF61 /* TestDispatchFactory.swift in Sources */,
8431F01929B2852D00D8DC56 /* Invocation.swift in Sources */,
84B7FA4629B2935F00AD93B1 /* ClearTestState.swift in Sources */,
84281C622A579D0700EE88F2 /* SentryProfilerMocksSwiftCompatible.mm in Sources */,
8431F01529B2851500D8DC56 /* TestSentryNSTimerFactory.swift in Sources */,
84B7FA3C29B2876F00AD93B1 /* TestConstants.swift in Sources */,
8431F01A29B2852D00D8DC56 /* Dynamic.swift in Sources */,
Expand All @@ -4442,6 +4474,7 @@
84B7FA4529B2926900AD93B1 /* TestDisplayLinkWrapper.swift in Sources */,
84AC61DB29F7654A009EEF61 /* TestDispatchSourceWrapper.swift in Sources */,
8431F01729B2851500D8DC56 /* TestSentrySystemWrapper.swift in Sources */,
84281C632A579D0700EE88F2 /* SentryProfilerMocks.mm in Sources */,
84B7FA4129B28CD200AD93B1 /* TestSentryDispatchQueueWrapper.swift in Sources */,
84B7FA3E29B28ADD00AD93B1 /* TestClient.swift in Sources */,
);
Expand Down
18 changes: 18 additions & 0 deletions SentryTestUtils/SentryProfilerMocks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#import "SentryProfilingConditionals.h"
#import <Foundation/Foundation.h>

#if SENTRY_TARGET_PROFILING_SUPPORTED

# import "SentryBacktrace.hpp"

using namespace sentry::profiling;

NS_ASSUME_NONNULL_BEGIN

Backtrace mockBacktrace(thread::TIDType threadID, const int threadPriority,
const char *_Nullable threadName, std::uint64_t queueAddress, std::string queueLabel,
std::vector<std::uintptr_t> addresses);

NS_ASSUME_NONNULL_END

#endif // SENTRY_TARGET_PROFILING_SUPPORTED
28 changes: 28 additions & 0 deletions SentryTestUtils/SentryProfilerMocks.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#import "SentryProfilerMocks.h"

#if SENTRY_TARGET_PROFILING_SUPPORTED

Backtrace
mockBacktrace(thread::TIDType threadID, const int threadPriority, const char *threadName,
std::uint64_t queueAddress, std::string queueLabel, std::vector<std::uintptr_t> addresses)
{
ThreadMetadata threadMetadata;
if (threadName != nullptr) {
threadMetadata.name = threadName;
}
threadMetadata.threadID = threadID;
threadMetadata.priority = threadPriority;

QueueMetadata queueMetadata;
queueMetadata.address = queueAddress;
queueMetadata.label = std::make_shared<std::string>(queueLabel);

Backtrace backtrace;
backtrace.threadMetadata = threadMetadata;
backtrace.queueMetadata = queueMetadata;
backtrace.addresses = std::vector<std::uintptr_t>(addresses);

return backtrace;
}

#endif // SENTRY_TARGET_PROFILING_SUPPORTED
28 changes: 28 additions & 0 deletions SentryTestUtils/SentryProfilerMocksSwiftCompatible.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#import "SentryProfilingConditionals.h"
#import <Foundation/Foundation.h>

#if SENTRY_TARGET_PROFILING_SUPPORTED

@class SentryProfilerState;

NS_ASSUME_NONNULL_BEGIN

/**
* This delivers a wrapper around the C++ function to create a mock backtrace for incorporation into
* profiler state that can be called from Swift tests.
*/
@interface SentryProfilerMocksSwiftCompatible : NSObject
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Mocks are usually test doubles that simulate the behavior of a real object with the added ability to verify interactions with that object. They are used to set expectations and assert that specific methods or properties of the mocked object are called or accessed correctly during testing. What you define here is a fake. Fakes are simplified implementations of a real object that mimics its behavior to some extent. They are typically used to provide a functional substitute for a real object that is too complex, slow, or unavailable for testing.

TLDR; if you want to follow the correct testing naming terms, we should rename all the mocks in this PR to fake, but I'm also fine with keeping it. I don't want to be nitpicking on testing terms.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but I think this is more of a mock. I think we should be careful introducing too much terminology (and therefore cognitive overhead) like fixture, double, oracle, stub, mock, fake, dummy, spy, robot... I don't think these are all standardized terms in industry.

Since this is just creating structs to use as input to a real implementation, and not actually providing an alternate implementation of an interface, there is really no doubling going on at all, so in an OOP sense, this is neither mocking nor stubbing (nor faking).

I think the intention of the code gets across so I'm hesitant to change it. We're providing predefined input to the SUT and testing the output of the system.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me 👍 .


+ (void)appendMockBacktraceToState:(SentryProfilerState *)state
threadID:(uint64_t)threadID
threadPriority:(const int)threadPriority
threadName:(nullable NSString *)threadName
queueAddress:(uint64_t)queueAddress
queueLabel:(NSString *)queueLabel
addresses:(NSArray<NSNumber *> *)addresses;

@end

NS_ASSUME_NONNULL_END

#endif // SENTRY_TARGET_PROFILING_SUPPORTED
37 changes: 37 additions & 0 deletions SentryTestUtils/SentryProfilerMocksSwiftCompatible.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#import "SentryProfilerMocksSwiftCompatible.h"

#if SENTRY_TARGET_PROFILING_SUPPORTED

# import "SentryCurrentDate.h"
# import "SentryProfilerMocks.h"
# import "SentryProfilerState+ObjCpp.h"
# include <vector>

using namespace std;

@implementation SentryProfilerMocksSwiftCompatible

+ (void)appendMockBacktraceToState:(SentryProfilerState *)state
threadID:(uint64_t)threadID
threadPriority:(const int)threadPriority
threadName:(nullable NSString *)threadName
queueAddress:(uint64_t)queueAddress
queueLabel:(NSString *)queueLabel
addresses:(NSArray<NSNumber *> *)addresses
{
auto backtraceAddresses = std::vector<std::uintptr_t>();

for (NSNumber *address in addresses) {
backtraceAddresses.push_back(address.unsignedLongLongValue);
}

auto backtrace = mockBacktrace(threadID, threadPriority,
[threadName cStringUsingEncoding:NSUTF8StringEncoding], queueAddress,
[queueLabel cStringUsingEncoding:NSUTF8StringEncoding], backtraceAddresses);
backtrace.absoluteTimestamp = SentryCurrentDate.getCurrentDateProvider.systemTime;
[state appendBacktrace:backtrace];
}

@end

#endif // SENTRY_TARGET_PROFILING_SUPPORTED
Loading