Skip to content

Commit

Permalink
test: Set log level to debug for all tests (#2938)
Browse files Browse the repository at this point in the history
Set the test log level to debug when tests are loaded and also set it in clearTestState to get more
insights in test logs.
  • Loading branch information
philipphofmann committed Apr 21, 2023
1 parent 85dbc2a commit 5de0a56
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
33042A1729DC2C4300C60085 /* SentryExtraContextProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */; };
627E7589299F6FE40085504D /* SentryInternalDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = 627E7588299F6FE40085504D /* SentryInternalDefines.h */; };
62885DA729E946B100554F38 /* TestConncurrentModifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62885DA629E946B100554F38 /* TestConncurrentModifications.swift */; };
62B86CFC29F052BB008F3947 /* SentryTestLogConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */; };
62E081A929ED4260000F69FC /* SentryBreadcrumbDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */; };
62E081AB29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */; };
62F226B729A37C120038080D /* SentryBooleanSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F226B629A37C120038080D /* SentryBooleanSerialization.m */; };
Expand Down Expand Up @@ -931,6 +932,7 @@
33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryExtraContextProviderTests.swift; sourceTree = "<group>"; };
627E7588299F6FE40085504D /* SentryInternalDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryInternalDefines.h; path = include/SentryInternalDefines.h; sourceTree = "<group>"; };
62885DA629E946B100554F38 /* TestConncurrentModifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestConncurrentModifications.swift; sourceTree = "<group>"; };
62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryTestLogConfig.m; sourceTree = "<group>"; };
62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryBreadcrumbDelegate.h; path = include/SentryBreadcrumbDelegate.h; sourceTree = "<group>"; };
62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBreadcrumbTestDelegate.swift; sourceTree = "<group>"; };
62F226B629A37C120038080D /* SentryBooleanSerialization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBooleanSerialization.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2983,6 +2985,7 @@
7B4F22DB294089530067EA17 /* FormatHexAddress.swift */,
62F226B829A37C270038080D /* SentryBooleanSerialization.h */,
62F226B629A37C120038080D /* SentryBooleanSerialization.m */,
62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */,
);
path = TestUtils;
sourceTree = "<group>";
Expand Down Expand Up @@ -4256,6 +4259,7 @@
7B82722927A319E900F4BFF4 /* SentryAutoSessionTrackingIntegrationTests.swift in Sources */,
D875ED0B276CC84700422FAC /* SentryNSDataTrackerTests.swift in Sources */,
8ED2D28026A6581C00CA8329 /* NSURLProtocolSwizzle.m in Sources */,
62B86CFC29F052BB008F3947 /* SentryTestLogConfig.m in Sources */,
D808FB92281BF6EC009A2A33 /* SentryUIEventTrackingIntegrationTests.swift in Sources */,
7BC6EC04255C235F0059822A /* SentryFrameTests.swift in Sources */,
0AE455AD28F584D2006680E5 /* SentryReachabilityTests.m in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions SentryTestUtils/ClearTestState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public func clearTestState() {
TestCleanup.clearTestState()
}

public func setTestDefaultLogLevel() {
SentryLog.configure(true, diagnosticLevel: .debug)
}

@objcMembers
class TestCleanup: NSObject {
static func clearTestState() {
Expand All @@ -17,6 +21,8 @@ class TestCleanup: NSObject {
SentrySDK.setAppStartMeasurement(nil)
CurrentDate.setCurrentDateProvider(nil)
SentryNetworkTracker.sharedInstance.disable()

setTestDefaultLogLevel()

#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
let framesTracker = SentryFramesTracker.sharedInstance()
Expand Down
2 changes: 2 additions & 0 deletions SentryTestUtils/SentryTestUtils-ObjC-BridgingHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#import "SentryCrashWrapper.h"
#import "SentryCurrentDate.h"
#import "SentryCurrentDateProvider.h"
#import "SentryDefines.h"
#import "SentryDependencyContainer.h"
#import "SentryDispatchQueueWrapper.h"
#import "SentryDisplayLinkWrapper.h"
#import "SentryEnvelope.h"
#import "SentryFileManager.h"
#import "SentryFramesTracker+TestInit.h"
#import "SentryGlobalEventProcessor.h"
#import "SentryLog.h"
#import "SentryNSProcessInfoWrapper.h"
#import "SentryNSTimerWrapper.h"
#import "SentryNetworkTracker.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class SentryANRTrackerTests: XCTestCase, SentryANRTrackerDelegate {
}

func testAppSuspended_NoANR() {
// To avoid spamming the test logs
SentryLog.configure(true, diagnosticLevel: .error)

anrDetectedExpectation.isInverted = true
fixture.dispatchQueue.blockBeforeMainBlock = {
let delta = self.fixture.timeoutInterval * 2
Expand All @@ -119,6 +122,8 @@ class SentryANRTrackerTests: XCTestCase, SentryANRTrackerDelegate {
start()

wait(for: [anrDetectedExpectation, anrStoppedExpectation], timeout: waitTimeout)

setTestDefaultLogLevel()
}

func testRemoveListener_StopsReportingANRs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class SentryFramesTrackerTests: XCTestCase {
}

func testAllFrames_ConcurrentRead() throws {
// To not spam the test logs
SentryLog.configure(true, diagnosticLevel: .error)

let sut = fixture.sut
sut.start()

Expand All @@ -98,6 +101,8 @@ class SentryFramesTrackerTests: XCTestCase {

group.wait()
try assert(slow: frames, frozen: frames, total: 3 * frames)

setTestDefaultLogLevel()
}

func testPerformanceOfTrackingFrames() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ class SentryPerformanceTrackerTests: XCTestCase {
}

func testStartSpanAsync() {
// To not spam the test logs
SentryLog.configure(true, diagnosticLevel: .error)

let sut = fixture.getSut()
let spanId = startSpan(tracker: sut)
sut.activateSpan(spanId) {
Expand All @@ -302,6 +305,8 @@ class SentryPerformanceTrackerTests: XCTestCase {
for span in spans {
sut.finishSpan(span.key)
}

setTestDefaultLogLevel()
}

func testStackAsync() {
Expand Down
27 changes: 27 additions & 0 deletions Tests/SentryTests/TestUtils/SentryTestLogConfig.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#import "SentryLog.h"
#import <Foundation/Foundation.h>

/**
* Confgures a the sentry log output for testing when this class is loaded.
*/
@interface SentryTestLogConfig : NSObject

