Skip to content

Commit 73007cd

Browse files
committed
added real test for broadcast.
1 parent 15a856e commit 73007cd

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ resolvers += Resolver.sonatypeRepo("snapshots")
99

1010
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
1111

12-
scalacOptions ++= Seq("-unchecked","-deprecation", "-feature" /* , "-Ymacro-debug-lite" , "-Ydebug" , "-Ylog:lambdalift" */ )
12+
scalacOptions ++= Seq("-unchecked","-deprecation", "-feature" /* , "-Ymacro-debug-lite" , "-Ydebug" , "-Ylog:lambdalift" */ )
1313

1414
libraryDependencies <+= scalaVersion( "org.scala-lang" % "scala-reflect" % _ )
1515

src/main/scala/gopher/util/Effected.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ trait Effected[T]
99

1010
@inline def <<=(f:T=>T): Unit = apply(f)
1111

12+
def replace(x: T): Unit = apply( _ => x)
13+
14+
@inline def :=(x:T): Unit = replace(x)
15+
1216
}
1317

1418

src/test/scala/example/BroadcasterSuite.scala

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package example.broadcast
77
*/
88

99
import scala.concurrent.{Channel=>_,_}
10+
import scala.concurrent.duration._
1011
import scala.concurrent.ExecutionContext.Implicits.global
12+
import scala.language.postfixOps
1113
import scala.async.Async._
1214

1315
import gopher._
@@ -25,10 +27,10 @@ class Broadcaster[A]
2527
val sendc: Channel[A] = makeChannel()
2628
val quitc: Channel[Boolean] = makeChannel()
2729

28-
val process = select.afold(makeChannel[Message[A]]()) { (last,s) =>
30+
val process = select.afold(makeChannel[Message[A]](1)) { (last,s) =>
2931
s match {
3032
case v: sendc.read @unchecked =>
31-
val next = makeChannel[Message[A]]()
33+
val next = makeChannel[Message[A]](1)
3234
last <~ ValueMessage(next,v)
3335
next
3436
case r: listenc.read @unchecked =>
@@ -47,11 +49,6 @@ class Broadcaster[A]
4749
new Receiver(c.read)
4850
}
4951

50-
51-
def write(a: A) = sendc.awrite(a)
52-
53-
def stop() = quitc.awrite(true)
54-
5552
}
5653

5754

@@ -76,7 +73,7 @@ object Broadcaster {
7673
current.write(b)
7774
b match {
7875
case ValueMessage(ch,v) =>
79-
current(_ => ch)
76+
current := ch
8077
Some(v)
8178
case EndMessage =>
8279
None
@@ -106,16 +103,48 @@ object Broadcaster {
106103
class BroadcaseSuite extends FunSuite
107104
{
108105

109-
def listen[A](r: Broadcaster.Receiver[A]): Unit = go {
110-
val x = await(r.aread)
111-
106+
def listen[A](r: Broadcaster.Receiver[A],out:Output[A]): Future[Unit] = go {
107+
var finish = false;
108+
while(!finish) {
109+
val x = await(r.aread)
110+
// can't use foreach inside 'go' block.
111+
if (!x.isEmpty) {
112+
out.write(x.get)
113+
} else {
114+
finish = true
115+
}
116+
}
117+
();
112118
}
113119

114-
test("broadcast") {
120+
def doBroadcast(out:Channel[Int]): Unit = go {
115121

116122
val b = new Broadcaster[Int]()
117123

124+
val r1 = await(b.alisten())
125+
val l1 = listen(r1,out)
126+
val r2 = await(b.alisten())
127+
val l2 = listen(r2,out)
128+
129+
b.sendc.write(1)
118130

131+
val r3 = await(b.alisten())
132+
val l3 = listen(r3,out)
133+
134+
b.sendc.write(2)
135+
136+
b.quitc.write(true)
137+
138+
Thread.sleep(500)
139+
out.close()
140+
}
141+
142+
test("broadcast") {
143+
val channel = makeChannel[Int]()
144+
doBroadcast(channel);
145+
val fsum = channel.afold(0){ (s,n) => s+n }
146+
val sum = Await.result(fsum,10 seconds)
147+
assert(sum==8)
119148
}
120149

121150
}

src/test/scala/gopher/scope/GoWithDeferSuite.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ class GoWithDeferSuite extends FunSuite {
3030
assert(Await.result(s, 1 second)=="CCC")
3131
}
3232

33+
test("2.2. go with defer and while") {
34+
var x = 0;
35+
var f:Future[Unit] = go {
36+
defer{ x=3; }
37+
var n=4;
38+
while(n > 0) {
39+
n = n-1;
40+
}
41+
}
42+
Await.ready(f, 1 second)
43+
assert(x === 3)
44+
}
45+
3346
// TODO: go with select.
3447

3548
}

0 commit comments

Comments
 (0)