-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: The /health endpoint fails if there are more than 10 missing …
…blocks
- Loading branch information
Showing
4 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
server/app/com/xsn/explorer/models/SynchronizationProgress.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.xsn.explorer.models | ||
|
||
case class SynchronizationProgress(total: Int, synced: Int) { | ||
def missing: Int = Math.max(0, total - synced) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
package controllers | ||
|
||
import javax.inject.Inject | ||
|
||
import com.xsn.explorer.services.StatisticsService | ||
import controllers.common.{MyJsonController, MyJsonControllerComponents} | ||
import javax.inject.Inject | ||
import org.scalactic.Good | ||
|
||
class HealthController @Inject()(cc: MyJsonControllerComponents) extends MyJsonController(cc) { | ||
class HealthController @Inject()(cc: MyJsonControllerComponents, statsService: StatisticsService) | ||
extends MyJsonController(cc) { | ||
|
||
def check() = Action { | ||
Ok | ||
def check() = Action.async { _ => | ||
statsService.getSynchronizationProgress | ||
.map { | ||
case Good(progress) if progress.missing < 10 => Ok | ||
case Good(progress) => InternalServerError(s"There are ${progress.missing} missing blocks") | ||
case _ => InternalServerError("Failed to check sync progress") | ||
} | ||
.recover { | ||
case _: Throwable => InternalServerError("Failed to check sync progress") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,48 @@ | ||
package controllers | ||
|
||
import com.xsn.explorer.data.StatisticsBlockingDataHandler | ||
import com.xsn.explorer.helpers.DataGenerator | ||
import com.xsn.explorer.models._ | ||
import com.xsn.explorer.models.values._ | ||
import com.xsn.explorer.services.XSNService | ||
import controllers.common.MyAPISpec | ||
import org.mockito.MockitoSugar._ | ||
import org.scalactic.Good | ||
import play.api.Application | ||
import play.api.inject.bind | ||
import play.api.test.Helpers._ | ||
|
||
import scala.concurrent.Future | ||
|
||
class HealthControllerSpec extends MyAPISpec { | ||
|
||
val application: Application = guiceApplicationBuilder.build() | ||
private val xsnServiceMock = mock[XSNService] | ||
private val statisticsDataHandlerMock = mock[StatisticsBlockingDataHandler] | ||
|
||
val application: Application = guiceApplicationBuilder | ||
.overrides(bind[XSNService].to(xsnServiceMock)) | ||
.overrides(bind[StatisticsBlockingDataHandler].to(statisticsDataHandlerMock)) | ||
.build() | ||
|
||
"GET /health" should { | ||
"return OK" in { | ||
val latestXSNBlock = DataGenerator.randomBlock().copy(height = Height(19)) | ||
val stats = Statistics(10, 0, None, None) | ||
when(xsnServiceMock.getLatestBlock()).thenReturn(Future.successful(Good(latestXSNBlock))) | ||
when(statisticsDataHandlerMock.getStatistics()).thenReturn(Good(stats)) | ||
val response = GET("/health") | ||
|
||
status(response) mustEqual OK | ||
} | ||
|
||
"return Internal Server Error if there are 10 missing blocks" in { | ||
val latestXSNBlock = DataGenerator.randomBlock().copy(height = Height(20)) | ||
val stats = Statistics(10, 0, None, None) | ||
when(xsnServiceMock.getLatestBlock()).thenReturn(Future.successful(Good(latestXSNBlock))) | ||
when(statisticsDataHandlerMock.getStatistics()).thenReturn(Good(stats)) | ||
val response = GET("/health") | ||
|
||
status(response) mustEqual INTERNAL_SERVER_ERROR | ||
} | ||
} | ||
} |