Skip to content

Commit 43e3a9f

Browse files
committed
WIP fix memory leak
1 parent e106336 commit 43e3a9f

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

server/src/test/scala/org/scalaexercises/evaluator/EvaluatorSpec.scala

+14-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ class EvaluatorSpec extends AnyFunSpec with Matchers with Implicits {
4040
}
4141
}
4242

43+
it("can evaluate simple expressions, for Scala 2.13") {
44+
val result: EvalResult[Int] = evaluator
45+
.eval("{ 41 + 1 }", remotes = commonResolvers, dependencies = scalaDependencies(Scala213))
46+
.unsafeRunSync()
47+
48+
result should matchPattern {
49+
case EvalSuccess(_, 42, _) =>
50+
}
51+
}
52+
4353
it("fails with a timeout when takes longer than the configured timeout") {
4454
val result: EvalResult[Int] = evaluator
4555
.eval(
@@ -54,7 +64,8 @@ class EvaluatorSpec extends AnyFunSpec with Matchers with Implicits {
5464
}
5565

5666
it("can load dependencies for an evaluation") {
57-
val code = """
67+
val code =
68+
"""
5869
import cats.data.Xor
5970
6071
Xor.Right(42).toOption.get
@@ -95,7 +106,8 @@ Xor.Right(42).toOption.get
95106
}
96107

97108
it("can load different versions of a dependency across evaluations") {
98-
val code = """
109+
val code =
110+
"""
99111
import cats._
100112
Eval.now(42).value
101113
"""

server/src/test/scala/org/scalaexercises/evaluator/helper.scala

+38-38
Original file line numberDiff line numberDiff line change
@@ -60,60 +60,60 @@ Asserts.scalaTestAsserts($assertCheck)
6060

6161
val fetchCode =
6262
"""
63-
import java.util.concurrent.ScheduledThreadPoolExecutor
63+
import java.util.concurrent.ScheduledThreadPoolExecutor
6464
65-
import scala.concurrent.ExecutionContext
65+
import scala.concurrent.ExecutionContext
6666
67-
import cats.data.NonEmptyList
68-
import cats.effect._
67+
import cats.data.NonEmptyList
68+
import cats.effect._
6969
70-
import fetch._
71-
import cats.implicits._
70+
import fetch._
71+
import cats.implicits._
7272
73-
val executor = new ScheduledThreadPoolExecutor(4)
74-
val executionContext: ExecutionContext = ExecutionContext.fromExecutor(executor)
73+
val executor = new ScheduledThreadPoolExecutor(4)
74+
val executionContext: ExecutionContext = ExecutionContext.fromExecutor(executor)
7575
76-
implicit val timer: Timer[IO] = IO.timer(executionContext)
77-
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)
76+
implicit val timer: Timer[IO] = IO.timer(executionContext)
77+
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)
7878
79-
type UserId = Int
79+
type UserId = Int
8080
81-
case class User(id: UserId, username: String)
81+
case class User(id: UserId, username: String)
8282
83-
def latency[F[_]: Concurrent](msg: String): F[Unit] =
84-
for {
85-
_ <- Sync[F].delay(println(s"--> [${Thread.currentThread.getId}] $msg"))
86-
_ <- Sync[F].delay(Thread.sleep(100))
87-
_ <- Sync[F].delay(println(s"<-- [${Thread.currentThread.getId}] $msg"))
88-
} yield ()
83+
def latency[F[_]: Concurrent](msg: String): F[Unit] =
84+
for {
85+
_ <- Sync[F].delay(println(s"--> [${Thread.currentThread.getId}] $msg"))
86+
_ <- Sync[F].delay(Thread.sleep(100))
87+
_ <- Sync[F].delay(println(s"<-- [${Thread.currentThread.getId}] $msg"))
88+
} yield ()
8989
90-
val userDatabase: Map[UserId, User] = Map(
91-
1 -> User(1, "@one"),
92-
2 -> User(2, "@two"),
93-
3 -> User(3, "@three"),
94-
4 -> User(4, "@four")
95-
)
90+
val userDatabase: Map[UserId, User] = Map(
91+
1 -> User(1, "@one"),
92+
2 -> User(2, "@two"),
93+
3 -> User(3, "@three"),
94+
4 -> User(4, "@four")
95+
)
9696
97-
object Users extends Data[UserId, User] {
98-
def name = "Users"
97+
object Users extends Data[UserId, User] {
98+
def name = "Users"
9999
100-
def source[F[_]: Concurrent]: DataSource[F, UserId, User] = new DataSource[F, UserId, User] {
101-
override def data = Users
100+
def source[F[_]: Concurrent]: DataSource[F, UserId, User] = new DataSource[F, UserId, User] {
101+
override def data = Users
102102
103-
def CF = Concurrent[F]
103+
def CF = Concurrent[F]
104104
105-
override def fetch(id: UserId): F[Option[User]] =
106-
latency[F](s"One User $id") >> CF.pure(userDatabase.get(id))
105+
override def fetch(id: UserId): F[Option[User]] =
106+
latency[F](s"One User $id") >> CF.pure(userDatabase.get(id))
107107
108-
override def batch(ids: NonEmptyList[UserId]): F[Map[UserId, User]] =
109-
latency[F](s"Batch Users $ids") >> CF.pure(userDatabase.filterKeys(ids.toList.toSet))
110-
}
111-
}
108+
override def batch(ids: NonEmptyList[UserId]): F[Map[UserId, User]] =
109+
latency[F](s"Batch Users $ids") >> CF.pure(userDatabase.view.filterKeys(ids.toList.toSet).toMap)
110+
}
111+
}
112112
113-
def getUser[F[_]: Concurrent](id: UserId): Fetch[F, User] = Fetch[F, UserId, User](id, Users.source)
113+
def getUser[F[_]: Concurrent](id: UserId): Fetch[F, User] = Fetch[F, UserId, User](id, Users.source)
114114
115-
def fetchUser[F[_] : Concurrent]: Fetch[F, User] = getUser(1)
115+
def fetchUser[F[_] : Concurrent]: Fetch[F, User] = getUser(1)
116116
117-
Fetch.run[IO](fetchUser).unsafeRunSync()
117+
Fetch.run[IO](fetchUser).unsafeRunSync()
118118
"""
119119
}

0 commit comments

Comments
 (0)