Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann committed Nov 5, 2024
1 parent e216131 commit 8e6d68b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
31 changes: 23 additions & 8 deletions Sources/Sentry/SentryScope.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ - (instancetype)initWithScope:(SentryScope *)scope
[_fingerprintArray addObjectsFromArray:[scope fingerprints]];
[_attachmentArray addObjectsFromArray:[scope attachments]];

self.propagationContext = [[SentryPropagationContext alloc] init];
self.propagationContext = scope.propagationContext;
self.maxBreadcrumbs = scope.maxBreadcrumbs;
self.userObject = scope.userObject.copy;
self.distString = scope.distString;
Expand Down Expand Up @@ -145,11 +145,7 @@ - (void)setSpan:(nullable id<SentrySpan>)span
_span = span;

for (id<SentryScopeObserver> observer in self.observers) {
if (span != nil) {
[observer setTraceContext:[span serialize]];
} else {
[observer setTraceContext:[self.propagationContext traceContextForEvent]];
}
[observer setTraceContext:[self buildTraceContext:span]];
}
}
}
Expand Down Expand Up @@ -461,9 +457,16 @@ - (void)clearAttachments
if (self.extras.count > 0) {
[serializedData setValue:[self extras] forKey:@"extra"];
}
if (self.context.count > 0) {
[serializedData setValue:[self context] forKey:@"context"];

NSDictionary *traceContext = nil;
@synchronized(_spanLock) {
traceContext = [self buildTraceContext:_span];
}

NSMutableDictionary *newContext = [self context].mutableCopy;
newContext[@"trace"] = traceContext;
[serializedData setValue:newContext forKey:@"context"];

[serializedData setValue:[self.userObject serialize] forKey:@"user"];
[serializedData setValue:self.distString forKey:@"dist"];
[serializedData setValue:self.environmentString forKey:@"environment"];
Expand Down Expand Up @@ -609,6 +612,18 @@ - (void)addObserver:(id<SentryScopeObserver>)observer
[self.observers addObject:observer];
}

/**
* Make sure to call this inside @c synchronized(_spanLock) caus this method isn't thread safe.
*/
- (NSDictionary *)buildTraceContext:(id<SentrySpan>)span
{
if (span != nil) {
return [span serialize];
} else {
return [self.propagationContext traceContextForEvent];
}
}

@end

NS_ASSUME_NONNULL_END
11 changes: 9 additions & 2 deletions Tests/SentryTests/SentryScopeSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class SentryScopeSwiftTests: XCTestCase {
transaction = SentryTracer(transactionContext: TransactionContext(name: transactionName, operation: transactionOperation), hub: nil)
}

var contextWithTraceContext: [String: [String: String]] {
var expectedContext = context
expectedContext["trace"] = scope.propagationContext.traceForEvent()
return expectedContext
}

var observer: TestScopeObserver {
return TestScopeObserver()
}
Expand All @@ -87,7 +93,7 @@ class SentryScopeSwiftTests: XCTestCase {
fixture = Fixture()
}

func testSerialize() {
func testSerialize() throws {
let scope = fixture.scope
let actual = scope.serialize()

Expand All @@ -106,7 +112,8 @@ class SentryScopeSwiftTests: XCTestCase {

XCTAssertEqual(["key": "value"], actual["tags"] as? [String: String])
XCTAssertEqual(["key": "value"], actual["extra"] as? [String: String])
XCTAssertEqual(fixture.context, actual["context"] as? [String: [String: String]])

XCTAssertEqual(fixture.contextWithTraceContext, actual["context"] as? [String: [String: String]])

let actualUser = actual["user"] as? [String: Any]
XCTAssertEqual(fixture.ipAddress, actualUser?["ip_address"] as? String)
Expand Down

0 comments on commit 8e6d68b

Please sign in to comment.