Skip to content

Commit d415813

Browse files
committed
Simplify findProvs test.
1 parent 0fdb97a commit d415813

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

SwiftIpfsApiTests/SwiftIpfsApiTests.swift

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class SwiftIpfsApiTests: XCTestCase {
1919
let hostPort = 5001
2020

2121
/// Your own IPNS node hash
22-
let nodeIdString = "QmWNwhBWa9sWPvbuS5XNaLp6Phh5vRN77BZRF5xPWG3FN1"
22+
// let nodeIdString = "QmWNwhBWa9sWPvbuS5XNaLp6Phh5vRN77BZRF5xPWG3FN1"
23+
let nodeIdString = "QmRzsihDMWML1dNPa51qwD2dacvZPfTrqsQt4pi1DWGJFP"
2324

2425
/// Another known neighbouring hash
2526
// let altNodeIdString = "QmWqjusr86LThkYgjAbNMa8gJ55wzVufkcv5E2TFfzYZXu"
@@ -445,35 +446,20 @@ class SwiftIpfsApiTests: XCTestCase {
445446
try api.dht.findProvs(multihash, numProviders: 1) {
446447
result in
447448

448-
var pass = false
449-
repeat {
450-
guard case .Array(let providers) = result else { break }
451-
452-
for prov in providers {
453-
454-
guard case .Object(let obj) = prov,
455-
case .Array(let responses) = obj["Responses"]! else { continue }
456-
457-
for response in responses {
458-
459-
guard case .Object(let ars) = response,
460-
case .String(let provHash) = ars["ID"]! else { continue }
461-
462-
/// This node should definitely be in the dht.
463-
if provHash == self.nodeIdString { pass = true }
464-
}
465-
}
466-
} while false
449+
// For each (if any) element in the result array we filter out
450+
// any objects with an ID that matches the localhost node's id hash.
451+
let matchingProviders = result.array?.compactMap { $0.object?["Responses"]?.array?.filter { $0.object?["ID"]?.string == self.nodeIdString }}
467452

468-
XCTAssert(pass)
453+
let count = matchingProviders?.count ?? 0
454+
XCTAssert(count > 0)
469455

470456
expectation.fulfill()
471457
}
472458
} catch {
473459
XCTFail("test failed with error \(error)")
474460
}
475461

476-
wait(for: [expectation], timeout: 5.0)
462+
wait(for: [expectation], timeout: 15.0)
477463
}
478464

479465
// This test fails on timeout because the api doesn't actually end and thus doesn't return. Fix somehow.

0 commit comments

Comments
 (0)