+ (void)setDefaultTestLogging;

@end

@implementation SentryTestLogConfig

+ (void)load
{
if (self == [SentryTestLogConfig class]) {
[SentryTestLogConfig setDefaultTestLogging];
}
}

+ (void)setDefaultTestLogging
{
[SentryLog configure:YES diagnosticLevel:kSentryLevelDebug];
}

@end
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Foundation
import SentryTestUtils

func testConcurrentModifications(asyncWorkItems: Int = 5, writeLoopCount: Int = 1_000, writeWork: @escaping (Int) -> Void, readWork: @escaping () -> Void = {}) {
// To not spam the test logs
SentryLog.configure(true, diagnosticLevel: .error)

let queue = DispatchQueue(label: "testConcurrentModifications", qos: .userInteractive, attributes: [.concurrent, .initiallyInactive])
let group = DispatchGroup()

Expand All @@ -20,4 +24,6 @@ func testConcurrentModifications(asyncWorkItems: Int = 5, writeLoopCount: Int =

queue.activate()
group.waitWithTimeout(timeout: 500)

setTestDefaultLogLevel()
}
5 changes: 5 additions & 0 deletions develop-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ To run the unit tests with the thread sanitizer enabled in Xcode click on edit s
* [Diagnosing Memory, Thread, and Crash Issues Early](https://developer.apple.com/documentation/xcode/diagnosing-memory-thread-and-crash-issues-early)
* [Stackoverflow: ThreadSanitizer suppression file with Xcode](https://stackoverflow.com/questions/38251409/how-can-i-suppress-thread-sanitizer-warnings-in-xcode-from-an-external-library)

## Test Logs

The [`SentryTestLogConfig`](https://github.com/getsentry/sentry-cocoa/blob/3a6ab6ec167d2532c024322a0a0019431275d1c1/Tests/SentryTests/TestUtils/SentryTestLogConfig.m) sets the log level to debug in `load`, so we understand what's going on during out tests.
The [`clearTestState`](https://github.com/getsentry/sentry-cocoa/blob/3a6ab6ec167d2532c024322a0a0019431275d1c1/SentryTestUtils/ClearTestState.swift#L25) method does the same, in case a test changes the log level.

## UI Tests

CI runs UI tests on simulators via the `test.yml` workflow, and on devices via `saucelabs-UI-tests.yml`. All are run for each PR, and Sauce Labs tests also run on a nightly cron schedule.
Expand Down

0 comments on commit 5de0a56

Please sign in to comment.