alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (CometTest.java)

This example Play Framework source code file (CometTest.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, comet, comettest, countdownlatch, exception, iterate, jsonnode, lib, library, override, play, play framework, string, test, testchannel

The CometTest.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.libs;

import com.fasterxml.jackson.databind.JsonNode;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import play.api.libs.iteratee.TestChannel;
import play.libs.F;
import play.libs.Json;
import play.mvc.Results.Chunks;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.fest.assertions.Assertions.assertThat;

public class CometTest {

    @Test
    public void testOnReady() throws Exception {
        final TestChannel<String> testChannel = new TestChannel<String>();
        final Chunks.Out<String> out = new Chunks.Out<String>(testChannel, new ArrayList<F.Callback0>());
        final CountDownLatch invoked = new CountDownLatch(1);
        Comet comet = new Comet("test") {
            @Override
            public void onConnected() {
                invoked.countDown();
            }
        };
        comet.onReady(out);

        assertThat(invoked.await(1, SECONDS)).isTrue();
        testChannel.expect(comet.initialBuffer());
        testChannel.expectEmpty();
    }

    @Test
    public void testMessageSends() {
        final TestChannel<String> testChannel = new TestChannel<String>();
        final Chunks.Out<String> out = new Chunks.Out<String>(testChannel, new ArrayList<F.Callback0>());
        Comet comet = new Comet("test") {
            @Override
            public void onConnected() {}
        };
        comet.onReady(out);

        String message = "test";
        JsonNode json = Json.newObject().put("test", "data");
        String jsonMessage = Json.stringify(json);

        comet.sendMessage(message);
        comet.sendMessage(json);

        testChannel.expect(comet.initialBuffer());
        testChannel.expect("<script type=\"text/javascript\">test('" + message + "');</script>");
        testChannel.expect("<script type=\"text/javascript\">test(" + jsonMessage + ");</script>");
        testChannel.expectEmpty();
    }

    @Test
    public void testClose() throws Exception {
        final TestChannel<String> testChannel = new TestChannel<String>();
        final Chunks.Out<String> out = new Chunks.Out<String>(testChannel, new ArrayList<F.Callback0>());
        Comet comet = new Comet("test") {
            @Override
            public void onConnected() {}
        };
        comet.onReady(out);

        comet.close();

        testChannel.expect(comet.initialBuffer());
        testChannel.expectEOF();
        testChannel.expectEnd();
        testChannel.expectEmpty();
    }

    @Test
    public void testWhenConnectedFactory() throws Exception {
        final CountDownLatch invoked = new CountDownLatch(1);
        Comet comet = Comet.whenConnected("test", new F.Callback<Comet>() {
            @Override
            public void invoke(Comet c) {
                invoked.countDown();
            }
        });
        comet.onConnected();
        assertThat(invoked.await(1, SECONDS)).isTrue();
    }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework CometTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.