Add more onion request tests

pull/148/head
gmbnt 5 years ago
parent f76980da5a
commit 547f7943c3

@ -2,7 +2,7 @@ import PromiseKit
@objc(LKAPI)
public final class LokiAPI : NSObject {
private static let stateQueue = DispatchQueue(label: "stateQueue")
private static let stateQueue = DispatchQueue(label: "LokiAPI.stateQueue")
/// Only ever modified from the message processing queue (`OWSBatchMessageProcessor.processingQueue`).
private static var syncMessageTimestamps: [String:Set<UInt64>] = [:]
@ -22,7 +22,7 @@ public final class LokiAPI : NSObject {
}
/// All service node related errors must be handled on this queue to avoid race conditions maintaining e.g. failure counts.
public static let errorHandlingQueue = DispatchQueue(label: "errorHandlingQueue")
public static let errorHandlingQueue = DispatchQueue(label: "LokiAPI.errorHandlingQueue")
// MARK: Convenience
internal static let storage = OWSPrimaryStorage.shared()

@ -123,6 +123,8 @@ internal enum OnionRequestAPI {
}
/// Returns a `Path` to be used for building an onion request. Builds new paths as needed.
///
/// - Note: Exposed for testing purposes.
internal static func getPath() -> Promise<Path> {
guard pathSize >= 1 else { preconditionFailure("Cannot build path of size zero.") }
// randomElement() uses the system's default random generator, which is cryptographically secure

@ -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
}

Loading…
Cancel
Save