Search in sources :

Example 1 with AbstractFuture

use of com.google.common.util.concurrent.AbstractFuture in project closure-templates by google.

the class RenderVisitorTest method testDelayedCheckingOfCachingProviders.

@Test
public void testDelayedCheckingOfCachingProviders() {
    String soyFileContent = "{namespace ns}\n" + "\n" + "{template .template}\n" + "  {@param foo: int}\n" + "  Before: {$foo}\n" + "{/template}\n";
    final StringBuilder outputSb = new StringBuilder();
    final AtomicReference<String> outputAtFutureGetTime = new AtomicReference<>();
    AbstractFuture<Integer> fooFuture = new AbstractFuture<Integer>() {

        {
            set(1);
        }

        @Override
        public Integer get() throws InterruptedException, ExecutionException {
            outputAtFutureGetTime.set(outputSb.toString());
            return super.get();
        }
    };
    SoyRecord data = SoyValueConverterUtility.newDict("foo", fooFuture);
    assertThat(renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent).parse(), "ns.template", data, TEST_IJ_DATA, Predicates.<String>alwaysFalse(), outputSb)).isEqualTo("Before: 1");
    assertThat(outputAtFutureGetTime.get()).isEqualTo("Before: ");
}
Also used : SoyRecord(com.google.template.soy.data.SoyRecord) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractFuture(com.google.common.util.concurrent.AbstractFuture) Test(org.junit.Test)

Example 2 with AbstractFuture

use of com.google.common.util.concurrent.AbstractFuture in project closure-templates by google.

the class RenderVisitorTest method testStreamLazyParamsToOutputStreamDirectly.

@Test
public void testStreamLazyParamsToOutputStreamDirectly() {
    String soyFileContent = Joiner.on("\n").join("{namespace ns}", "", "{template .callee}", "  {@param body: html}", "  <div>", "    {$body}", "  </div>", "{/template}", "", "{template .caller}", "  {@param future: string}", "  {call .callee}", "    {param body kind=\"html\"}", "      static-content{sp}", "      {$future}", "    {/param}", "  {/call}", "{/template}");
    final StringBuilder outputSb = new StringBuilder();
    final AtomicReference<String> outputAtFutureGetTime = new AtomicReference<>();
    AbstractFuture<String> future = new AbstractFuture<String>() {

        {
            set("future-content");
        }

        @Override
        public String get() throws InterruptedException, ExecutionException {
            outputAtFutureGetTime.set(outputSb.toString());
            return super.get();
        }
    };
    SoyRecord data = SoyValueConverterUtility.newDict("future", future);
    assertThat(renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent).parse(), "ns.caller", data, TEST_IJ_DATA, Predicates.<String>alwaysFalse(), outputSb)).isEqualTo("<div>static-content future-content</div>");
    // we only get the <div>.  we used to get the 'static-content' as well but that was only
    // because we aren't using the autoescaper.
    assertThat(outputAtFutureGetTime.get()).isEqualTo("<div>");
}
Also used : SoyRecord(com.google.template.soy.data.SoyRecord) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractFuture(com.google.common.util.concurrent.AbstractFuture) Test(org.junit.Test)

Aggregations

AbstractFuture (com.google.common.util.concurrent.AbstractFuture)2 SoyRecord (com.google.template.soy.data.SoyRecord)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2