Skip to content

Commit 1478888

Browse files
author
Sam Pullara
committed
Merge branch 'master' of github.com:spullara/mustache.java
2 parents 8d30aeb + 3e60be1 commit 1478888

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

compiler/src/main/java/com/github/mustachejava/codes/IterableCode.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import java.util.concurrent.ExecutorService;
1818

1919
/**
20-
* Created by IntelliJ IDEA.
21-
* User: spullara
22-
* Date: 1/9/12
23-
* Time: 2:57 PM
24-
* To change this template use File | Settings | File Templates.
25-
*/
20+
* Created by IntelliJ IDEA.
21+
* User: spullara
22+
* Date: 1/9/12
23+
* Time: 2:57 PM
24+
* To change this template use File | Settings | File Templates.
25+
*/
2626
public class IterableCode extends DefaultCode implements Iteration {
2727

2828
private final String variable;

compiler/src/main/java/com/github/mustachejava/util/LatchedWriter.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,27 @@ public LatchedWriter(Writer writer) {
3333
}
3434

3535
// Call this when your processing is complete
36-
public synchronized void done() {
36+
public synchronized void done() throws IOException {
37+
writer.append(buffer);
3738
latch.countDown();
3839
}
3940

4041
// If you fail to complete, put an exception here
4142
public void failed(Throwable e) {
4243
this.e = e;
43-
done();
44+
latch.countDown();
4445
}
4546

4647
@Override
4748
public synchronized void write(char[] cbuf, int off, int len) throws IOException {
4849
checkException();
4950
if (latch.getCount() == 0) {
50-
checkWrite();
5151
writer.write(cbuf, off, len);
5252
} else {
5353
buffer.append(cbuf, off, len);
5454
}
5555
}
5656

57-
private void checkWrite() throws IOException {
58-
if (!isWritten) {
59-
isWritten = true;
60-
writer.append(buffer);
61-
}
62-
}
63-
6457
private void checkException() throws IOException {
6558
if (e != null) {
6659
if (e instanceof IOException) {
@@ -76,7 +69,6 @@ public void flush() throws IOException {
7669
if (latch.getCount() == 0) {
7770
checkException();
7871
synchronized (this) {
79-
checkWrite();
8072
writer.flush();
8173
}
8274
}

compiler/src/test/java/com/github/mustachejava/InterpreterTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ int taxed_value() {
4545
assertEquals(getContents(root, "simple.txt"), sw.toString());
4646
}
4747

48+
public void testNestedLatches() throws IOException {
49+
MustacheFactory c = new DefaultMustacheFactory(root);
50+
Mustache m = c.compile("latchedtest.html");
51+
StringWriter sw = new StringWriter();
52+
m.execute(sw, new Object() {
53+
Callable<Object> nest = new Callable<Object>() {
54+
@Override
55+
public Object call() throws Exception {
56+
Thread.sleep(300);
57+
return "How";
58+
}
59+
};
60+
Callable<Object> nested = new Callable<Object>() {
61+
@Override
62+
public Object call() throws Exception {
63+
Thread.sleep(200);
64+
return "are";
65+
}
66+
};
67+
Callable<Object> nestest = new Callable<Object>() {
68+
@Override
69+
public Object call() throws Exception {
70+
Thread.sleep(100);
71+
return "you?";
72+
}
73+
};
74+
});
75+
assertEquals(getContents(root, "latchedtest.txt"), sw.toString());
76+
}
77+
4878
public void testBrokenSimple() throws MustacheException, IOException, ExecutionException, InterruptedException {
4979
try {
5080
MustacheFactory c = init();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{#nest}}
2+
{{.}}
3+
{{#nested}}
4+
{{.}}
5+
{{#nestest}}
6+
{{/nestest}}
7+
{{/nested}}
8+
{{#nested}}
9+
{{#nestest}}
10+
{{.}}
11+
{{/nestest}}
12+
{{/nested}}
13+
{{/nest}}

src/test/resources/latchedtest.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
How
2+
are
3+
you?

0 commit comments

Comments
 (0)