private static Executor getDefaultThreadPoolExecutor() { if (defaultThreadPoolExecutor == null) { Executor executor = getAsyncTaskThreadPool(); if (executor == null) { final AtomicInteger threadCounter = new AtomicInteger(); //noinspection NullableProblems ThreadFactory tFactory = new ThreadFactory() { @Override public Thread newThread(final Runnable r) { return new Thread(r, "Goro Thread #" + threadCounter.incrementAndGet()); } }; final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(MAX_QUEUE_LENGTH); executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, queue, tFactory); } defaultThreadPoolExecutor = executor; } return defaultThreadPoolExecutor; }
@Override public Executor getExecutor(final String queueName) { synchronized (executorsMap) { if (delegateExecutor == null) { delegateExecutor = getDefaultThreadPoolExecutor(); } if (queueName == null) { return delegateExecutor; } TaskQueueExecutor exec = executorsMap.get(queueName); if (exec == null) { exec = new TaskQueueExecutor(delegateExecutor); executorsMap.put(queueName, exec); } return exec; } }
@Test public void clearShouldRemoveTasks() { final ArrayList<Runnable> tasks = new ArrayList<>(); //noinspection NullableProblems queuesImpl.setDelegateExecutor(new Executor() { @Override public void execute(final Runnable command) { tasks.add(command); } }); // normal scenario queuesImpl.getExecutor("q").execute(mock(Runnable.class)); queuesImpl.getExecutor("q").execute(mock(Runnable.class)); assertThat(tasks).hasSize(1); tasks.remove(0).run(); assertThat(tasks).hasSize(1); tasks.remove(0).run(); assertThat(tasks).isEmpty(); // clear scenario queuesImpl.getExecutor("q").execute(mock(Runnable.class)); queuesImpl.getExecutor("q").execute(mock(Runnable.class)); queuesImpl.getExecutor("q").execute(mock(Runnable.class)); queuesImpl.clear("q"); assertThat(tasks).hasSize(1); tasks.remove(0).run(); assertThat(tasks).isEmpty(); }
private static Executor getDefaultThreadPoolExecutor() { if (defaultThreadPoolExecutor == null) { Executor executor = getAsyncTaskThreadPool(); if (executor == null) { final AtomicInteger threadCounter = new AtomicInteger(); //noinspection NullableProblems ThreadFactory tFactory = new ThreadFactory() { @Override public Thread newThread(final Runnable r) { return new Thread(r, "Goro Thread #" + threadCounter.incrementAndGet()); } }; final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(MAX_QUEUE_LENGTH); executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, queue, tFactory); } defaultThreadPoolExecutor = executor; } return defaultThreadPoolExecutor; }
@Override public Executor getExecutor(final String queueName) { synchronized (executorsMap) { if (delegateExecutor == null) { delegateExecutor = getDefaultThreadPoolExecutor(); } if (queueName == null) { return delegateExecutor; } TaskQueueExecutor exec = executorsMap.get(queueName); if (exec == null) { exec = new TaskQueueExecutor(delegateExecutor); executorsMap.put(queueName, exec); } return exec; } }
@Test public void shouldReturnExecutorsThatDelegatesToTheDefinedExecutor() { Executor mainExecutor = new Executor() { @Override public void execute(final Runnable command) { command.run(); } }; mainExecutor = spy(mainExecutor); Runnable task = mock(Runnable.class); queuesImpl.setDelegateExecutor(mainExecutor); Executor queueExecutor = queuesImpl.getExecutor("1"); assertThat(queueExecutor).isNotEqualTo(mainExecutor); queueExecutor.execute(task); verify(mainExecutor).execute(any(Runnable.class)); verify(task).run(); }
GoroImpl() { this(new Queues.Impl()); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowNullThreadPool() { queuesImpl.setDelegateExecutor(null); }
@Before public void createQueuesImpl() { queuesImpl = new Queues.Impl(); }
GoroImpl() { this(new Queues.Impl()); }
@Test public void shouldReturnSameExecutorForSameQueue() { Executor executor = queuesImpl.getExecutor("1"); assertThat(executor).isNotNull().isSameAs(queuesImpl.getExecutor("1")); }
@Test public void shouldReturnMainExecutorForNullQueueName() { Executor mainExecutor = mock(Executor.class); queuesImpl.setDelegateExecutor(mainExecutor); assertThat(queuesImpl.getExecutor(null)).isSameAs(mainExecutor); }
@Test(expected = IllegalStateException.class) public void shouldNotAllowThreadsPoolChangeAfterQueueCreation() { queuesImpl.getExecutor("1"); queuesImpl.setDelegateExecutor(Executors.newCachedThreadPool()); }