/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 7153400 * @summary Tests if [corePoolSize <= maximumPoolSize] invariant is enforced * by corresponding setters */ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; public class PoolSizesInvariant { public static void main(String[] args) { succeed(poolOfSize(2, 3), p -> p.setCorePoolSize(3)); succeed(poolOfSize(2, 3), p -> p.setMaximumPoolSize(2)); fail(poolOfSize(2, 3), p -> p.setCorePoolSize(4)); fail(poolOfSize(2, 3), p -> p.setMaximumPoolSize(1)); } private static ThreadPoolExecutor poolOfSize(int corePoolSize, int maximumPoolSize) { return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(4)); } private static void succeed(ThreadPoolExecutor p, Consumer c) { try { c.accept(p); } catch (Throwable t) { throw new AssertionError("Test wasn't supposed to throw any exception", t); } finally { dispose(p); } } private static void fail(ThreadPoolExecutor p, Consumer c) { try { c.accept(p); } catch (IllegalArgumentException e) { return; } catch (Throwable t) { throw new AssertionError("Test was supposed to throw IllegalArgumentException", t); } finally { dispose(p); } throw new AssertionError("Test was supposed to throw IllegalArgumentException"); } private static void dispose(ThreadPoolExecutor p) { p.shutdownNow(); try { p.awaitTermination(1, TimeUnit.MINUTES); } catch (InterruptedException e) { throw new RuntimeException("Should not happen", e); } } }