Skip to content

Commit

Permalink
retry added
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Aug 27, 2024
1 parent 6db79cb commit 9ed3965
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
34 changes: 34 additions & 0 deletions ce3/src/main/scala/_sandbox/Sandbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.DurationInt
import scala.concurrent.{Await, Future}

object Sandbox {

def id[A](a: A): A = a
Expand All @@ -15,6 +19,36 @@ class SandboxSpec extends AnyFunSuite with Matchers with ScalaCheckPropertyCheck

import Sandbox._

test("123") {
case class Person(age: Int, name: String)
def getAge = Future(123)
def getName = Future("Jim")

val fp: Future[Person] =
getName.flatMap(n =>
getAge.map(a =>
Person(a, n)
)
)
val p: Person = Await.result(fp, 10.seconds)




}

test("11") {
pprint.log(math.acos(0.96))
pprint.log(math.sin(math.acos(0.96)))
pprint.log(math.acos(1))
pprint.log(math.sin(math.acos(1)))
pprint.log(math.acos(0.95))
pprint.log(math.sin(math.acos(0.95)))
pprint.log(math.acos(0.65))
pprint.log(math.sin(math.acos(0.65)))
}


test("0") {
def isScala212a(v: Option[(Long, Long)]): Boolean =
v.exists(_._1 == 2) && v.exists(_._2 == 12)
Expand Down
45 changes: 44 additions & 1 deletion ce3/src/main/scala/el_meter/Sandbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import cats.effect.IO
import cats.effect.unsafe.implicits.global
import cats.implicits._
import io.scalaland.chimney.Transformer

import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import javax.swing.text.DateFormatter
import org.http4s.Method
import org.http4s.Request
import org.http4s.Status.BadRequest
Expand All @@ -19,6 +22,10 @@ import org.http4s.implicits.http4sLiteralsSyntax
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import retry.{RetryDetails, RetryPolicy}
import retry.implicits.retrySyntaxError
import retryideas.RetryApp.x

import scala.concurrent.duration.DurationInt

object Http {
Expand Down Expand Up @@ -48,6 +55,25 @@ class SandboxSpec extends AnyFunSuite with Matchers with ScalaCheckPropertyCheck
import model._
// import io.scalaland.chimney.dsl._

def leftPad(x: Int, width: Int): String = s"%${width}d".formatted(x)
def leftPad(x: Double, width: Int): String = leftPad(x.toInt, width)

test("leftPad") {
val x = leftPad(1.123, 6)
pprint.log(x)
}

test("date formatted") {
val ldt = LocalDateTime.parse("2024-08-25T13:52")
val rep = ldt.format(DateTimeFormatter.ISO_DATE_TIME)
pprint.log(rep)
}

test("pprint") {
pprint
.log("Hello")
}

test("round") {
pprint.log(round1(1.23456))
}
Expand All @@ -72,9 +98,26 @@ class SandboxSpec extends AnyFunSuite with Matchers with ScalaCheckPropertyCheck
}

test("streamed") {

val policy: RetryPolicy[IO] = {
import retry.RetryPolicies._
// 6 retries starting from 1 gives us +1 +2 +4 +8 +16 +32 = 63 sec ~= 1 min
val growing: RetryPolicy[IO] = limitRetries[IO](5) join exponentialBackoff[IO](1.second)
// constant never terminating retry
val constant: RetryPolicy[IO] = constantDelay[IO](10.seconds)

growing followedBy constant
}

val onError = (t: Throwable, d: RetryDetails) =>
IO { println(LocalDateTime.now -> "device inaccessible retrying...") }

val getDataWithRetry = getData
.retryingOnAllErrors(policy, onError)

fs2.Stream
.awakeEvery[IO](5000.millis)
.evalMap(_ => getData)
.evalMap(_ => getDataWithRetry)
.unNone
.map(_.into[DataLine].transform)
.map(_.into[DataLineWattOnlyDetailed].transform)
Expand Down

0 comments on commit 9ed3965

Please sign in to comment.