Skip to content

Commit

Permalink
Merge pull request #1255 from planetary-social/fix-intermittent-test-…
Browse files Browse the repository at this point in the history
…failures

Fix intermittent test failures
  • Loading branch information
mplorentz authored Jun 24, 2024
2 parents d51c987 + a8aa5e2 commit 25573dc
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 132 deletions.
10 changes: 9 additions & 1 deletion Nos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@
C9ADB14129951CB10075E7F8 /* NSManagedObject+Nos.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9ADB14029951CB10075E7F8 /* NSManagedObject+Nos.swift */; };
C9ADB14229951CB10075E7F8 /* NSManagedObject+Nos.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9ADB14029951CB10075E7F8 /* NSManagedObject+Nos.swift */; };
C9B597652BBC8300002EC76A /* ImagePickerUIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B597642BBC8300002EC76A /* ImagePickerUIViewController.swift */; };
C9B5C78E2C24AF650070445B /* MockRelaySubscriptionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0320C1142BFE63DC00C4C080 /* MockRelaySubscriptionManager.swift */; };
C9B678DB29EEBF3B00303F33 /* DependencyInjection.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B678DA29EEBF3B00303F33 /* DependencyInjection.swift */; };
C9B678DC29EEBF3B00303F33 /* DependencyInjection.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B678DA29EEBF3B00303F33 /* DependencyInjection.swift */; };
C9B678DE29EEC35B00303F33 /* Foundation+Sendable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B678DD29EEC35B00303F33 /* Foundation+Sendable.swift */; };
Expand Down Expand Up @@ -429,6 +430,8 @@
C9EE3E602A0538B7008A7491 /* ExpirationTimeButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9EE3E5F2A0538B7008A7491 /* ExpirationTimeButton.swift */; };
C9EE3E632A053910008A7491 /* ExpirationTimeOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9EE3E622A053910008A7491 /* ExpirationTimeOption.swift */; };
C9EE3E642A053910008A7491 /* ExpirationTimeOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9EE3E622A053910008A7491 /* ExpirationTimeOption.swift */; };
C9EF84CF2C24D63000182B6F /* MockRelayService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9EF84CE2C24D63000182B6F /* MockRelayService.swift */; };
C9EF84D02C24D63000182B6F /* MockRelayService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9EF84CE2C24D63000182B6F /* MockRelayService.swift */; };
C9F0BB6929A5039D000547FC /* Int+Bool.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9F0BB6829A5039D000547FC /* Int+Bool.swift */; };
C9F0BB6B29A503D6000547FC /* PublicKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9F0BB6A29A503D6000547FC /* PublicKey.swift */; };
C9F0BB6C29A503D6000547FC /* PublicKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9F0BB6A29A503D6000547FC /* PublicKey.swift */; };
Expand Down Expand Up @@ -807,6 +810,7 @@
C9E8C1142B081EBE002D46B0 /* NIP05View.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NIP05View.swift; sourceTree = "<group>"; };
C9EE3E5F2A0538B7008A7491 /* ExpirationTimeButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpirationTimeButton.swift; sourceTree = "<group>"; };
C9EE3E622A053910008A7491 /* ExpirationTimeOption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpirationTimeOption.swift; sourceTree = "<group>"; };
C9EF84CE2C24D63000182B6F /* MockRelayService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockRelayService.swift; sourceTree = "<group>"; };
C9F0BB6829A5039D000547FC /* Int+Bool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+Bool.swift"; sourceTree = "<group>"; };
C9F0BB6A29A503D6000547FC /* PublicKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PublicKey.swift; sourceTree = "<group>"; };
C9F0BB6E29A50437000547FC /* NostrConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrConstants.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -900,7 +904,6 @@
isa = PBXGroup;
children = (
0320C0FA2BFE43A600C4C080 /* RelayServiceTests.swift */,
0320C1142BFE63DC00C4C080 /* MockRelaySubscriptionManager.swift */,
);
path = Relay;
sourceTree = "<group>";
Expand Down Expand Up @@ -1180,6 +1183,8 @@
C959DB752BD01DF4008F3627 /* GiftWrapper.swift */,
A3B943CE299AE00100A15A08 /* KeyChain.swift */,
C9F64D8B29ED840700563F2B /* LogHelper.swift */,
C9EF84CE2C24D63000182B6F /* MockRelayService.swift */,
0320C1142BFE63DC00C4C080 /* MockRelaySubscriptionManager.swift */,
5BC0D9CB2B867B9D005D6980 /* NamesAPI.swift */,
C936B4612A4CB01C00DF1EB9 /* PushNotificationService.swift */,
C9A8015D2BD0177D006E29B2 /* ReportPublisher.swift */,
Expand Down Expand Up @@ -1909,6 +1914,7 @@
C9A8015E2BD0177D006E29B2 /* ReportPublisher.swift in Sources */,
C973364F2A7968220012D8B8 /* SetUpUNSBanner.swift in Sources */,
C9F0BB6B29A503D6000547FC /* PublicKey.swift in Sources */,
C9EF84CF2C24D63000182B6F /* MockRelayService.swift in Sources */,
5B79F6192B98B24C002DA9BE /* DeleteUsernameWizard.swift in Sources */,
C9EE3E602A0538B7008A7491 /* ExpirationTimeButton.swift in Sources */,
A303AF8329A9153A005DC8FC /* FollowButton.swift in Sources */,
Expand Down Expand Up @@ -2029,6 +2035,7 @@
C936B4622A4CB01C00DF1EB9 /* PushNotificationService.swift in Sources */,
C95D68A6299E6F9E00429F86 /* ProfileHeader.swift in Sources */,
C9BAB09B2996FBA10003A84E /* EventProcessor.swift in Sources */,
C9B5C78E2C24AF650070445B /* MockRelaySubscriptionManager.swift in Sources */,
C960C57129F3236200929990 /* LikeButton.swift in Sources */,
C97797B9298AA19A0046BD25 /* RelayService.swift in Sources */,
C99721CB2AEBED26004EBEAB /* String+Empty.swift in Sources */,
Expand Down Expand Up @@ -2099,6 +2106,7 @@
C9A0DAF929C92F4500466635 /* UNSAPI.swift in Sources */,
C97A1C8C29E45B4E009D9E8D /* RawEventController.swift in Sources */,
CD09A75F29A521FD0063464F /* RelayService.swift in Sources */,
C9EF84D02C24D63000182B6F /* MockRelayService.swift in Sources */,
CD09A76029A521FD0063464F /* Filter.swift in Sources */,
C9B71DC32A9003670031ED9F /* CrashReporting.swift in Sources */,
5B08A1E12A1FDFF700EB8F2E /* TLV.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions Nos/Service/DependencyInjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ fileprivate enum RouterKey: DependencyKey {

private enum RelayServiceKey: DependencyKey {
static let liveValue = RelayService()
static let testValue = RelayService()
static let previewValue = RelayService()
static let testValue: RelayService = MockRelayService()
static let previewValue: RelayService = MockRelayService()
}

fileprivate enum PushNotificationServiceKey: DependencyKey {
Expand Down
6 changes: 6 additions & 0 deletions Nos/Service/MockRelayService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// A version of the relay service that won't talk to real relays.
class MockRelayService: RelayService {
init() {
super.init(subscriptionManager: MockRelaySubscriptionManager())
}
}
2 changes: 1 addition & 1 deletion Nos/Service/Relay/RelayService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit

/// A service that maintains connections to Nostr Relay servers and executes requests for data from those relays
/// in the form of `Filters` and `RelaySubscription`s.
final class RelayService: ObservableObject {
class RelayService: ObservableObject {

private var subscriptionManager: RelaySubscriptionManager
private var processSubscriptionQueueTimer: AsyncTimer?
Expand Down
1 change: 1 addition & 0 deletions NosTests/Controller/ContentWarningControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ final class ContentWarningControllerTests: CoreDataTestCase {
// swiftlint:disable:next implicitly_unwrapped_optional
var fixture: PreviewData!

@MainActor
override func setUp() async throws {
try await super.setUp()
fixture = PreviewData()
Expand Down
12 changes: 6 additions & 6 deletions NosTests/Controller/SocialGraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Dependencies

final class SocialGraphTests: CoreDataTestCase {

func testEmpty() async throws {
@MainActor func testEmpty() async throws {
// Arrange
_ = try Author.findOrCreate(by: KeyFixture.alice.publicKeyHex, context: testContext)

Expand All @@ -17,7 +17,7 @@ final class SocialGraphTests: CoreDataTestCase {
}

/// The `XCTExpectFailure` below does _not_ work in CI. That is, when the test fails, CI still fails.
func testOneFollower() async throws {
@MainActor func testOneFollower() async throws {
XCTExpectFailure("This test is failing intermittently, see #671", options: .nonStrict())

// Arrange
Expand All @@ -42,7 +42,7 @@ final class SocialGraphTests: CoreDataTestCase {
}

/// The `XCTExpectFailure` below does _not_ work in CI. That is, when the test fails, CI still fails.
func testFollow() async throws {
@MainActor func testFollow() async throws {
XCTExpectFailure("This test is failing intermittently, see #671", options: .nonStrict())

// Arrange
Expand All @@ -69,7 +69,7 @@ final class SocialGraphTests: CoreDataTestCase {
}

/// The `XCTExpectFailure` below does _not_ work in CI. That is, when the test fails, CI still fails.
func testTwoFollows() async throws {
@MainActor func testTwoFollows() async throws {
XCTExpectFailure("This test is failing intermittently, see #671", options: .nonStrict())

// Arrange
Expand Down Expand Up @@ -106,7 +106,7 @@ final class SocialGraphTests: CoreDataTestCase {
try await eventually { await sut.followedKeys == expectedKeys }
}

func testTwoHops() async throws {
@MainActor func testTwoHops() async throws {
// Arrange
let alice = try Author.findOrCreate(by: KeyFixture.alice.publicKeyHex, context: testContext)
let bob = try Author.findOrCreate(by: KeyFixture.bob.publicKeyHex, context: testContext)
Expand Down Expand Up @@ -143,7 +143,7 @@ final class SocialGraphTests: CoreDataTestCase {
try await eventually { await sut.isInNetwork(KeyFixture.eve.publicKeyHex) }
}

func testOutOfNetwork() async throws {
@MainActor func testOutOfNetwork() async throws {
// Arrange
_ = try Author.findOrCreate(by: KeyFixture.alice.publicKeyHex, context: testContext)
_ = try Author.findOrCreate(by: KeyFixture.bob.publicKeyHex, context: testContext)
Expand Down
6 changes: 3 additions & 3 deletions NosTests/Integration/EventProcessorIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EventProcessorIntegrationTests: CoreDataTestCase {
XCTAssertEqual(sampleEvent.createdAt?.timeIntervalSince1970, 1_674_624_689)
}

func testParseSampleRepliesAndFetchReplies() throws {
@MainActor func testParseSampleRepliesAndFetchReplies() throws {
// Arrange
let sampleData = try jsonData(filename: "sample_replies")
let sampleEventID = "57b994eb5903d37ee11d507872611eec843098d24eb5d21a1678983dffd92b86"
Expand Down Expand Up @@ -119,7 +119,7 @@ class EventProcessorIntegrationTests: CoreDataTestCase {
XCTAssertEqual(follow.petName, sampleName)
}

func testParseContactListIgnoresInvalidKeys() throws {
@MainActor func testParseContactListIgnoresInvalidKeys() throws {
// Arrange
let jsonData = try jsonData(filename: "bad_contact_list")
let jsonEvent = try JSONDecoder().decode(JSONEvent.self, from: jsonData)
Expand Down Expand Up @@ -231,7 +231,7 @@ class EventProcessorIntegrationTests: CoreDataTestCase {
// MARK: - Stub

/// Verifies that when we see an event we already have in Core Data as a stub it is updated correctly.
func testParsingEventStub() throws {
@MainActor func testParsingEventStub() throws {
let referencingJSONEvent = JSONEvent(
id: "1",
pubKey: KeyFixture.alice.publicKeyHex,
Expand Down
12 changes: 6 additions & 6 deletions NosTests/Model/AuthorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class AuthorTests: CoreDataTestCase {
XCTAssertEqual(Set(author.followedKeys), Set(expectedFollowedKeys))
}

func testFollowedKeysIgnoresInvalidKeys() throws {
@MainActor func testFollowedKeysIgnoresInvalidKeys() throws {
// inject bad data into the database
let user = try Author.findOrCreate(by: "user", context: testContext)
let followee = try Author.findOrCreate(by: "followee", context: testContext)
Expand Down Expand Up @@ -148,7 +148,7 @@ final class AuthorTests: CoreDataTestCase {

// MARK: Fetch requests

func test_knownFollowers_givenMultipleFollowers() throws {
@MainActor func test_knownFollowers_givenMultipleFollowers() throws {
// Arrange
let alice = try Author.findOrCreate(by: "alice", context: testContext)
let bob = try Author.findOrCreate(by: "bob", context: testContext)
Expand All @@ -173,7 +173,7 @@ final class AuthorTests: CoreDataTestCase {
XCTAssertEqual(try testContext.fetch(alice.knownFollowers(of: alice)), [])
}

func test_knownFollowers_givenFollowCircle() throws {
@MainActor func test_knownFollowers_givenFollowCircle() throws {
// Arrange
let alice = try Author.findOrCreate(by: "alice", context: testContext)
let bob = try Author.findOrCreate(by: "bob", context: testContext)
Expand All @@ -196,7 +196,7 @@ final class AuthorTests: CoreDataTestCase {
XCTAssertEqual(try testContext.fetch(alice.knownFollowers(of: alice)), [])
}

func test_knownFollowers_givenSelfFollow() throws {
@MainActor func test_knownFollowers_givenSelfFollow() throws {
// Arrange
let alice = try Author.findOrCreate(by: "alice", context: testContext)
let eve = try Author.findOrCreate(by: "eve", context: testContext)
Expand All @@ -213,7 +213,7 @@ final class AuthorTests: CoreDataTestCase {
XCTAssertEqual(try testContext.fetch(alice.knownFollowers(of: eve)), [])
}

func test_allPostsRequest_onlyRootPosts() throws {
@MainActor func test_allPostsRequest_onlyRootPosts() throws {
// Arrange
let publicKey = "test"
_ = try EventFixture.build(in: testContext, publicKey: publicKey, deletedOn: [Relay(context: testContext)])
Expand All @@ -227,7 +227,7 @@ final class AuthorTests: CoreDataTestCase {
XCTAssertEqual(events.count, 0)
}

func test_outOfNetwork_givenCircleOfFollows() throws {
@MainActor func test_outOfNetwork_givenCircleOfFollows() throws {
// Arrange
let alice = try Author.findOrCreate(by: "alice", context: testContext)
let bob = try Author.findOrCreate(by: "bob", context: testContext)
Expand Down
26 changes: 13 additions & 13 deletions NosTests/Model/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Dependencies
/// Tests for the Event model.
final class EventTests: CoreDataTestCase {
// MARK: - Serialization
func testSerializedEventForSigning() throws {
@MainActor func testSerializedEventForSigning() throws {
// Arrange
let tags = [["p", "d0a1ffb8761b974cec4a3be8cbcb2e96a7090dcf465ffeac839aa4ca20c9a59e"]]
let content = "Testing nos #[0]"
Expand All @@ -28,7 +28,7 @@ final class EventTests: CoreDataTestCase {

// MARK: - Identifier calculation

func testIdentifierCalculation() throws {
@MainActor func testIdentifierCalculation() throws {
// Arrange
let tags = [["p", "d0a1ffb8761b974cec4a3be8cbcb2e96a7090dcf465ffeac839aa4ca20c9a59e"]]
let content = "Testing nos #[0]"
Expand All @@ -41,7 +41,7 @@ final class EventTests: CoreDataTestCase {
)
}

func testIdentifierCalculationWithEmptyAndNoTags() throws {
@MainActor func testIdentifierCalculationWithEmptyAndNoTags() throws {
// Arrange
let content = "Testing nos #[0]"
let nilTagsEvent = try EventFixture.build(in: testContext, content: content, tags: nil)
Expand All @@ -63,7 +63,7 @@ final class EventTests: CoreDataTestCase {
/// Verifies that we can sign an event and verify it.
/// Since Schnorr signatures are non-deterministic we can't assert on constants. That's why all this test really
/// does is verify that we are internally consistent in our signature logic.
func testSigningAndVerification() throws {
@MainActor func testSigningAndVerification() throws {
// Arrange
let event = try EventFixture.build(in: testContext)

Expand All @@ -74,7 +74,7 @@ final class EventTests: CoreDataTestCase {
XCTAssert(try event.verifySignature(for: KeyFixture.keyPair.publicKey))
}

func testVerificationOnBadId() throws {
@MainActor func testVerificationOnBadId() throws {
// Arrange
let event = try EventFixture.build(in: testContext)

Expand All @@ -86,7 +86,7 @@ final class EventTests: CoreDataTestCase {
XCTAssertFalse(try event.verifySignature(for: KeyFixture.keyPair.publicKey))
}

func testVerificationOnBadSignature() throws {
@MainActor func testVerificationOnBadSignature() throws {
// Arrange
let event = try EventFixture.build(in: testContext)
event.identifier = try event.calculateIdentifier()
Expand All @@ -99,7 +99,7 @@ final class EventTests: CoreDataTestCase {
XCTAssertFalse(try event.verifySignature(for: KeyFixture.keyPair.publicKey))
}

func testFetchEventByIDPerformance() throws {
@MainActor func testFetchEventByIDPerformance() throws {
let testEvent = try EventFixture.build(in: testContext)
testEvent.identifier = try testEvent.calculateIdentifier()
let eventID = testEvent.identifier!
Expand All @@ -113,7 +113,7 @@ final class EventTests: CoreDataTestCase {

// MARK: - Replies

func testReferencedNoteGivenMentionMarker() throws {
@MainActor func testReferencedNoteGivenMentionMarker() throws {
let testEvent = try EventFixture.build(in: testContext)

let mention = try EventReference(
Expand All @@ -125,7 +125,7 @@ final class EventTests: CoreDataTestCase {
XCTAssertNil(testEvent.referencedNote())
}

func testRepostedNote() throws {
@MainActor func testRepostedNote() throws {
let testEvent = try EventFixture.build(in: testContext)
testEvent.kind = 6

Expand All @@ -141,7 +141,7 @@ final class EventTests: CoreDataTestCase {
)
}

func testRepostedNoteGivenNonRepost() throws {
@MainActor func testRepostedNoteGivenNonRepost() throws {
let testEvent = try EventFixture.build(in: testContext)
testEvent.kind = 1

Expand All @@ -156,7 +156,7 @@ final class EventTests: CoreDataTestCase {

// MARK: - Fetch requests

func test_eventByIdentifierSeenOnRelay_givenAlreadySeen() throws {
@MainActor func test_eventByIdentifierSeenOnRelay_givenAlreadySeen() throws {
// Arrange
let eventID = "foo"
let event = try Event.findOrCreateStubBy(id: eventID, context: testContext)
Expand All @@ -172,7 +172,7 @@ final class EventTests: CoreDataTestCase {
XCTAssertEqual(events.first, event)
}

func test_eventByIdentifierSeenOnRelay_givenNotSeen() throws {
@MainActor func test_eventByIdentifierSeenOnRelay_givenNotSeen() throws {
// Arrange
let eventID = "foo"
_ = try Event.findOrCreateStubBy(id: eventID, context: testContext)
Expand All @@ -185,7 +185,7 @@ final class EventTests: CoreDataTestCase {
XCTAssertEqual(events.count, 0)
}

func test_eventByIdentifierSeenOnRelay_givenSeenOnAnother() throws {
@MainActor func test_eventByIdentifierSeenOnRelay_givenSeenOnAnother() throws {
// Arrange
let eventID = "foo"
let event = try Event.findOrCreateStubBy(id: eventID, context: testContext)
Expand Down
2 changes: 1 addition & 1 deletion NosTests/Model/FollowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest
/// Tests for the `Follow` model.
final class FollowTests: CoreDataTestCase {

func testFollowWithBadHexDoesNotSave() throws {
@MainActor func testFollowWithBadHexDoesNotSave() throws {
let author = try Author.findOrCreate(by: "test", context: testContext)
XCTAssertThrowsError(try Follow.upsert(by: author, jsonTag: ["p", "artstr"], context: testContext))
}
Expand Down
Loading

0 comments on commit 25573dc

Please sign in to comment.