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

MOB-1846 - Fixed vaulted domains sync issue #409

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@
C6B6B03E296FF74400D4E30F /* SortDomainsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6B6B03D296FF74400D4E30F /* SortDomainsManager.swift */; };
C6B6B043296FF7D600D4E30F /* DomainsSortOrderStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6B6B042296FF7D600D4E30F /* DomainsSortOrderStorage.swift */; };
C6B6B048297039E500D4E30F /* DateFormattingService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6B6B047297039E500D4E30F /* DateFormattingService.swift */; };
C6B6B8732B91A4F900565ED2 /* TaskWithDeadlineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6B6B8722B91A4F900565ED2 /* TaskWithDeadlineTests.swift */; };
C6B7CF63294AF1F80005C48D /* BlockChainItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6B7CF62294AF1F80005C48D /* BlockChainItem.swift */; };
C6B91EB429BB718100389FF5 /* StripeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6B91EB329BB718100389FF5 /* StripeService.swift */; };
C6BA746A2AD4398500628DC6 /* Int.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6BA74692AD4398500628DC6 /* Int.swift */; };
Expand Down Expand Up @@ -3325,6 +3326,7 @@
C6B6B03D296FF74400D4E30F /* SortDomainsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortDomainsManager.swift; sourceTree = "<group>"; };
C6B6B042296FF7D600D4E30F /* DomainsSortOrderStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainsSortOrderStorage.swift; sourceTree = "<group>"; };
C6B6B047297039E500D4E30F /* DateFormattingService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateFormattingService.swift; sourceTree = "<group>"; };
C6B6B8722B91A4F900565ED2 /* TaskWithDeadlineTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskWithDeadlineTests.swift; sourceTree = "<group>"; };
C6B7CF62294AF1F80005C48D /* BlockChainItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlockChainItem.swift; sourceTree = "<group>"; };
C6B91EB329BB718100389FF5 /* StripeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StripeService.swift; sourceTree = "<group>"; };
C6BA74692AD4398500628DC6 /* Int.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Int.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3850,6 +3852,7 @@
30F526452785BB22004C7AB6 /* ResolutionInitTests.swift */,
308B1D4025B8A75D005FE726 /* testUpdateRecords.swift */,
C6D9E29D28537AC8002CDAC2 /* TransactionsDecodingTests.swift */,
C6B6B8722B91A4F900565ED2 /* TaskWithDeadlineTests.swift */,
300F291525CFEBEC007EEAD1 /* TransactionsStorageTests.swift */,
C67B6D542AE79E3B00F74B0B /* ImageLoadingServiceTests.swift */,
C67B6D562AE79E8400F74B0B /* ImagesCacheStorageTests.swift */,
Expand Down Expand Up @@ -9226,6 +9229,7 @@
C6DF986E290F83020098733A /* DomainRecordsViewModelTests.swift in Sources */,
C63391782A86819600623188 /* XMTPMessagingAPIServiceTests.swift in Sources */,
C67B6D572AE79E8400F74B0B /* ImagesCacheStorageTests.swift in Sources */,
C6B6B8732B91A4F900565ED2 /* TaskWithDeadlineTests.swift in Sources */,
C6DF9870290F83020098733A /* testUpdateRecords.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ extension MockEntitiesFabric {

for tld in tlds {
for i in 0..<5 {
let domain = DomainDisplayInfo(name: "oleg_\(i)_\(ownerWallet.last!).\(tld)",
let domain = DomainDisplayInfo(name: "oleg_\(i)_\(ownerWallet.last ?? "1").\(tld)",
ownerWallet: ownerWallet,
blockchain: .Matic,
isSetForRR: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,20 @@
//

import Foundation
import Combine

final class TaskWithDeadline<Value> {

let deadline: TimeInterval
let taskPublisher: Future<Value, Error>
private var cancellables: [AnyCancellable] = []
private var ongoingTask: Task<Value, Error>?
private var operation: @Sendable () async throws -> Value
private let responseQueue: DispatchQueue

init(deadline: TimeInterval,
responseQueue: DispatchQueue = .main,
operation: @escaping @Sendable () async throws -> Value) {
self.deadline = deadline
self.responseQueue = responseQueue
taskPublisher = Future<Value, Error> { promise in
Task {
do {
let value = try await operation()
responseQueue.async {
promise(.success(value))
}
} catch {
responseQueue.async {
promise(.failure(error))
}
}
}
}
self.operation = operation
}

var value: Value {
Expand All @@ -54,26 +39,38 @@ final class TaskWithDeadline<Value> {
}

private func performOperationWithDelay() async throws -> Value {
try await withSafeCheckedThrowingContinuation { continuation in
taskPublisher
.timeout(
.seconds(deadline),
scheduler: responseQueue,
options: nil,
customError: {
TaskError.timeout
})
.sink(
receiveCompletion: { completion in
if case .failure(let error) = completion {
continuation(.failure(error))
}
},
receiveValue: { value in
let responseQueue = self.responseQueue

return try await withSafeCheckedThrowingContinuation(critical: false) { continuation in

runOperation { result in
responseQueue.async {
switch result {
case .success(let value):
continuation(.success(value))
case .failure(let error):
continuation(.failure(error))
}
)
.store(in: &cancellables)
}
}

DispatchQueue.main.asyncAfter(deadline: .now() + deadline) {
responseQueue.async {
continuation(.failure(TaskError.timeout))
}
}
}
}

private func runOperation(completion: @escaping (Result<Value, Error>)->()) {
Task {
do {
let value = try await operation()
completion(.success(value))

} catch {
completion(.failure(error))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ import Foundation
return res
}

@inlinable func withSafeCheckedThrowingContinuation<T>(function: String = #function,
@inlinable func withSafeCheckedThrowingContinuation<T>(critical: Bool = true,
function: String = #function,
_ block: ( @Sendable (@escaping (Result<T, any Error>)->())->()) ) async throws -> T {
var didFireContinuation = false

let res = try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<T, Error>) in
block { result in
guard !didFireContinuation else {
Debugger.printFailure("Second resume in \(function)", critical: true)
Debugger.printFailure("Second resume in \(function)", critical: critical)
return }

didFireContinuation = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BaseTestClass: XCTestCase {

func waitFor(interval: TimeInterval = 0.2) async throws {
let duration = UInt64(interval * 1_000_000_000)
await Task.sleep(nanoseconds: duration)
try await Task.sleep(nanoseconds: duration)
}

private var walletAddress: HexAddress { "0x1944dF1425C2237Ec501206ba416B82f47f9901d" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ private final class PrivateMockExternalEventsService: ExternalEventsServiceProto
}

private final class MockCoreAppCoordinator: CoreAppCoordinatorProtocol {
func showHome(profile: domains_manager_ios.UserProfile) {

}

var topVC: UIViewController? = nil

var isReadyToHandleExternalEvents: Bool { true }

func didRegisterShakeDevice() {

}
Expand All @@ -187,10 +195,6 @@ private final class MockCoreAppCoordinator: CoreAppCoordinatorProtocol {

}

func showHome(mintingState: domains_manager_ios.DomainsCollectionMintingState, wallet: WalletEntity) {

}

func showAppUpdateRequired() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ final class PushMessagingAPIServiceTests: XCTestCase {
// MARK: - Private methods
private extension PushMessagingAPIServiceTests {
func createMessagingChat(lastMessage: MessagingChatMessageDisplayInfo?, threadHash: String?) -> MessagingChat {
let serviceMetadata = PushEnvironment.ChatServiceMetadata(threadHash: threadHash, publicKeys: []).jsonData()
let serviceMetadata = PushEnvironment.ChatServiceMetadata(threadHash: threadHash).jsonData()
let displayInfo = MessagingChatDisplayInfo(id: "", thisUserDetails: .init(wallet: ""),
avatarURL: nil,
serviceIdentifier: .xmtp,
Expand Down Expand Up @@ -273,7 +273,6 @@ private extension PushMessagingAPIServiceTests {

static func createMessageWith(id: String, link: String, isFirstInChat: Bool = false) -> MessagingChatMessage {
let serviceMetadata = PushEnvironment.MessageServiceMetadata(encType: "",
encryptedSecret: "",
link: link).jsonData()
let displayInfo = MessagingChatMessageDisplayInfo(id: id,
chatId: "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// TaskWithDeadlineTests.swift
// domains-manager-iosTests
//
// Created by Oleg Kuplin on 01.03.2024.
//

import XCTest
@testable import domains_manager_ios

class TaskWithDeadlineTests: XCTestCase {
func testSuccessfulTaskCompletion() async throws {
let expectedValue = "Test Value"
let task = TaskWithDeadline(deadline: 1.0) {
return expectedValue
}

let value = try await task.value
XCTAssertEqual(value, expectedValue)
}

func testTaskCompletesWithinDeadline() async throws {
let expectedValue = "Test Value"
let task = TaskWithDeadline(deadline: 1.0) {
try await Task.sleep(for: .milliseconds(500))
return expectedValue
}

let value = try await task.value
XCTAssertEqual(value, expectedValue)
}

func testTaskTimeout() async throws {
let task = TaskWithDeadline(deadline: 0.1) {
try await Task.sleep(for: .seconds(1))
return ""
}

do {
_ = try await task.value
XCTFail("Task should not have completed successfully")
} catch {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class WCRequestsHandlingServiceTests: BaseTestClass {
}

private func configureWC2() {
Networking.configure(projectId: AppIdentificators.wc2ProjectId,
Networking.configure(groupIdentifier: "", projectId: AppIdentificators.wc2ProjectId,
socketFactory: SocketFactory())

let metadata = AppMetadata(name: String.Constants.mobileAppName.localized(),
Expand Down Expand Up @@ -186,8 +186,8 @@ private extension WCRequestsHandlingServiceTests {
try await waitFor(interval: 0.1)
}

func getConnectionTarget() -> (UDWallet, DomainItem) {
(createLocallyGeneratedBackedUpUDWallet(), createMockDomainItem())
func getConnectionTarget() -> (UDWallet) {
(createLocallyGeneratedBackedUpUDWallet())
}

func getWCV2URI(topic: String) -> WalletConnectSign.WalletConnectURI {
Expand Down