Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Using or scopeExit in plain functions #253

Open
wants to merge 7 commits into
base: 1.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
local.sbt
secret/
.idea
1 change: 1 addition & 0 deletions keywords-Using/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ scalacOptions ++= {
}

libraryDependencies += "com.thoughtworks.dsl" %%% "keywords-await" % "1.3.1" % Test
libraryDependencies += "com.thoughtworks.dsl" %%% "domains-task" % "1.3.1" % Test
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ import scala.util.control.NonFatal
*/
final case class Using[R <: AutoCloseable](open: () => R) extends AnyVal with Keyword[Using[R], R]

object Using {
trait LowPriorityUsing {

implicit def usingDsl[R <: AutoCloseable, Domain]: Dsl[Using[R], Domain, R] =
new Dsl[Using[R], Domain, R] {
def cpsApply(keyword: Using[R], handler: R => Domain): Domain = {
val r = keyword.open()
try {
handler(r)
} finally {
r.close()
}
}
}

}

object Using extends LowPriorityUsing {

implicit def implicitUsing[R <: AutoCloseable](r: => R): Using[R] = Using[R](r _)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.thoughtworks.dsl.keywords

import java.util.concurrent.LinkedBlockingQueue

import org.scalatest.{FreeSpec, Matchers}
import com.thoughtworks.dsl.domains.task.Task
import scala.concurrent.duration._

class UsingSpec extends FreeSpec with Matchers {

"scopeExit" - {
import Using.scopeExit

"execute sequence: function" in {
val queue = new LinkedBlockingQueue[Int]()

def run() = {
!scopeExit(() => queue.offer(1))
queue.offer(2)
}

run()

queue.poll() should be(2)
queue.poll() should be(1)
}

"execute sequence: function contains call-by-name function call" in {
val queue = new LinkedBlockingQueue[Int]()

def runOp(op: => Unit): Unit = op

def run() = {
!scopeExit(() => queue.offer(1))
runOp {
!scopeExit(() => queue.offer(3))
}
queue.offer(2)
}

run()

queue.poll() should be(3)
queue.poll() should be(2)
queue.poll() should be(1)
}

"execute sequence: function contains call of function accepts function literal execute sequence" in {
val queue = new LinkedBlockingQueue[Int]()

def runOp(op: () => Unit): Unit = op()

def run() = {
!scopeExit(() => queue.offer(1))
runOp(() => !scopeExit(() => queue.offer(3)))
queue.offer(2)
}

run()

queue.poll() should be(3)
queue.poll() should be(2)
queue.poll() should be(1)
}

"execute sequence: task" in {
val queue = new LinkedBlockingQueue[Int]()

def run() = Task {
!scopeExit(() => queue.offer(1))
queue.offer(2)
}

Task.blockingAwait(run(), 1.minute)

queue.poll() should be(2)
queue.poll() should be(1)
}


"execute sequence: task contains call-by-name function call" in {
val queue = new LinkedBlockingQueue[Int]()

def runTask(op: => Task[Unit]): Unit = Task.blockingAwait(op, 1.minute)

def run() = Task {
!scopeExit(() => queue.offer(1))
runTask {
Task(!scopeExit(() => queue.offer(3)))
}
queue.offer(2)
}

Task.blockingAwait(run(), 1.minute)

queue.poll() should be(3)
queue.poll() should be(2)
queue.poll() should be(1)
}

"execute sequence: task contains call of function accepts function literal execute sequence" in {
val queue = new LinkedBlockingQueue[Int]()

def runOp(op: () => Task[Unit]): Unit = Task.blockingAwait(op(), 1.minute)

def run() = {
!scopeExit(() => queue.offer(1))
runOp(() => Task(!scopeExit(() => queue.offer(3))))
queue.offer(2)
}

run()

queue.poll() should be(3)
queue.poll() should be(2)
queue.poll() should be(1)
}

}

}