Search in sources :

Example 21 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class ChannelTestBase method testSeveralWriteMessage.

@Test
public void testSeveralWriteMessage() throws Exception {
    final AtomicBoolean wasEmpty = new AtomicBoolean();
    final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
    final CountDownLatch latch = new CountDownLatch(50);
    final AtomicInteger count = new AtomicInteger();
    recvChannel.receiveMessage(new Channel.Receiver() {

        public void handleError(final Channel channel, final IOException error) {
            error.printStackTrace();
            exRef.set(error);
            latch.countDown();
        }

        public void handleEnd(final Channel channel) {
            System.out.println("End of channel");
            latch.countDown();
        }

        public void handleMessage(final Channel channel, final MessageInputStream message) {
            System.out.println("Message received");
            try {
                if (message.read() == -1) {
                    wasEmpty.set(true);
                }
                message.close();
            } catch (IOException e) {
                exRef.set(e);
            } finally {
                IoUtils.safeClose(message);
                latch.countDown();
                if (count.getAndIncrement() < 50) {
                    recvChannel.receiveMessage(this);
                }
            }
        }
    });
    for (int i = 0; i < 50; i++) {
        MessageOutputStream messageOutputStream = sendChannel.writeMessage();
        messageOutputStream.close();
        // close should be idempotent
        messageOutputStream.close();
        // no effect expected, since message is closed
        messageOutputStream.flush();
    }
    latch.await();
    IOException exception = exRef.get();
    if (exception != null) {
        throw exception;
    }
    assertTrue(wasEmpty.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageOutputStream(org.jboss.remoting3.MessageOutputStream) MessageInputStream(org.jboss.remoting3.MessageInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(org.jboss.remoting3.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 22 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class ConnectionTestCase method testManyChannelsLotsOfData.

@Test
@Ignore
public void testManyChannelsLotsOfData() throws Exception {
    final XnioWorker clientWorker = clientEndpoint.getXnioWorker();
    final XnioWorker serverWorker = serverEndpoint.getXnioWorker();
    final Queue<Throwable> problems = new ConcurrentLinkedQueue<Throwable>();
    final CountDownLatch serverChannelCount = new CountDownLatch(CHANNEL_COUNT * CONNECTION_COUNT);
    final CountDownLatch clientChannelCount = new CountDownLatch(CHANNEL_COUNT * CONNECTION_COUNT);
    serverEndpoint.registerService("test", new OpenListener() {

        public void channelOpened(final Channel channel) {
            channel.receiveMessage(new Channel.Receiver() {

                public void handleError(final Channel channel, final IOException error) {
                    problems.add(error);
                    error.printStackTrace();
                    serverChannelCount.countDown();
                }

                public void handleEnd(final Channel channel) {
                    serverChannelCount.countDown();
                }

                public void handleMessage(final Channel channel, final MessageInputStream message) {
                    try {
                        channel.receiveMessage(this);
                        while (message.read(junkBuffer) > -1) ;
                    } catch (Exception e) {
                        e.printStackTrace();
                        problems.add(e);
                    } finally {
                        IoUtils.safeClose(message);
                    }
                }
            });
        }

        public void registrationTerminated() {
        }
    }, OptionMap.EMPTY);
    final AtomicReferenceArray<Connection> connections = new AtomicReferenceArray<Connection>(CONNECTION_COUNT);
    for (int h = 0; h < CONNECTION_COUNT; h++) {
        IoFuture<Connection> futureConnection = AuthenticationContext.empty().with(MatchRule.ALL, AuthenticationConfiguration.empty().useName("bob").usePassword("pass").setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("SCRAM-SHA-256"))).run(new PrivilegedAction<IoFuture<Connection>>() {

            public IoFuture<Connection> run() {
                try {
                    return clientEndpoint.connect(new URI("remote://localhost:30123"), OptionMap.EMPTY);
                } catch (URISyntaxException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        final Connection connection = futureConnection.get();
        connections.set(h, connection);
        for (int i = 0; i < CHANNEL_COUNT; i++) {
            clientWorker.execute(new Runnable() {

                public void run() {
                    final Random random = new Random();
                    final IoFuture<Channel> future = connection.openChannel("test", OptionMap.EMPTY);
                    try {
                        final Channel channel = future.get();
                        try {
                            final byte[] bytes = new byte[BUFFER_SIZE];
                            for (int j = 0; j < MESSAGE_COUNT; j++) {
                                final MessageOutputStream stream = channel.writeMessage();
                                try {
                                    for (int k = 0; k < 100; k++) {
                                        random.nextBytes(bytes);
                                        stream.write(bytes, 0, random.nextInt(BUFFER_SIZE - 1) + 1);
                                    }
                                    stream.close();
                                } finally {
                                    IoUtils.safeClose(stream);
                                }
                                stream.close();
                            }
                        } finally {
                            IoUtils.safeClose(channel);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        problems.add(e);
                    } finally {
                        clientChannelCount.countDown();
                    }
                }
            });
        }
    }
    Thread.sleep(500);
    serverChannelCount.await();
    clientChannelCount.await();
    for (int h = 0; h < CONNECTION_COUNT; h++) {
        connections.get(h).close();
    }
    assertArrayEquals(new Object[0], problems.toArray());
}
Also used : MessageInputStream(org.jboss.remoting3.MessageInputStream) XnioWorker(org.xnio.XnioWorker) OpenListener(org.jboss.remoting3.OpenListener) Channel(org.jboss.remoting3.Channel) Connection(org.jboss.remoting3.Connection) IoFuture(org.xnio.IoFuture) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) Endpoint(org.jboss.remoting3.Endpoint) MessageOutputStream(org.jboss.remoting3.MessageOutputStream) Random(java.util.Random) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class ConnectionTestCase method testChannelOptions.

@Test
public void testChannelOptions() throws Exception {
    serverEndpoint.registerService("test", new OpenListener() {

        @Override
        public void channelOpened(Channel channel) {
            // 
            Assert.assertTrue(channel.getOption(RemotingOptions.RECEIVE_WINDOW_SIZE) <= MAX_SERVER_RECEIVE);
            Assert.assertTrue(channel.getOption(RemotingOptions.TRANSMIT_WINDOW_SIZE) <= MAX_SERVER_TRANSMIT);
        }

        @Override
        public void registrationTerminated() {
        // 
        }
    }, OptionMap.create(RemotingOptions.RECEIVE_WINDOW_SIZE, MAX_SERVER_RECEIVE, RemotingOptions.TRANSMIT_WINDOW_SIZE, MAX_SERVER_TRANSMIT));
    final Connection connection = AuthenticationContext.empty().with(MatchRule.ALL, AuthenticationConfiguration.empty().useName("bob").usePassword("pass").setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("SCRAM-SHA-256"))).run(new PrivilegedAction<Connection>() {

        public Connection run() {
            try {
                return clientEndpoint.connect(new URI("remote://localhost:30123"), OptionMap.EMPTY).get();
            } catch (IOException | URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    });
    IoFuture<Channel> future = connection.openChannel("test", OptionMap.create(RemotingOptions.RECEIVE_WINDOW_SIZE, 0x8000, RemotingOptions.TRANSMIT_WINDOW_SIZE, 0x12000));
    Channel channel = future.get();
    try {
        Assert.assertEquals("transmit", 0x12000, (int) channel.getOption(RemotingOptions.TRANSMIT_WINDOW_SIZE));
        Assert.assertEquals("receive", 0x8000, (int) channel.getOption(RemotingOptions.RECEIVE_WINDOW_SIZE));
    } finally {
        if (channel != null) {
            channel.close();
        }
    }
    future = connection.openChannel("test", OptionMap.create(RemotingOptions.RECEIVE_WINDOW_SIZE, 0x24000, RemotingOptions.TRANSMIT_WINDOW_SIZE, 0x24000));
    channel = future.get();
    try {
        Assert.assertEquals("transmit", MAX_SERVER_RECEIVE, (int) channel.getOption(RemotingOptions.TRANSMIT_WINDOW_SIZE));
        Assert.assertEquals("receive", MAX_SERVER_TRANSMIT, (int) channel.getOption(RemotingOptions.RECEIVE_WINDOW_SIZE));
    } finally {
        if (channel != null) {
            channel.close();
        }
    }
}
Also used : OpenListener(org.jboss.remoting3.OpenListener) Channel(org.jboss.remoting3.Channel) Connection(org.jboss.remoting3.Connection) URI(java.net.URI) Test(org.junit.Test)

Example 24 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class OutboundMessageCountTestCase method testOutboundMessageSend.

/**
 * Tests that multiple threads opening and closing a message on the channel doesn't cause the JBoss Remoting
 * code to get out of sync with the current outbound message count it maintains.
 *
 * @throws Exception
 */
@Test
public void testOutboundMessageSend() throws Exception {
    serverChannel.receiveMessage(new Channel.Receiver() {

        public void handleError(final Channel channel, final IOException error) {
        }

        public void handleEnd(final Channel channel) {
        }

        public void handleMessage(final Channel channel, final MessageInputStream message) {
            channel.receiveMessage(this);
            try {
                while (message.read() != -1) ;
            } catch (IOException ignored) {
            } finally {
                safeClose(message);
            }
        }
    });
    final int NUM_THREADS = 150;
    final ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
    final Future<Throwable>[] futureFailures = new Future[NUM_THREADS];
    final Semaphore semaphore = new Semaphore(MAX_OUTBOUND_MESSAGES, true);
    try {
        // create and submit the tasks which will send out the messages
        for (int i = 0; i < NUM_THREADS; i++) {
            futureFailures[i] = executorService.submit(new MessageSender(this.clientChannel, semaphore));
        }
        int failureCount = 0;
        // wait for the tasks to complete and then collect any failures
        for (int i = 0; i < NUM_THREADS; i++) {
            final Throwable failure = futureFailures[i].get();
            if (failure == null) {
                continue;
            }
            failureCount++;
            logger.info("Thread#" + i + " failed with exception", failure);
        }
        Assert.assertEquals("Some threads failed to send message on the channel", 0, failureCount);
    } finally {
        executorService.shutdown();
    }
}
Also used : MessageInputStream(org.jboss.remoting3.MessageInputStream) Channel(org.jboss.remoting3.Channel) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) IoFuture(org.xnio.IoFuture) IOException(java.io.IOException) Semaphore(java.util.concurrent.Semaphore) Endpoint(org.jboss.remoting3.Endpoint) Test(org.junit.Test)

Example 25 with Channel

use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.

the class OutboundMessageCountTestCase method beforeTest.

@Before
public void beforeTest() throws IOException, URISyntaxException, InterruptedException {
    System.gc();
    System.runFinalization();
    Logger.getLogger("TEST").infof("Running test %s", name.getMethodName());
    final FutureResult<Channel> passer = new FutureResult<Channel>();
    serviceRegistration = endpoint.registerService("org.jboss.test", new OpenListener() {

        public void channelOpened(final Channel channel) {
            passer.setResult(channel);
        }

        public void registrationTerminated() {
        }
    }, OptionMap.EMPTY);
    IoFuture<Connection> futureConnection = AuthenticationContext.empty().with(MatchRule.ALL, AuthenticationConfiguration.empty().useName("bob").usePassword("pass").setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("SCRAM-SHA-256"))).run(new PrivilegedAction<IoFuture<Connection>>() {

        public IoFuture<Connection> run() {
            try {
                return endpoint.connect(new URI("remote://[::1]:30123"), OptionMap.EMPTY);
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    });
    connection = futureConnection.get();
    final OptionMap channelCreationOptions = OptionMap.create(RemotingOptions.MAX_OUTBOUND_MESSAGES, MAX_OUTBOUND_MESSAGES);
    IoFuture<Channel> futureChannel = connection.openChannel("org.jboss.test", channelCreationOptions);
    clientChannel = futureChannel.get();
    serverChannel = passer.getIoFuture().get();
    assertNotNull(serverChannel);
}
Also used : OpenListener(org.jboss.remoting3.OpenListener) Channel(org.jboss.remoting3.Channel) Connection(org.jboss.remoting3.Connection) IoFuture(org.xnio.IoFuture) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) FutureResult(org.xnio.FutureResult) OptionMap(org.xnio.OptionMap) Before(org.junit.Before)

Aggregations

Channel (org.jboss.remoting3.Channel)41 IOException (java.io.IOException)29 Test (org.junit.Test)14 LivestreamServiceClient (com.google.cloud.video.livestream.v1.LivestreamServiceClient)13 Connection (org.jboss.remoting3.Connection)12 MessageInputStream (org.jboss.remoting3.MessageInputStream)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 OpenListener (org.jboss.remoting3.OpenListener)10 MessageOutputStream (org.jboss.remoting3.MessageOutputStream)9 InetSocketAddress (java.net.InetSocketAddress)8 URI (java.net.URI)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 IoFuture (org.xnio.IoFuture)7 Channel (com.google.cloud.video.livestream.v1.Channel)6 URISyntaxException (java.net.URISyntaxException)6 ManagementClientChannelStrategy (org.jboss.as.protocol.mgmt.ManagementClientChannelStrategy)6 FutureResult (org.xnio.FutureResult)6 ManagementChannelHandler (org.jboss.as.protocol.mgmt.ManagementChannelHandler)5 Endpoint (org.jboss.remoting3.Endpoint)5 Event (com.google.cloud.video.livestream.v1.Event)4