use of java.net.BindException in project guice by google.
the class CheckedProviderTest method testProviderMethodWithManyExceptions.
public void testProviderMethodWithManyExceptions() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(ThrowingProviderBinder.forModule(this));
}
@SuppressWarnings("unused")
@CheckedProvides(RemoteProvider.class)
String foo() throws InterruptedException, RuntimeException, RemoteException, AccessException, TooManyListenersException, BindException, SubBindException {
return null;
}
});
fail();
} catch (CreationException ce) {
// The only two that should fail are Interrupted & TooManyListeners.. the rest are OK.
List<Message> errors = ImmutableList.copyOf(ce.getErrorMessages());
assertEquals(InterruptedException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", errors.get(0).getMessage());
assertEquals(TooManyListenersException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", errors.get(1).getMessage());
assertEquals(2, errors.size());
}
}
use of java.net.BindException in project guice by google.
the class CheckedProviderMethodsModuleTest method testWithThrownException.
public void testWithThrownException() {
TestModule testModule = new TestModule();
Injector injector = Guice.createInjector(testModule);
RpcProvider<Float> provider = injector.getInstance(Key.get(rpcProviderOfFloat));
try {
provider.get();
fail();
} catch (RemoteException e) {
fail();
} catch (BindException e) {
// good
}
}
use of java.net.BindException in project guice by google.
the class CheckedProviderTest method tExceptionsThrown.
private void tExceptionsThrown(Injector injector) throws Exception {
RemoteProvider<Foo> remoteProvider = injector.getInstance(Key.get(remoteProviderOfFoo));
mockRemoteProvider.throwOnNextGet(new BindException("kaboom!"));
MockFoo.nextToThrow = new BindException("kaboom!");
try {
remoteProvider.get();
fail();
} catch (BindException expected) {
assertEquals("kaboom!", expected.getMessage());
}
}
use of java.net.BindException in project voldemort by voldemort.
the class SocketServer method run.
@Override
public void run() {
logger.info("Starting voldemort socket server (" + serverName + ") on port " + port);
try {
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(port));
serverSocket.setReceiveBufferSize(this.socketBufferSize);
startedStatusQueue.put(SUCCESS);
while (!isInterrupted() && !serverSocket.isClosed()) {
final Socket socket = serverSocket.accept();
configureSocket(socket);
long sessionId = this.sessionIdSequence.getAndIncrement();
this.threadPool.execute(new SocketServerSession(activeSessions, socket, handlerFactory, sessionId));
}
} catch (BindException e) {
logger.error("Could not bind to port " + port + ".");
startedStatusQueue.offer(e);
throw new VoldemortException(e);
} catch (SocketException e) {
startedStatusQueue.offer(e);
// If we have been manually shutdown, ignore
if (!isInterrupted())
logger.error("Error in server: ", e);
} catch (IOException e) {
startedStatusQueue.offer(e);
throw new VoldemortException(e);
} catch (Throwable t) {
logger.error(t);
startedStatusQueue.offer(t);
if (t instanceof Error)
throw (Error) t;
else if (t instanceof RuntimeException)
throw (RuntimeException) t;
throw new VoldemortException(t);
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
logger.warn("Error while shutting down server.", e);
}
}
}
}
use of java.net.BindException in project voldemort by voldemort.
the class ServerTestUtils method startVoldemortServer.
/**
* Starts a Voldemort server for testing purposes.
*
* Unless the ports passed in via cluster are guaranteed to be available,
* this method is susceptible to BindExceptions in VoldemortServer.start().
* (And, there is no good way of guaranteeing that ports will be available,
* so...)
*
* The method {@link ServerTestUtils#startVoldemortCluster} should be used
* in preference to this method.}
*
* @param socketStoreFactory
* @param config
* @param cluster
* @param testConnection
* @return
*/
public static VoldemortServer startVoldemortServer(SocketStoreFactory socketStoreFactory, VoldemortConfig config, Cluster cluster, boolean testConnection) throws BindException {
// TODO: Some tests that use this method fail intermittently with the
// following output:
//
// A successor version version() to this version() exists for key
// cluster.xml
// voldemort.versioning.ObsoleteVersionException: A successor version
// version() to this version() exists for key cluster.xml"
//
// Need to trace through the constructor VoldemortServer(VoldemortConfig
// config, Cluster cluster) to understand how this error is possible,
// and why it only happens intermittently.
final int MAX_NUMBER_OF_ATTEMPTS = 120;
// 5 minutes
final long MAX_RETRY_TIME_IN_MS = 5 * Time.MS_PER_MINUTE;
VoldemortException lastVE = null;
long startTime = System.currentTimeMillis(), currentTime = System.currentTimeMillis();
for (int i = 1; (i <= MAX_NUMBER_OF_ATTEMPTS) && (currentTime - startTime < MAX_RETRY_TIME_IN_MS); i++, currentTime = System.currentTimeMillis()) {
VoldemortServer server = null;
boolean success = false;
try {
if (cluster != null) {
server = new VoldemortServer(config, cluster);
} else {
server = new VoldemortServer(config);
}
server.start();
if (testConnection)
ServerTestUtils.waitForServerStart(socketStoreFactory, server.getIdentityNode());
// wait till server starts or throw exception
success = true;
return server;
} catch (VoldemortException ve) {
if (ve.getCause() instanceof BindException) {
ve.printStackTrace();
trySleep(Math.min(100 * i, 1000));
lastVE = ve;
} else {
throw ve;
}
} finally {
if (!success && server != null) {
// This is in case new VoldemortServer() worked but ServerTestUtils.waitForServerStart() failed.
try {
server.stop();
} catch (Exception e) {
logger.error("Got an exception while trying to close a VoldemortServer", e);
}
}
}
}
throw new BindException(lastVE.getMessage());
}
Aggregations