use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testWriteCancel.
public void testWriteCancel(final byte[] data) throws IOException, InterruptedException {
final AtomicBoolean wasOk = new AtomicBoolean();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
final CountDownLatch latch = new CountDownLatch(1);
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) {
new Thread(new Runnable() {
public void run() {
final byte[] received = new byte[TEST_FILE_LENGTH];
int c = 0;
try {
System.out.println("Message received");
int r;
do {
r = message.read(received, c, TEST_FILE_LENGTH - c);
if (r == -1) {
break;
}
c += r;
} while (c < TEST_FILE_LENGTH);
if (r != -1) {
r = message.read();
}
message.close();
} catch (MessageCancelledException e) {
System.out.println("Value of c at message cancelled is " + c);
int i = 0;
while (i < c) {
if (data[i] != received[i]) {
break;
}
i++;
}
wasOk.set(i == c);
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
}).start();
}
});
MessageOutputStream messageOutputStream = sendChannel.writeMessage();
messageOutputStream.write(data);
messageOutputStream.cancel();
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(wasOk.get());
}
use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testRemoteChannelClose.
@Test
public void testRemoteChannelClose() throws Exception {
final CountDownLatch closedLatch = new CountDownLatch(1);
sendChannel.addCloseHandler(new CloseHandler<Channel>() {
public void handleClose(final Channel closed, final IOException exception) {
closedLatch.countDown();
}
});
sendChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
channel.closeAsync();
}
public void handleEnd(final Channel channel) {
channel.closeAsync();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
IoUtils.safeClose(message);
}
});
recvChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
channel.closeAsync();
}
public void handleEnd(final Channel channel) {
channel.closeAsync();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
IoUtils.safeClose(message);
}
});
sendChannel.writeShutdown();
IoUtils.safeClose(recvChannel);
System.out.println("Waiting for closed");
closedLatch.await();
System.out.println("Closed");
}
use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testSimpleWriteMethod.
@Test
public void testSimpleWriteMethod() throws Exception {
Byte[] bytes = new Byte[] { 1, 2, 3 };
MessageOutputStream out = sendChannel.writeMessage();
for (int i = 0; i < bytes.length; i++) {
out.write(bytes[i]);
}
out.close();
final CountDownLatch latch = new CountDownLatch(1);
final ArrayList<Byte> result = new ArrayList<Byte>();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
recvChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
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 {
int i = message.read();
while (i != -1) {
result.add((byte) i);
i = message.read();
}
message.close();
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
});
latch.await();
assertNull(exRef.get());
Byte[] resultBytes = result.toArray(new Byte[result.size()]);
assertArrayEquals(bytes, resultBytes);
}
use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.
the class ConnectionTestCase method rejectUnknownService.
@Test
public void rejectUnknownService() throws Exception {
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);
}
}
});
final IoFuture<Channel> channelFuture = connection.openChannel("unknown", OptionMap.EMPTY);
try {
channelFuture.get();
fail();
} catch (CancellationException e) {
throw e;
} catch (IOException e) {
// ok
}
}
use of com.google.cloud.video.livestream.v1.Channel in project jboss-remoting by jboss-remoting.
the class RemoteServiceWithPredicateTest method tryToConnectToService.
/**
* Method which registers a service with the provided validation predicate, then tests to see if the service
* accepts or rejects service Channel creation attempts which are expected by the predicate provided.
*
* @param validationPredicate
* @throws IOException
* @throws URISyntaxException
* @throws InterruptedException
*/
public void tryToConnectToService(Predicate<Connection> validationPredicate, boolean shouldSucceed) throws IOException {
Connection connection;
Registration serviceRegistration;
Channel sendChannel = null, recvChannel = null;
final FutureResult<Channel> passer = new FutureResult<Channel>();
// register the service with the endpoint using the provided validation predicate
serviceRegistration = endpoint.registerService("org.jboss.test", new OpenListener() {
public void channelOpened(final Channel channel) {
passer.setResult(channel);
}
public void registrationTerminated() {
}
}, OptionMap.EMPTY, validationPredicate);
// create a connection to the endpoint's connector
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://localhost:30123"), OptionMap.EMPTY);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
});
connection = futureConnection.get();
assertNull("No SSLSession", connection.getSslSession());
// use the connection to open a channel to the service
IoFuture.Status status = IoFuture.Status.WAITING;
IoFuture<Channel> futureChannel = connection.openChannel("org.jboss.test", OptionMap.EMPTY);
try {
status = futureChannel.awaitInterruptibly(2L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// verify the resukt of the service connection attempt
Logger.getLogger("TEST").infof("service returned status %s", status);
if (status == IoFuture.Status.DONE) {
Logger.getLogger("TEST").infof("Channel creation succeeded");
sendChannel = futureChannel.get();
recvChannel = passer.getIoFuture().get();
assertNotNull(recvChannel);
assertNull("No SSLSession", recvChannel.getConnection().getSslSession());
if (!shouldSucceed)
fail("Channel creation succeeded when it should have failed!");
} else if (status == IoFuture.Status.FAILED) {
Exception exception = futureChannel.getException();
Logger.getLogger("TEST").infof("Channel creation failed with exception %s", exception);
if (shouldSucceed)
fail("Channel creation failed when it should have succeeded!");
}
// service has been tested, we can shut down
safeClose(sendChannel);
safeClose(recvChannel);
safeClose(connection);
serviceRegistration.close();
}
Aggregations