use of java.util.concurrent.ArrayBlockingQueue in project android_frameworks_base by ParanoidAndroid.
the class LoaderTestCase method getLoaderResultSynchronously.
/**
* Runs a Loader synchronously and returns the result of the load. The loader will
* be started, stopped, and destroyed by this method so it cannot be reused.
*
* @param loader The loader to run synchronously
* @return The result from the loader
*/
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
// The test thread blocks on this queue until the loader puts it's result in
final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
// This callback runs on the "main" thread and unblocks the test thread
// when it puts the result into the blocking queue
final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {
@Override
public void onLoadComplete(Loader<T> completedLoader, T data) {
// Shut the loader down
completedLoader.unregisterListener(this);
completedLoader.stopLoading();
completedLoader.reset();
// Store the result, unblocking the test thread
queue.add(data);
}
};
// This handler runs on the "main" thread of the process since AsyncTask
// is documented as needing to run on the main thread and many Loaders use
// AsyncTask
final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
loader.registerListener(0, listener);
loader.startLoading();
}
};
// Ask the main thread to start the loading process
mainThreadHandler.sendEmptyMessage(0);
// Block on the queue waiting for the result of the load to be inserted
T result;
while (true) {
try {
result = queue.take();
break;
} catch (InterruptedException e) {
throw new RuntimeException("waiting thread interrupted", e);
}
}
return result;
}
use of java.util.concurrent.ArrayBlockingQueue in project neo4j by neo4j.
the class RecordCheckWorkerTest method shouldDoProcessingInitializationInOrder.
@Test
public void shouldDoProcessingInitializationInOrder() throws Throwable {
// GIVEN
final Race race = new Race();
final AtomicInteger coordination = new AtomicInteger(-1);
final AtomicInteger expected = new AtomicInteger();
final int threads = 30;
@SuppressWarnings("unchecked") final RecordCheckWorker<Integer>[] workers = new RecordCheckWorker[threads];
final RecordProcessor<Integer> processor = new RecordProcessor.Adapter<Integer>() {
@Override
public void process(Integer record) {
// We're testing init() here, not really process()
}
@Override
public void init(int id) {
assertEquals(id, expected.getAndAdd(1));
}
};
for (int id = 0; id < threads; id++) {
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
race.addContestant(workers[id] = new RecordCheckWorker<>(id, coordination, queue, processor));
}
race.addContestant(new Runnable() {
@Override
public void run() {
try {
long end = currentTimeMillis() + SECONDS.toMillis(100);
while (currentTimeMillis() < end && expected.get() < threads) {
parkNanos(MILLISECONDS.toNanos(10));
}
assertEquals(threads, expected.get());
} finally {
for (RecordCheckWorker<Integer> worker : workers) {
worker.done();
}
}
}
});
// WHEN
race.go();
}
use of java.util.concurrent.ArrayBlockingQueue in project weave by continuuity.
the class DiscoveryServiceTestBase method testCancelChangeListener.
@Test
public void testCancelChangeListener() throws InterruptedException {
Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
DiscoveryService discoveryService = entry.getKey();
DiscoveryServiceClient discoveryServiceClient = entry.getValue();
String serviceName = "cancel_listener";
ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover(serviceName);
// An executor that delay execute a Runnable. It's for testing race because listener cancel and discovery changes.
Executor delayExecutor = new Executor() {
@Override
public void execute(final Runnable command) {
Thread t = new Thread() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
command.run();
} catch (InterruptedException e) {
throw Throwables.propagate(e);
}
}
};
t.start();
}
};
final BlockingQueue<List<Discoverable>> events = new ArrayBlockingQueue<List<Discoverable>>(10);
Cancellable cancelWatch = serviceDiscovered.watchChanges(new ServiceDiscovered.ChangeListener() {
@Override
public void onChange(ServiceDiscovered serviceDiscovered) {
events.add(ImmutableList.copyOf(serviceDiscovered));
}
}, delayExecutor);
// Wait for the init event call
Assert.assertNotNull(events.poll(3, TimeUnit.SECONDS));
// Register a new service endpoint, wait a short while and then cancel the listener
register(discoveryService, serviceName, "localhost", 1);
TimeUnit.SECONDS.sleep(1);
cancelWatch.cancel();
// The change listener shouldn't get any event, since the invocation is delayed by the executor.
Assert.assertNull(events.poll(3, TimeUnit.SECONDS));
}
use of java.util.concurrent.ArrayBlockingQueue in project weave by continuuity.
the class DiscoveryServiceTestBase method testChangeListener.
@Test
public void testChangeListener() throws InterruptedException {
Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
DiscoveryService discoveryService = entry.getKey();
DiscoveryServiceClient discoveryServiceClient = entry.getValue();
// Start discovery
String serviceName = "listener_test";
ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover(serviceName);
// Watch for changes.
final BlockingQueue<List<Discoverable>> events = new ArrayBlockingQueue<List<Discoverable>>(10);
serviceDiscovered.watchChanges(new ServiceDiscovered.ChangeListener() {
@Override
public void onChange(ServiceDiscovered serviceDiscovered) {
events.add(ImmutableList.copyOf(serviceDiscovered));
}
}, Threads.SAME_THREAD_EXECUTOR);
// An empty list will be received first, as no endpoint has been registered.
List<Discoverable> discoverables = events.poll(5, TimeUnit.SECONDS);
Assert.assertNotNull(discoverables);
Assert.assertTrue(discoverables.isEmpty());
// Register a service
Cancellable cancellable = register(discoveryService, serviceName, "localhost", 10000);
discoverables = events.poll(5, TimeUnit.SECONDS);
Assert.assertNotNull(discoverables);
Assert.assertEquals(1, discoverables.size());
// Register another service endpoint
Cancellable cancellable2 = register(discoveryService, serviceName, "localhost", 10001);
discoverables = events.poll(5, TimeUnit.SECONDS);
Assert.assertNotNull(discoverables);
Assert.assertEquals(2, discoverables.size());
// Cancel both of them
cancellable.cancel();
cancellable2.cancel();
// There could be more than one event triggered, but the last event should be an empty list.
discoverables = events.poll(5, TimeUnit.SECONDS);
Assert.assertNotNull(discoverables);
if (!discoverables.isEmpty()) {
discoverables = events.poll(5, TimeUnit.SECONDS);
}
Assert.assertTrue(discoverables.isEmpty());
}
use of java.util.concurrent.ArrayBlockingQueue in project OpenAM by OpenRock.
the class RequestListenerFactory method getRadiusRequestListener.
/**
* Factory method to obtain a new RadiusRequestListener.
*
* @param serviceConfig - the configuration of the RADIUS service.
* @return a <code>RadiusRquestListener</code>
* @throws RadiusLifecycleException - if a RquestListener can not be created.
*/
public RadiusRequestListener getRadiusRequestListener(RadiusServiceConfig serviceConfig) throws RadiusLifecycleException {
final ThreadPoolConfig poolConfig = serviceConfig.getThreadPoolConfig();
final int coreSize = poolConfig.getCoreThreads();
final int maxSize = poolConfig.getMaxThreads();
final int idleTimeout = poolConfig.getKeepAliveSeconds();
final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(poolConfig.getQueueSize());
final ExecutorService executorService = executorServiceFactory.createThreadPool(coreSize, maxSize, idleTimeout, TimeUnit.SECONDS, queue);
return new RadiusRequestListener(serviceConfig, executorService, eventBus, accessRequestHandlerFactory);
}
Aggregations