|
|
|
@ -1,13 +1,13 @@
|
|
|
|
|
import PromiseKit
|
|
|
|
|
@testable import SignalServiceKit
|
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
|
|
class OnionRequestAPITests : XCTestCase {
|
|
|
|
|
private let maxRetryCount: UInt = 2 // Be a bit more stringent when testing
|
|
|
|
|
|
|
|
|
|
func testGetPath() {
|
|
|
|
|
func testPathBuilding() {
|
|
|
|
|
let semaphore = DispatchSemaphore(value: 0)
|
|
|
|
|
var error: Error? = nil
|
|
|
|
|
let _ = OnionRequestAPI.getPath().retryingIfNeeded(maxRetryCount: maxRetryCount).done(on: OnionRequestAPI.workQueue) { _ in
|
|
|
|
|
let _ = OnionRequestAPI.getPath().done(on: OnionRequestAPI.workQueue) { _ in
|
|
|
|
|
semaphore.signal()
|
|
|
|
|
}.catch(on: OnionRequestAPI.workQueue) {
|
|
|
|
|
error = $0; semaphore.signal()
|
|
|
|
@ -16,6 +16,39 @@ class OnionRequestAPITests : XCTestCase {
|
|
|
|
|
XCTAssert(error == nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Add request sending test
|
|
|
|
|
// TODO: Add error handling test
|
|
|
|
|
/// Builds a path and then routes the same request through it several times. Logs the number of successes
|
|
|
|
|
/// versus the number of failures.
|
|
|
|
|
func testOnionRequestSending() {
|
|
|
|
|
let semaphore = DispatchSemaphore(value: 0)
|
|
|
|
|
LokiAPI.getRandomSnode().then(on: OnionRequestAPI.workQueue) { snode -> Promise<LokiAPITarget> in
|
|
|
|
|
return OnionRequestAPI.getPath().map(on: OnionRequestAPI.workQueue) { _ in snode }
|
|
|
|
|
}.done(on: OnionRequestAPI.workQueue) { snode in
|
|
|
|
|
var successCount = 0
|
|
|
|
|
var failureCount = 0
|
|
|
|
|
let promises: [Promise<Void>] = (0..<16).map { _ in
|
|
|
|
|
let mockSessionID = "0582bc30f11e8a9736407adcaca03b049f4acd4af3ae7eb6b6608d30f0b1e6a20e"
|
|
|
|
|
let parameters: JSON = [ "pubKey" : mockSessionID ]
|
|
|
|
|
let (promise, seal) = Promise<Void>.pending()
|
|
|
|
|
OnionRequestAPI.invoke(.getSwarm, on: snode, with: parameters).done(on: OnionRequestAPI.workQueue) { _ in
|
|
|
|
|
successCount += 1
|
|
|
|
|
seal.fulfill(())
|
|
|
|
|
}.catch(on: OnionRequestAPI.workQueue) { error in
|
|
|
|
|
failureCount += 1
|
|
|
|
|
seal.reject(error)
|
|
|
|
|
}.finally(on: OnionRequestAPI.workQueue) {
|
|
|
|
|
print("[Loki] [Onion Request API] Success rate: \(successCount)/\(failureCount).")
|
|
|
|
|
}
|
|
|
|
|
return promise
|
|
|
|
|
}
|
|
|
|
|
when(resolved: promises).done(on: OnionRequestAPI.workQueue) { _ in
|
|
|
|
|
semaphore.signal()
|
|
|
|
|
}
|
|
|
|
|
}.catch(on: OnionRequestAPI.workQueue) { error in
|
|
|
|
|
print("[Loki] [Onion Request API] Path building failed due to error: \(error).")
|
|
|
|
|
semaphore.signal()
|
|
|
|
|
}
|
|
|
|
|
semaphore.wait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Test error handling
|
|
|
|
|
}
|
|
|
|
|