Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Increase number of batches #4

Merged
merged 1 commit into from
Oct 12, 2016
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
36 changes: 36 additions & 0 deletions JSONRPCKit/BatchFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,40 @@ public final class BatchFactory {

return Batch3(batchElement1: batchElement1, batchElement2: batchElement2, batchElement3: batchElement3)
}

public func create<Request1: RequestType, Request2: RequestType, Request3: RequestType, Request4: RequestType>(request1: Request1, _ request2: Request2, _ request3: Request3, _ request4: Request4) -> Batch4<Request1, Request2, Request3, Request4> {
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: RequestType, Request2: RequestType, Request3: RequestType, Request4: RequestType, Request5: RequestType>(request1: Request1, _ request2: Request2, _ request3: Request3, _ request4: Request4, _ request5: Request5) -> Batch5<Request1, Request2, Request3, Request4, Request5> {
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: RequestType, Request2: RequestType, Request3: RequestType, Request4: RequestType, Request5: RequestType, Request6: RequestType>(request1: Request1, _ request2: Request2, _ request3: Request3, _ request4: Request4, _ request5: Request5, _ request6: Request6) -> Batch6<Request1, Request2, Request3, Request4, Request5, Request6> {
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)
}
}
196 changes: 196 additions & 0 deletions JSONRPCKit/BatchType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,199 @@ public struct Batch3<Request1: RequestType, Request2: RequestType, Request3: Req
)
}
}


public struct Batch4<Request1: RequestType, Request2: RequestType, Request3: RequestType, Request4: RequestType>: BatchType {
public typealias Responses = (Request1.Response, Request2.Response, Request3.Response, Request4.Response)
public typealias Results = (Result<Request1.Response, JSONRPCError>, Result<Request2.Response, JSONRPCError>, Result<Request3.Response, JSONRPCError>, Result<Request4.Response, JSONRPCError>)

public let batchElement1: BatchElement<Request1>
public let batchElement2: BatchElement<Request2>
public let batchElement3: BatchElement<Request3>
public let batchElement4: BatchElement<Request4>

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<Request1: RequestType, Request2: RequestType, Request3: RequestType, Request4: RequestType, Request5: RequestType>: BatchType {
public typealias Responses = (Request1.Response, Request2.Response, Request3.Response, Request4.Response, Request5.Response)
public typealias Results = (Result<Request1.Response, JSONRPCError>, Result<Request2.Response, JSONRPCError>, Result<Request3.Response, JSONRPCError>, Result<Request4.Response, JSONRPCError>, Result<Request5.Response, JSONRPCError>)

public let batchElement1: BatchElement<Request1>
public let batchElement2: BatchElement<Request2>
public let batchElement3: BatchElement<Request3>
public let batchElement4: BatchElement<Request4>
public let batchElement5: BatchElement<Request5>

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<Request1: RequestType, Request2: RequestType, Request3: RequestType, Request4: RequestType, Request5: RequestType, Request6: RequestType>: BatchType {
public typealias Responses = (Request1.Response, Request2.Response, Request3.Response, Request4.Response, Request5.Response, Request6.Response)
public typealias Results = (Result<Request1.Response, JSONRPCError>, Result<Request2.Response, JSONRPCError>, Result<Request3.Response, JSONRPCError>, Result<Request4.Response, JSONRPCError>, Result<Request5.Response, JSONRPCError>, Result<Request6.Response, JSONRPCError>)

public let batchElement1: BatchElement<Request1>
public let batchElement2: BatchElement<Request2>
public let batchElement3: BatchElement<Request3>
public let batchElement4: BatchElement<Request4>
public let batchElement5: BatchElement<Request5>
public let batchElement6: BatchElement<Request6>

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()
)
}
}
45 changes: 45 additions & 0 deletions Tests/BatchFactoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down