Skip to content

Commit

Permalink
Remove lastAcquiredKey, reuse key from acquired
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 19, 2023
1 parent 714b0d1 commit d28d01b
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 17 deletions.
1 change: 0 additions & 1 deletion app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ object Executor:
level = req.level,
clock = req.clock,
tries = 0,
lastTryByKey = None,
acquired = None,
createdAt = now,
)
4 changes: 1 addition & 3 deletions app/src/main/scala/Work.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ object Work:
level: Int,
clock: Option[Work.Clock],
tries: Int,
lastTryByKey: Option[ClientKey],
acquired: Option[Acquired],
createdAt: Instant,
):
Expand All @@ -33,7 +32,7 @@ object Work:
def isAcquiredBy(clientKey: ClientKey) = acquiredByKey contains clientKey
def isAcquired = acquired.isDefined
def nonAcquired = !isAcquired
def canAcquire(clientKey: ClientKey) = lastTryByKey.fold(true)(clientKey.!=)
def canAcquire(clientKey: ClientKey) = acquired.fold(true)(_.clientKey != clientKey)
def acquiredBefore(date: Instant) = acquiredAt.fold(false)(_.isBefore(date))

def assignTo(clientKey: ClientKey, at: Instant) =
Expand All @@ -44,7 +43,6 @@ object Work:
date = at,
)
),
lastTryByKey = Some(clientKey),
tries = tries + 1,
)

Expand Down
15 changes: 2 additions & 13 deletions app/src/test/scala/ExecutorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ object ExecutorTest extends SimpleIOSuite with Checkers:
clock = None,
)

// val work = Work.Move(
// _id = workId,
// game = game,
// level = 1,
// clock = None,
// tries = 0,
// lastTryByKey = None,
// acquired = None,
// createdAt = Instant.now,
// )

val key = ClientKey("key")

val acquiredKey = MoveDb.Acquire(key)
Expand All @@ -54,7 +43,7 @@ object ExecutorTest extends SimpleIOSuite with Checkers:
acquired = acquiredOption.get
yield assert(acquired.acquired.get.clientKey == key)
`and` assert(acquired.tries == 1)
`and` assert(acquired.lastTryByKey == key.some)
`and` assert(acquired.acquired.get.clientKey == key)
`and` assert(acquired.toRequest == request)

test("after acquire the only work, acquire again should return none"):
Expand Down Expand Up @@ -124,7 +113,7 @@ object ExecutorTest extends SimpleIOSuite with Checkers:
acquired = acquiredOption.get
yield assert(acquired.acquired.get.clientKey == key)
`and` assert(acquired.tries == 2)
`and` assert(acquired.lastTryByKey == key.some)
`and` assert(acquired.acquired.get.clientKey == key)
`and` assert(acquired.toRequest == request)

test("should not give up after 2 tries"):
Expand Down

0 comments on commit d28d01b

Please sign in to comment.