Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/ConcurrentReadingTest.java
41152 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 654724126* @summary Test verifies that concurrent usage of jpeg reader instance27* by number of threads does not cause crash in jpeg library.28* @run main ConcurrentReadingTest29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.RadialGradientPaint;34import java.awt.geom.Point2D;35import java.awt.image.BufferedImage;36import java.io.File;37import java.io.IOException;38import javax.imageio.ImageIO;39import javax.imageio.ImageReader;40import javax.imageio.ImageReadParam;41import javax.imageio.stream.ImageInputStream;4243public class ConcurrentReadingTest extends Thread {4445static ImageReader r = null;46static File file = new File("IMGP1001.JPG");4748private static final int MAX_THREADS = 50;4950static int completeCount = 0;;51static Object lock = new Object();5253public static void main(String[] args) throws Exception {54createTestFile();5556ImageInputStream iis = ImageIO.createImageInputStream(file);57r = ImageIO.getImageReaders(iis).next();58iis.close();5960for (int i = 0; i < MAX_THREADS; i++) {61(new ConcurrentReadingTest()).start();62}6364// wait for started threads65boolean needWait = true;66while (needWait) {67Thread.sleep(100);68synchronized(lock) {69needWait = completeCount < MAX_THREADS;70}71}72System.out.println("Test PASSED.");73}7475public void run() {76try {77ImageInputStream iis = ImageIO.createImageInputStream(file);78r.setInput(iis);79ImageReadParam p = r.getDefaultReadParam();80Thread.sleep(70);81BufferedImage res = r.read(0, p);82Thread.sleep(70);83r.reset();84} catch (IllegalStateException e) {85System.out.println(e);86} catch (IOException e) {87System.out.println(e);88} catch (Throwable e) {89// Unexpected exception. Test failed.90throw new RuntimeException("Test failed.", e);91} finally {92synchronized(lock) {93completeCount ++;94}95}96}9798private static void createTestFile() {99int w = 1280;100int h = 1024;101102BufferedImage img = new103BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);104Graphics2D g = img.createGraphics();105Color[] colors = { Color.red, Color.green, Color.blue };106float[] dist = {0.0f, 0.5f, 1.0f };107Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);108109RadialGradientPaint p =110new RadialGradientPaint(center, 0.5f * w, dist, colors);111g.setPaint(p);112g.fillRect(0, 0, w, h);113g.dispose();114115try {116System.out.println("Create test image " + file.getAbsolutePath());117boolean b = ImageIO.write(img, "JPEG", file);118if (!b) {119throw new RuntimeException("Failed to create test image.");120}121} catch (IOException e) {122throw new RuntimeException("Test failed", e);123}124}125}126127128