Browse Source

server: Handle XSNWorkQueueDepthExceeded when the response is not a json

When the RPC API returns a non-json response, the XSNWorkQueueDepthExceeded
error can be derived.
prometheus-integration
Alexis Hernandez 6 years ago
parent
commit
479eaca09c
  1. 23
      server/app/com/xsn/explorer/services/XSNService.scala
  2. 24
      server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

23
server/app/com/xsn/explorer/services/XSNService.scala

@ -443,13 +443,22 @@ class XSNServiceRPCImpl @Inject() (
}
}
// if there is no result nor error, it is probably that the server returned non 200 status
maybe.orElse {
Try(response.json)
.toOption
.flatMap { json => mapError(json, errorCodeMapper) }
.map { e => Bad(e).accumulating }
}
maybe
.orElse {
// if there is no result nor error, it is probably that the server returned non 200 status
Try(response.json)
.toOption
.flatMap { json => mapError(json, errorCodeMapper) }
.map { e => Bad(e).accumulating }
}
.orElse {
// if still there is no error, the response might not be a json
Try(response.body)
.collect {
case "Work queue depth exceeded" => Bad(XSNWorkQueueDepthExceeded).accumulating
}
.toOption
}
}
override val genesisBlockhash: Blockhash = explorerConfig.genesisBlock

24
server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

@ -8,8 +8,10 @@ import com.xsn.explorer.models.rpc.Masternode
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._
import org.scalactic.{Bad, Good}
import org.scalatest.concurrent.PatienceConfiguration.Timeout
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.mockito.MockitoSugar
import org.scalatest.time.{Seconds, Span}
import org.scalatest.{MustMatchers, OptionValues, WordSpec}
import play.api.libs.json.{JsNull, JsString, JsValue, Json}
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
@ -176,6 +178,19 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures
result mustEqual Bad(XSNWorkQueueDepthExceeded).accumulating
}
}
"handle work queue depth exceeded (no json)" in {
val txid = createTransactionId("0834641a7d30d8a2d2b451617599670445ee94ed7736e146c13be260c576c641")
val responseBody = "Work queue depth exceeded"
mockRequestString(request, response)(500, responseBody)
val timeout = Timeout(Span(5, Seconds))
whenReady(service.getTransaction(txid), timeout) { result =>
result mustEqual Bad(XSNWorkQueueDepthExceeded).accumulating
}
}
}
"getRawTransaction" should {
@ -571,8 +586,15 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures
}
private def mockRequest(request: WSRequest, response: WSResponse)(status: Int, body: JsValue) = {
when(response.status).thenReturn(200)
when(response.status).thenReturn(status)
when(response.json).thenReturn(body)
when(response.body).thenReturn(body.toString())
when(request.post[String](anyString())(any())).thenReturn(Future.successful(response))
}
private def mockRequestString(request: WSRequest, response: WSResponse)(status: Int, body: String) = {
when(response.status).thenReturn(status)
when(response.body).thenReturn(body)
when(request.post[String](anyString())(any())).thenReturn(Future.successful(response))
}
}

Loading…
Cancel
Save