|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package wvlet.airframe.http.client |
| 15 | + |
| 16 | +import wvlet.airframe.http.HttpMessage.Response |
| 17 | +import wvlet.airframe.http.{Http, HttpMessage, HttpStatus, RPCMethod, RPCStatus, RxHttpEndpoint, RxHttpFilter} |
| 18 | +import wvlet.airframe.rx.Rx |
| 19 | +import wvlet.airframe.surface.Surface |
| 20 | +import wvlet.airspec.AirSpec |
| 21 | + |
| 22 | +import java.util.concurrent.atomic.AtomicInteger |
| 23 | +import scala.util.{Failure, Success} |
| 24 | + |
| 25 | +object RPCErrorHandlingTest extends AirSpec { |
| 26 | + |
| 27 | + private class DummyHttpChannel extends HttpChannel { |
| 28 | + private val requestCount = new AtomicInteger(0) |
| 29 | + |
| 30 | + override def send(req: HttpMessage.Request, channelConfig: HttpChannelConfig): Response = ??? |
| 31 | + override def sendAsync(req: HttpMessage.Request, channelConfig: HttpChannelConfig): Rx[Response] = { |
| 32 | + requestCount.getAndIncrement() match { |
| 33 | + case 0 => |
| 34 | + // For retry test |
| 35 | + Rx.single(Http.response(HttpStatus.InternalServerError_500)) |
| 36 | + case _ => |
| 37 | + Rx.single(RPCStatus.INVALID_REQUEST_U1.newException("RPC error").toResponse) |
| 38 | + } |
| 39 | + } |
| 40 | + override def close(): Unit = {} |
| 41 | + } |
| 42 | + |
| 43 | + initDesign { |
| 44 | + _.bind[AsyncClient].toInstance { |
| 45 | + val config = Http.client // .withClientFilter(null) |
| 46 | + new AsyncClientImpl(new DummyHttpChannel, config) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + test("test error response") { (client: AsyncClient) => |
| 51 | + val dummyRPCMerthod = |
| 52 | + RPCMethod("/rpc_method", "demo.RPCClass", "hello", Surface.of[Map[String, Any]], Surface.of[String]) |
| 53 | + |
| 54 | + var observedRetry = false |
| 55 | + val myClient = client.withClientFilter(new RxHttpFilter { |
| 56 | + override def apply(request: HttpMessage.Request, next: RxHttpEndpoint): Rx[Response] = { |
| 57 | + next(request).runOn { |
| 58 | + case Success(x) => |
| 59 | + x.status match { |
| 60 | + case HttpStatus.InternalServerError_500 => |
| 61 | + observedRetry = true |
| 62 | + case _ => |
| 63 | + } |
| 64 | + case Failure(e) => |
| 65 | + logger.info(e) |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + myClient.rpc[Map[String, Any], String](dummyRPCMerthod, Map("message" -> "world")).transform { |
| 71 | + case scala.util.Failure(e) => |
| 72 | + observedRetry shouldBe true |
| 73 | + e.getMessage shouldContain "RPC error" |
| 74 | + case _ => |
| 75 | + fail("should fail") |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments