@@ -22,7 +22,7 @@ class RuntimeCodableTests: XCTestCase {
2222 let greeting : String
2323 }
2424
25- func testHappyPath ( ) {
25+ func testCodableHandlerWithResultSuccess ( ) {
2626 let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
2727 defer {
2828 XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
@@ -35,8 +35,8 @@ class RuntimeCodableTests: XCTestCase {
3535 let inputBytes = try JSONEncoder ( ) . encodeAsByteBuffer ( TestRequest ( name: " world " ) , allocator: ByteBufferAllocator ( ) )
3636 let ctx = try Context ( environment: . forTesting( ) , invocation: . forTesting( ) , eventLoop: eventLoopGroup. next ( ) )
3737
38- let response = try handler ( inputBytes, ctx) . flatMapThrowing { ( outputBytes ) -> TestResponse in
39- return try JSONDecoder ( ) . decode ( TestResponse . self, from: outputBytes )
38+ let response = try handler ( inputBytes, ctx) . flatMapThrowing { ( bytes ) -> TestResponse in
39+ return try JSONDecoder ( ) . decode ( TestResponse . self, from: bytes! )
4040 } . wait ( )
4141
4242 XCTAssertEqual ( response, TestResponse ( greeting: " Hello world! " ) )
@@ -46,7 +46,7 @@ class RuntimeCodableTests: XCTestCase {
4646 }
4747 }
4848
49- func testInvalidJSONInput ( ) {
49+ func testCodableHandlerWithResultInvalidInput ( ) {
5050 let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
5151 defer {
5252 XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
@@ -62,7 +62,7 @@ class RuntimeCodableTests: XCTestCase {
6262
6363 _ = try handler ( inputBytes, ctx) . flatMapThrowing { ( outputBytes) -> TestResponse in
6464 XCTFail ( " The function should not be invoked. " )
65- return try JSONDecoder ( ) . decode ( TestResponse . self, from: outputBytes)
65+ return try JSONDecoder ( ) . decode ( TestResponse . self, from: outputBytes! )
6666 } . wait ( )
6767
6868 XCTFail ( " Did not expect to succeed. " )
@@ -75,7 +75,80 @@ class RuntimeCodableTests: XCTestCase {
7575 }
7676 }
7777
78- func testSynchronousCodableInterface( ) {
78+ func testCodableHandlerWithoutResultSuccess( ) {
79+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
80+ defer {
81+ XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
82+ }
83+ let handler = Runtime . codable { ( req: TestRequest , ctx) -> EventLoopFuture < Void > in
84+ return ctx. eventLoop. makeSucceededFuture ( Void ( ) )
85+ }
86+
87+ do {
88+ let inputBytes = try JSONEncoder ( ) . encodeAsByteBuffer ( TestRequest ( name: " world " ) , allocator: ByteBufferAllocator ( ) )
89+ let ctx = try Context ( environment: . forTesting( ) , invocation: . forTesting( ) , eventLoop: eventLoopGroup. next ( ) )
90+
91+ _ = try handler ( inputBytes, ctx) . wait ( )
92+ }
93+ catch {
94+ XCTFail ( " Unexpected error: \( error) " )
95+ }
96+ }
97+
98+ func testCodableHandlerWithoutResultInvalidInput( ) {
99+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
100+ defer {
101+ XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
102+ }
103+ let handler = Runtime . codable { ( req: TestRequest , ctx) -> EventLoopFuture < Void > in
104+ return ctx. eventLoop. makeSucceededFuture ( Void ( ) )
105+ }
106+
107+ do {
108+ var inputBytes = try JSONEncoder ( ) . encodeAsByteBuffer ( TestRequest ( name: " world " ) , allocator: ByteBufferAllocator ( ) )
109+ inputBytes. setString ( " asd " , at: 0 ) // destroy the json
110+ let ctx = try Context ( environment: . forTesting( ) , invocation: . forTesting( ) , eventLoop: eventLoopGroup. next ( ) )
111+
112+ _ = try handler ( inputBytes, ctx) . wait ( )
113+
114+ XCTFail ( " Did not expect to succeed. " )
115+ }
116+ catch DecodingError . dataCorrupted( _) {
117+ // this is our expected case
118+ }
119+ catch {
120+ XCTFail ( " Unexpected error: \( error) " )
121+ }
122+ }
123+
124+ func testCodableHandlerWithoutResultFailure( ) {
125+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
126+ defer {
127+ XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
128+ }
129+ let handler = Runtime . codable { ( req: TestRequest , ctx) -> EventLoopFuture < Void > in
130+ return ctx. eventLoop. makeFailedFuture ( RuntimeError . unknown)
131+ }
132+
133+ do {
134+ let inputBytes = try JSONEncoder ( ) . encodeAsByteBuffer ( TestRequest ( name: " world " ) , allocator: ByteBufferAllocator ( ) )
135+ let ctx = try Context ( environment: . forTesting( ) , invocation: . forTesting( ) , eventLoop: eventLoopGroup. next ( ) )
136+
137+ _ = try handler ( inputBytes, ctx) . wait ( )
138+
139+ XCTFail ( " Did not expect to reach this point " )
140+ }
141+ catch RuntimeError . unknown {
142+ // expected case
143+ }
144+ catch {
145+ XCTFail ( " Unexpected error: \( error) " )
146+ }
147+ }
148+
149+ // MARK: - Synchrounous Interface -
150+
151+ func testSynchronousCodableInterfaceSuccess( ) {
79152 let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
80153 defer {
81154 XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
@@ -88,8 +161,8 @@ class RuntimeCodableTests: XCTestCase {
88161 let inputBytes = try JSONEncoder ( ) . encodeAsByteBuffer ( TestRequest ( name: " world " ) , allocator: ByteBufferAllocator ( ) )
89162 let ctx = try Context ( environment: . forTesting( ) , invocation: . forTesting( ) , eventLoop: eventLoopGroup. next ( ) )
90163
91- let response = try handler ( inputBytes, ctx) . flatMapThrowing { ( outputBytes ) -> TestResponse in
92- return try JSONDecoder ( ) . decode ( TestResponse . self, from: outputBytes )
164+ let response = try handler ( inputBytes, ctx) . flatMapThrowing { ( bytes ) -> TestResponse in
165+ return try JSONDecoder ( ) . decode ( TestResponse . self, from: bytes! )
93166 } . wait ( )
94167
95168 XCTAssertEqual ( response, TestResponse ( greeting: " Hello world! " ) )
0 commit comments