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

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

This example Play Framework source code file (EventSourceTest.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, countdownlatch, eventsource, eventsourcetest, exception, iterate, lib, library, override, play, play framework, test, testchannel

The EventSourceTest.java Play Framework example source code

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

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;
import static play.libs.EventSource.Event.event;

public class EventSourceTest {

    @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);
        EventSource eventSource = new EventSource() {
            @Override
            public void onConnected() {
                invoked.countDown();
            }
        };
        eventSource.onReady(out);

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

    @Test
    public void testSends() {
        final TestChannel<String> testChannel = new TestChannel<String>();
        final Chunks.Out<String> out = new Chunks.Out<String>(testChannel, new ArrayList<F.Callback0>());
        EventSource eventSource = new EventSource() {
            @Override
            public void onConnected() {}
        };
        eventSource.onReady(out);

        eventSource.send(new EventSource.Event("data1", "id1", "name1"));
        eventSource.send(event("data2"));
        eventSource.send(event("data3").withId("id3"));
        eventSource.send(event("data4").withName("name4"));
        eventSource.send(event("data5").withId("id5").withName("name5"));
        eventSource.send(event(Json.newObject().put("test", "data")));

        testChannel.expect("event: name1\nid: id1\ndata: data1\n\n");
        testChannel.expect("data: data2\n\n");
        testChannel.expect("id: id3\ndata: data3\n\n");
        testChannel.expect("event: name4\ndata: data4\n\n");
        testChannel.expect("event: name5\nid: id5\ndata: data5\n\n");
        testChannel.expect("data: {\"test\":\"data\"}\n\n");
        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>());
        EventSource eventSource = new EventSource() {
            @Override
            public void onConnected() {}
        };
        eventSource.onReady(out);

        eventSource.close();

        testChannel.expectEOF();
        testChannel.expectEnd();
        testChannel.expectEmpty();
    }

    @Test
    public void testWhenConnectedFactory() throws Exception {
        final CountDownLatch invoked = new CountDownLatch(1);
        EventSource eventSource = EventSource.whenConnected(new F.Callback<EventSource>() {
            @Override
            public void invoke(EventSource es) {
                invoked.countDown();
            }
        });
        eventSource.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 EventSourceTest.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.