Skip to content

Commit d3c7863

Browse files
committed
hof-async lifting inside select
1 parent 753887c commit d3c7863

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/scala/gopher/channels/SelectorBuilder.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import scala.reflect.macros.blackbox.Context
55
import scala.reflect.api._
66
import gopher._
77
import gopher.util._
8+
import gopher.goasync._
89
import scala.concurrent._
910
import scala.concurrent.duration._
1011
import scala.annotation.unchecked._
@@ -171,10 +172,11 @@ object SelectorBuilder
171172
val ftParam = ValDef(Modifiers(Flag.PARAM),ft,tq"gopher.FlowTermination[${weakTypeOf[T]}]",EmptyTree)
172173
val ecParam = ValDef(Modifiers(Flag.PARAM),ec,tq"scala.concurrent.ExecutionContext",EmptyTree)
173174
val nvaldefs = ecParam::ftParam::valdefs
175+
val asyncBody = GoAsync.transformAsyncBody[T](c)(body)
174176
val nbody = q"""{
175177
implicit val ${ft1} = ${ft}
176178
implicit val ${ec1} = ${ec}
177-
scala.async.Async.async(${transformDelayedMacroses[T](c)(body)})(${ec})
179+
scala.async.Async.async(${transformDelayedMacroses[T](c)(asyncBody)})(${ec})
178180
}
179181
"""
180182
val newTree = lastFun(nvaldefs,nbody)

src/test/scala/gopher/hofasyn/HofAsyncSuite.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ class HofAsyncSuite extends FunSuite
113113
assert(r == 1)
114114
}
115115

116+
test("lift inside select") {
117+
import gopherApi._
118+
val ch1 = makeChannel[Int](10)
119+
val ch2 = makeChannel[Int](10)
120+
val quit = makeChannel[Boolean]()
121+
val fin = go{
122+
for(s <- select.forever)
123+
s match {
124+
case x: ch1.read =>
125+
//System.err.println(s"received $x")
126+
for(i <- 1 to x) {
127+
//System.err.println(s"writing ${i*x}")
128+
ch2.write(i*x)
129+
}
130+
case y: select.timeout if (y==(500.milliseconds)) =>
131+
System.err.println(s"timeout $y")
132+
case z: quit.read =>
133+
select.exit(())
134+
}
135+
}
136+
val fout = ch1.awrite(2)
137+
val x1 = ch2.aread
138+
val rx1 = Await.result(x1, 1 minute)
139+
quit.awrite(true)
140+
assert(rx1 == 2)
141+
}
142+
116143
lazy val gopherApi = CommonTestObjects.gopherApi
117144

118145
}

0 commit comments

Comments
 (0)