Пример #1
0
  @Test
  public void testWaitFor() throws Exception {
    final BlockingArrayQueue<String> results = new BlockingArrayQueue<>();

    String channelName = "/chat/msg";
    MarkedReference<ServerChannel> channel = bayeux.createChannelIfAbsent(channelName);
    channel
        .getReference()
        .addListener(
            new ServerChannel.MessageListener() {
              public boolean onMessage(ServerSession from, ServerChannel channel, Mutable message) {
                results.add(from.getId());
                results.add(channel.getId());
                results.add(String.valueOf(message.getData()));
                return true;
              }
            });

    BayeuxClient client = newBayeuxClient();
    long wait = 1000L;
    long start = System.nanoTime();
    client.handshake(wait);
    long stop = System.nanoTime();
    Assert.assertTrue(TimeUnit.NANOSECONDS.toMillis(stop - start) < wait);
    Assert.assertNotNull(client.getId());
    String data = "Hello World";
    client.getChannel(channelName).publish(data);

    Assert.assertEquals(client.getId(), results.poll(1, TimeUnit.SECONDS));
    Assert.assertEquals(channelName, results.poll(1, TimeUnit.SECONDS));
    Assert.assertEquals(data, results.poll(1, TimeUnit.SECONDS));

    disconnectBayeuxClient(client);
  }
Пример #2
0
  @Test
  public void testPublish() throws Exception {
    final BlockingArrayQueue<String> results = new BlockingArrayQueue<>();

    String channelName = "/chat/msg";
    MarkedReference<ServerChannel> channel = bayeux.createChannelIfAbsent(channelName);
    channel
        .getReference()
        .addListener(
            new ServerChannel.MessageListener() {
              public boolean onMessage(ServerSession from, ServerChannel channel, Mutable message) {
                results.add(from.getId());
                results.add(channel.getId());
                results.add(String.valueOf(message.getData()));
                return true;
              }
            });

    BayeuxClient client = newBayeuxClient();
    client.handshake();
    Assert.assertTrue(client.waitFor(5000, State.CONNECTED));

    String data = "Hello World";
    client.getChannel(channelName).publish(data);

    String id = results.poll(10, TimeUnit.SECONDS);
    Assert.assertEquals(client.getId(), id);
    Assert.assertEquals(channelName, results.poll(10, TimeUnit.SECONDS));
    Assert.assertEquals(data, results.poll(10, TimeUnit.SECONDS));

    disconnectBayeuxClient(client);
  }