@@ -22,7 +22,7 @@ class RuntimeCodableTests: XCTestCase {
22
22
let greeting : String
23
23
}
24
24
25
- func testHappyPath ( ) {
25
+ func testCodableHandlerWithResultSuccess ( ) {
26
26
let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
27
27
defer {
28
28
XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
@@ -35,8 +35,8 @@ class RuntimeCodableTests: XCTestCase {
35
35
let inputBytes = try JSONEncoder ( ) . encodeAsByteBuffer ( TestRequest ( name: " world " ) , allocator: ByteBufferAllocator ( ) )
36
36
let ctx = try Context ( environment: . forTesting( ) , invocation: . forTesting( ) , eventLoop: eventLoopGroup. next ( ) )
37
37
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! )
40
40
} . wait ( )
41
41
42
42
XCTAssertEqual ( response, TestResponse ( greeting: " Hello world! " ) )
@@ -46,7 +46,7 @@ class RuntimeCodableTests: XCTestCase {
46
46
}
47
47
}
48
48
49
- func testInvalidJSONInput ( ) {
49
+ func testCodableHandlerWithResultInvalidInput ( ) {
50
50
let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
51
51
defer {
52
52
XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
@@ -62,7 +62,7 @@ class RuntimeCodableTests: XCTestCase {
62
62
63
63
_ = try handler ( inputBytes, ctx) . flatMapThrowing { ( outputBytes) -> TestResponse in
64
64
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! )
66
66
} . wait ( )
67
67
68
68
XCTFail ( " Did not expect to succeed. " )
@@ -75,7 +75,80 @@ class RuntimeCodableTests: XCTestCase {
75
75
}
76
76
}
77
77
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( ) {
79
152
let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
80
153
defer {
81
154
XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) )
@@ -88,8 +161,8 @@ class RuntimeCodableTests: XCTestCase {
88
161
let inputBytes = try JSONEncoder ( ) . encodeAsByteBuffer ( TestRequest ( name: " world " ) , allocator: ByteBufferAllocator ( ) )
89
162
let ctx = try Context ( environment: . forTesting( ) , invocation: . forTesting( ) , eventLoop: eventLoopGroup. next ( ) )
90
163
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! )
93
166
} . wait ( )
94
167
95
168
XCTAssertEqual ( response, TestResponse ( greeting: " Hello world! " ) )
0 commit comments