use of java.net.ServerSocket in project flink by apache.
the class TaskManagerStartupTest method testStartupWhenTaskmanagerActorPortIsUsed.
/**
* Tests that the TaskManager fails synchronously when the actor system port
* is in use.
*
* @throws Throwable
*/
@Test(expected = BindException.class)
public void testStartupWhenTaskmanagerActorPortIsUsed() throws BindException {
ServerSocket blocker = null;
try {
final String localHostName = "localhost";
final InetAddress localBindAddress = InetAddress.getByName(NetUtils.getWildcardIPAddress());
// block some port
blocker = new ServerSocket(0, 50, localBindAddress);
final int port = blocker.getLocalPort();
TaskManager.runTaskManager(localHostName, ResourceID.generate(), port, new Configuration(), TaskManager.class);
fail("This should fail with an IOException");
} catch (IOException e) {
// expected. validate the error message
List<Throwable> causes = StartupUtils.getExceptionCauses(e, new ArrayList<Throwable>());
for (Throwable cause : causes) {
if (cause instanceof BindException) {
throw (BindException) cause;
}
}
fail("This should fail with an exception caused by BindException");
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (blocker != null) {
try {
blocker.close();
} catch (IOException e) {
// no need to log here
}
}
}
}
use of java.net.ServerSocket in project flink by apache.
the class TaskManagerStartupTest method testStartupWhenNetworkStackFailsToInitialize.
/**
* Tests that the task manager start-up fails if the network stack cannot be initialized.
* @throws Exception
*/
@Test(expected = IOException.class)
public void testStartupWhenNetworkStackFailsToInitialize() throws Exception {
ServerSocket blocker = null;
try {
blocker = new ServerSocket(0, 50, InetAddress.getByName("localhost"));
final Configuration cfg = new Configuration();
cfg.setString(ConfigConstants.TASK_MANAGER_HOSTNAME_KEY, "localhost");
cfg.setInteger(ConfigConstants.TASK_MANAGER_DATA_PORT_KEY, blocker.getLocalPort());
cfg.setInteger(ConfigConstants.TASK_MANAGER_MEMORY_SIZE_KEY, 1);
TaskManager.startTaskManagerComponentsAndActor(cfg, ResourceID.generate(), null, "localhost", Option.<String>empty(), Option.<LeaderRetrievalService>empty(), false, TaskManager.class);
} finally {
if (blocker != null) {
try {
blocker.close();
} catch (IOException e) {
// ignore, best effort
}
}
}
}
use of java.net.ServerSocket in project flink by apache.
the class SocketClientSinkTest method testSocketSink.
@Test
public void testSocketSink() throws Exception {
final ServerSocket server = new ServerSocket(0);
final int port = server.getLocalPort();
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
Thread sinkRunner = new Thread("Test sink runner") {
@Override
public void run() {
try {
SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0);
simpleSink.open(new Configuration());
simpleSink.invoke(TEST_MESSAGE + '\n');
simpleSink.close();
} catch (Throwable t) {
error.set(t);
}
}
};
sinkRunner.start();
Socket sk = server.accept();
BufferedReader rdr = new BufferedReader(new InputStreamReader(sk.getInputStream()));
String value = rdr.readLine();
sinkRunner.join();
server.close();
if (error.get() != null) {
Throwable t = error.get();
t.printStackTrace();
fail("Error in spawned thread: " + t.getMessage());
}
assertEquals(TEST_MESSAGE, value);
}
use of java.net.ServerSocket in project flink by apache.
the class SocketClientSinkTest method testSinkAutoFlush.
@Test
public void testSinkAutoFlush() throws Exception {
final ServerSocket server = new ServerSocket(0);
final int port = server.getLocalPort();
final SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0, true);
simpleSink.open(new Configuration());
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
Thread sinkRunner = new Thread("Test sink runner") {
@Override
public void run() {
try {
// need two messages here: send a fin to cancel the client state:FIN_WAIT_2 while the server is CLOSE_WAIT
simpleSink.invoke(TEST_MESSAGE + '\n');
} catch (Throwable t) {
error.set(t);
}
}
};
sinkRunner.start();
Socket sk = server.accept();
BufferedReader rdr = new BufferedReader(new InputStreamReader(sk.getInputStream()));
String value = rdr.readLine();
sinkRunner.join();
simpleSink.close();
server.close();
if (error.get() != null) {
Throwable t = error.get();
t.printStackTrace();
fail("Error in spawned thread: " + t.getMessage());
}
assertEquals(TEST_MESSAGE, value);
}
use of java.net.ServerSocket in project flink by apache.
the class SocketTextStreamFunctionTest method testSocketSourceOutputInfiniteRetries.
@Test
public void testSocketSourceOutputInfiniteRetries() throws Exception {
ServerSocket server = new ServerSocket(0);
Socket channel = null;
try {
SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", -1, 100);
SocketSourceThread runner = new SocketSourceThread(source, "test1", "check");
runner.start();
// first connection: nothing
channel = server.accept();
channel.close();
// second connection: first string
channel = server.accept();
OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream());
writer.write("test1\n");
writer.close();
channel.close();
// third connection: nothing
channel = server.accept();
channel.close();
// forth connection: second string
channel = server.accept();
writer = new OutputStreamWriter(channel.getOutputStream());
writer.write("check\n");
writer.flush();
runner.waitForNumElements(2);
runner.cancel();
runner.waitUntilDone();
} finally {
if (channel != null) {
IOUtils.closeQuietly(channel);
}
IOUtils.closeQuietly(server);
}
}
Aggregations