From 3079ceca7768176583a6acc92a9a5fb91344f993 Mon Sep 17 00:00:00 2001 From: Yosuke Ishikawa Date: Wed, 12 Oct 2016 17:43:38 +0900 Subject: [PATCH] Increase number of batches --- JSONRPCKit/BatchFactory.swift | 36 +++++++ JSONRPCKit/BatchType.swift | 196 ++++++++++++++++++++++++++++++++++ Tests/BatchFactoryTests.swift | 45 ++++++++ 3 files changed, 277 insertions(+) diff --git a/JSONRPCKit/BatchFactory.swift b/JSONRPCKit/BatchFactory.swift index 9260684..3d75fa3 100644 --- a/JSONRPCKit/BatchFactory.swift +++ b/JSONRPCKit/BatchFactory.swift @@ -46,4 +46,40 @@ public final class BatchFactory { return Batch3(batchElement1: batchElement1, batchElement2: batchElement2, batchElement3: batchElement3) } + + public func create(request1: Request1, _ request2: Request2, _ request3: Request3, _ request4: Request4) -> Batch4 { + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) + let batchElement1 = BatchElement(request: request1, version: version, id: idGenerator.next()) + let batchElement2 = BatchElement(request: request2, version: version, id: idGenerator.next()) + let batchElement3 = BatchElement(request: request3, version: version, id: idGenerator.next()) + let batchElement4 = BatchElement(request: request4, version: version, id: idGenerator.next()) + dispatch_semaphore_signal(semaphore) + + return Batch4(batchElement1: batchElement1, batchElement2: batchElement2, batchElement3: batchElement3, batchElement4: batchElement4) + } + + public func create(request1: Request1, _ request2: Request2, _ request3: Request3, _ request4: Request4, _ request5: Request5) -> Batch5 { + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) + let batchElement1 = BatchElement(request: request1, version: version, id: idGenerator.next()) + let batchElement2 = BatchElement(request: request2, version: version, id: idGenerator.next()) + let batchElement3 = BatchElement(request: request3, version: version, id: idGenerator.next()) + let batchElement4 = BatchElement(request: request4, version: version, id: idGenerator.next()) + let batchElement5 = BatchElement(request: request5, version: version, id: idGenerator.next()) + dispatch_semaphore_signal(semaphore) + + return Batch5(batchElement1: batchElement1, batchElement2: batchElement2, batchElement3: batchElement3, batchElement4: batchElement4, batchElement5: batchElement5) + } + + public func create(request1: Request1, _ request2: Request2, _ request3: Request3, _ request4: Request4, _ request5: Request5, _ request6: Request6) -> Batch6 { + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) + let batchElement1 = BatchElement(request: request1, version: version, id: idGenerator.next()) + let batchElement2 = BatchElement(request: request2, version: version, id: idGenerator.next()) + let batchElement3 = BatchElement(request: request3, version: version, id: idGenerator.next()) + let batchElement4 = BatchElement(request: request4, version: version, id: idGenerator.next()) + let batchElement5 = BatchElement(request: request5, version: version, id: idGenerator.next()) + let batchElement6 = BatchElement(request: request6, version: version, id: idGenerator.next()) + dispatch_semaphore_signal(semaphore) + + return Batch6(batchElement1: batchElement1, batchElement2: batchElement2, batchElement3: batchElement3, batchElement4: batchElement4, batchElement5: batchElement5, batchElement6: batchElement6) + } } diff --git a/JSONRPCKit/BatchType.swift b/JSONRPCKit/BatchType.swift index b961bbb..6eb266b 100644 --- a/JSONRPCKit/BatchType.swift +++ b/JSONRPCKit/BatchType.swift @@ -143,3 +143,199 @@ public struct Batch3: BatchType { + public typealias Responses = (Request1.Response, Request2.Response, Request3.Response, Request4.Response) + public typealias Results = (Result, Result, Result, Result) + + public let batchElement1: BatchElement + public let batchElement2: BatchElement + public let batchElement3: BatchElement + public let batchElement4: BatchElement + + public var requestObject: AnyObject { + return [ + batchElement1.body, + batchElement2.body, + batchElement3.body, + batchElement4.body, + ] + } + + public func responsesFromObject(object: AnyObject) throws -> Responses { + guard let batchObjects = object as? [AnyObject] else { + throw JSONRPCError.NonArrayResponse(object) + } + + return ( + try batchElement1.responseFromBatchObjects(batchObjects), + try batchElement2.responseFromBatchObjects(batchObjects), + try batchElement3.responseFromBatchObjects(batchObjects), + try batchElement4.responseFromBatchObjects(batchObjects) + ) + } + + public func resultsFromObject(object: AnyObject) -> Results { + guard let batchObjects = object as? [AnyObject] else { + return ( + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)) + ) + } + + return ( + batchElement1.resultFromBatchObjects(batchObjects), + batchElement2.resultFromBatchObjects(batchObjects), + batchElement3.resultFromBatchObjects(batchObjects), + batchElement4.resultFromBatchObjects(batchObjects) + ) + } + + public static func responsesFromResults(results: Results) throws -> Responses { + return ( + try results.0.dematerialize(), + try results.1.dematerialize(), + try results.2.dematerialize(), + try results.3.dematerialize() + ) + } +} + +public struct Batch5: BatchType { + public typealias Responses = (Request1.Response, Request2.Response, Request3.Response, Request4.Response, Request5.Response) + public typealias Results = (Result, Result, Result, Result, Result) + + public let batchElement1: BatchElement + public let batchElement2: BatchElement + public let batchElement3: BatchElement + public let batchElement4: BatchElement + public let batchElement5: BatchElement + + public var requestObject: AnyObject { + return [ + batchElement1.body, + batchElement2.body, + batchElement3.body, + batchElement4.body, + batchElement5.body, + ] + } + + public func responsesFromObject(object: AnyObject) throws -> Responses { + guard let batchObjects = object as? [AnyObject] else { + throw JSONRPCError.NonArrayResponse(object) + } + + return ( + try batchElement1.responseFromBatchObjects(batchObjects), + try batchElement2.responseFromBatchObjects(batchObjects), + try batchElement3.responseFromBatchObjects(batchObjects), + try batchElement4.responseFromBatchObjects(batchObjects), + try batchElement5.responseFromBatchObjects(batchObjects) + ) + } + + public func resultsFromObject(object: AnyObject) -> Results { + guard let batchObjects = object as? [AnyObject] else { + return ( + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)) + ) + } + + return ( + batchElement1.resultFromBatchObjects(batchObjects), + batchElement2.resultFromBatchObjects(batchObjects), + batchElement3.resultFromBatchObjects(batchObjects), + batchElement4.resultFromBatchObjects(batchObjects), + batchElement5.resultFromBatchObjects(batchObjects) + ) + } + + public static func responsesFromResults(results: Results) throws -> Responses { + return ( + try results.0.dematerialize(), + try results.1.dematerialize(), + try results.2.dematerialize(), + try results.3.dematerialize(), + try results.4.dematerialize() + ) + } +} + +public struct Batch6: BatchType { + public typealias Responses = (Request1.Response, Request2.Response, Request3.Response, Request4.Response, Request5.Response, Request6.Response) + public typealias Results = (Result, Result, Result, Result, Result, Result) + + public let batchElement1: BatchElement + public let batchElement2: BatchElement + public let batchElement3: BatchElement + public let batchElement4: BatchElement + public let batchElement5: BatchElement + public let batchElement6: BatchElement + + public var requestObject: AnyObject { + return [ + batchElement1.body, + batchElement2.body, + batchElement3.body, + batchElement4.body, + batchElement5.body, + batchElement6.body, + ] + } + + public func responsesFromObject(object: AnyObject) throws -> Responses { + guard let batchObjects = object as? [AnyObject] else { + throw JSONRPCError.NonArrayResponse(object) + } + + return ( + try batchElement1.responseFromBatchObjects(batchObjects), + try batchElement2.responseFromBatchObjects(batchObjects), + try batchElement3.responseFromBatchObjects(batchObjects), + try batchElement4.responseFromBatchObjects(batchObjects), + try batchElement5.responseFromBatchObjects(batchObjects), + try batchElement6.responseFromBatchObjects(batchObjects) + ) + } + + public func resultsFromObject(object: AnyObject) -> Results { + guard let batchObjects = object as? [AnyObject] else { + return ( + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)), + .Failure(.NonArrayResponse(object)) + ) + } + + return ( + batchElement1.resultFromBatchObjects(batchObjects), + batchElement2.resultFromBatchObjects(batchObjects), + batchElement3.resultFromBatchObjects(batchObjects), + batchElement4.resultFromBatchObjects(batchObjects), + batchElement5.resultFromBatchObjects(batchObjects), + batchElement6.resultFromBatchObjects(batchObjects) + ) + } + + public static func responsesFromResults(results: Results) throws -> Responses { + return ( + try results.0.dematerialize(), + try results.1.dematerialize(), + try results.2.dematerialize(), + try results.3.dematerialize(), + try results.4.dematerialize(), + try results.5.dematerialize() + ) + } +} diff --git a/Tests/BatchFactoryTests.swift b/Tests/BatchFactoryTests.swift index d58d7f0..a796562 100644 --- a/Tests/BatchFactoryTests.swift +++ b/Tests/BatchFactoryTests.swift @@ -46,6 +46,51 @@ class BatchFactoryTests: XCTestCase { XCTAssertEqual(batch.batchElement3.id?.value as? Int, 3) } + func test4() { + let request1 = TestRequest(method: "method1", parameters: ["key1": "value1"]) + let request2 = TestRequest(method: "method2", parameters: ["key2": "value2"]) + let request3 = TestRequest(method: "method3", parameters: ["key3": "value3"]) + let request4 = TestRequest(method: "method4", parameters: ["key4": "value4"]) + let batch = batchFactory.create(request1, request2, request3, request4) + + XCTAssertEqual(batch.batchElement1.id?.value as? Int, 1) + XCTAssertEqual(batch.batchElement2.id?.value as? Int, 2) + XCTAssertEqual(batch.batchElement3.id?.value as? Int, 3) + XCTAssertEqual(batch.batchElement4.id?.value as? Int, 4) + } + + func test5() { + let request1 = TestRequest(method: "method1", parameters: ["key1": "value1"]) + let request2 = TestRequest(method: "method2", parameters: ["key2": "value2"]) + let request3 = TestRequest(method: "method3", parameters: ["key3": "value3"]) + let request4 = TestRequest(method: "method4", parameters: ["key4": "value4"]) + let request5 = TestRequest(method: "method5", parameters: ["key5": "value5"]) + let batch = batchFactory.create(request1, request2, request3, request4, request5) + + XCTAssertEqual(batch.batchElement1.id?.value as? Int, 1) + XCTAssertEqual(batch.batchElement2.id?.value as? Int, 2) + XCTAssertEqual(batch.batchElement3.id?.value as? Int, 3) + XCTAssertEqual(batch.batchElement4.id?.value as? Int, 4) + XCTAssertEqual(batch.batchElement5.id?.value as? Int, 5) + } + + func test6() { + let request1 = TestRequest(method: "method1", parameters: ["key1": "value1"]) + let request2 = TestRequest(method: "method2", parameters: ["key2": "value2"]) + let request3 = TestRequest(method: "method3", parameters: ["key3": "value3"]) + let request4 = TestRequest(method: "method4", parameters: ["key4": "value4"]) + let request5 = TestRequest(method: "method5", parameters: ["key5": "value5"]) + let request6 = TestRequest(method: "method6", parameters: ["key6": "value6"]) + let batch = batchFactory.create(request1, request2, request3, request4, request5, request6) + + XCTAssertEqual(batch.batchElement1.id?.value as? Int, 1) + XCTAssertEqual(batch.batchElement2.id?.value as? Int, 2) + XCTAssertEqual(batch.batchElement3.id?.value as? Int, 3) + XCTAssertEqual(batch.batchElement4.id?.value as? Int, 4) + XCTAssertEqual(batch.batchElement5.id?.value as? Int, 5) + XCTAssertEqual(batch.batchElement6.id?.value as? Int, 6) + } + func testThreadSafety() { let operationQueue = NSOperationQueue()