-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
724e339
commit ed49d7f
Showing
16 changed files
with
387 additions
and
146 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
consensus-client-it/src/test/scala/com/wavesplatform/api/HasRetry.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,36 @@ | ||
package com.wavesplatform.api | ||
|
||
import org.scalatest.concurrent.Eventually.PatienceConfig | ||
|
||
import scala.concurrent.duration.{Deadline, DurationInt} | ||
import scala.util.{Failure, Success, Try} | ||
|
||
trait HasRetry { | ||
protected implicit def patienceConfig: PatienceConfig = PatienceConfig(timeout = 30.seconds, interval = 1.second) | ||
|
||
protected def retryWithAttempts[ResponseT](f: Int => ResponseT)(implicit patienceConfig: PatienceConfig): ResponseT = { | ||
var attempt = 0 | ||
retry { | ||
attempt += 1 | ||
f(attempt) | ||
} | ||
} | ||
|
||
// Eventually has issues with handling patienceConfig | ||
protected def retry[ResponseT](f: => ResponseT)(implicit patienceConfig: PatienceConfig): ResponseT = { | ||
val deadline = Deadline.now + patienceConfig.timeout | ||
|
||
var r = Try(f) | ||
while (r.isFailure && deadline.hasTimeLeft()) { | ||
Thread.sleep(patienceConfig.interval.toMillis) | ||
r = Try(f) | ||
} | ||
|
||
r match { | ||
case Failure(e) => throw new RuntimeException(s"All attempts are out: $patienceConfig", e) | ||
case Success(r) => r | ||
} | ||
} | ||
|
||
protected def failRetry(message: String): Nothing = throw new RuntimeException(message) | ||
} |
47 changes: 47 additions & 0 deletions
47
consensus-client-it/src/test/scala/com/wavesplatform/api/LoggingBackend.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,47 @@ | ||
package com.wavesplatform.api | ||
|
||
import com.wavesplatform.api.LoggingBackend.{LoggingOptions, LoggingOptionsTag} | ||
import com.wavesplatform.utils.ScorexLogging | ||
import sttp.capabilities.Effect | ||
import sttp.client3.* | ||
|
||
class LoggingBackend[F[_], P](delegate: SttpBackend[F, P]) extends DelegateSttpBackend[F, P](delegate) with ScorexLogging { | ||
override def send[T, R >: P & Effect[F]](request: Request[T, R]): F[Response[T]] = { | ||
val l = request.tag(LoggingOptionsTag).collect { case l: LoggingOptions => l } | ||
|
||
l.filter(_.logRequest).foreach { l => | ||
var logStr = s"${l.prefix} ${request.method} ${request.uri}" | ||
if (l.logResponseBody) logStr += s": body=${request.body.show}" | ||
log.debug(logStr) | ||
} | ||
|
||
val requestWithRawJson = request.response(asBothOption(request.response, asStringAlways)) | ||
val withErrorLog = responseMonad.handleError(requestWithRawJson.send(delegate)) { x => | ||
l.foreach { l => log.debug(s"${l.prefix} Error: ${x.getMessage}") } | ||
responseMonad.error(x) | ||
} | ||
|
||
responseMonad.flatMap(withErrorLog) { response => | ||
l.foreach { l => | ||
var logStr = s"${l.prefix} HTTP ${response.code}" | ||
if (l.logResponseBody) logStr += s": body=${response.body._2}" | ||
log.debug(logStr) | ||
} | ||
|
||
responseMonad.unit(response.copy(body = response.body._1)) | ||
} | ||
} | ||
} | ||
|
||
object LoggingBackend { | ||
val LoggingOptionsTag = "logging" | ||
|
||
case class LoggingOptions( | ||
logRequest: Boolean = true, | ||
logRequestBody: Boolean = true, | ||
logResponseBody: Boolean = true, | ||
requestId: Int = LoggingUtil.currRequestId | ||
) { | ||
val prefix = s"[$requestId]" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
consensus-client-it/src/test/scala/com/wavesplatform/api/LoggingUtil.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,9 @@ | ||
package com.wavesplatform.api | ||
|
||
import java.util.concurrent.ThreadLocalRandom | ||
|
||
object LoggingUtil { | ||
val Length = 5 | ||
|
||
def currRequestId: Int = ThreadLocalRandom.current().nextInt(10000, 100000) | ||
} |
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
36 changes: 0 additions & 36 deletions
36
consensus-client-it/src/test/scala/com/wavesplatform/api/WithRetries.scala
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.