diff options
author | Sven Gothel <[email protected]> | 2014-07-31 02:42:39 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-07-31 02:42:39 +0200 |
commit | ba1ffe66697c3175b423cb7ab9b686d73959708d (patch) | |
tree | 13d4daed94906e96f0001cc9df5ad4d6c8cd3518 /src/test | |
parent | 2e1484f4c25605c6dc0307f12f3e4e434c429a37 (diff) |
Bug 1039 - Specify behavior of GLEventListener Exceptions occurring while GLAutoDrawable processing [part-1]
Implements Specification as described on 'Bug 1039 Comment 1'
<https://jogamp.org/bugzilla/show_bug.cgi?id=1039#c1>
TODO:
- Offthread exception handler
++++
GLDrawableHelper is used in all GLAutoDrawable implementations
and for most operations.
GLAutoDrawable/GLDrawableHelper invoke(..) method:
- invoke(..) forwards a caught exception
- if blocking, it forwards an exception
happening within the passed GLRunnable(s).
Here the exception is caught, printed
and then thrown by invoke itself.
- if non-blocking, an exception
happening within the passed GLRunnable(s)
will be thrown in the thread issuing it's execution,
i.e. display() call.
Here the exception is not caught and simply thrown
by the GLRunnable.
GLAutoDrawable.destroy() -> GLDrawableHelper.disposeGL(..) method:
- disposeAllGLEventListener() being invoked by disposeGL(..),
catches exception thrown by GLEventListener.dispose(..)
and prints them to stderr.
The first caught exception is re-thrown at the end as an GLException.
- disposeGL() catches re-thrown GLException by disposeAllGLEventListener()
for GLEventListener.dispose(..)
and re-throws it when operation is complete.
- disposeGL() catches an exception thrown at context destruction or release
and re-throws it when operation is complete.
An early exception at context.makeCurrent() is _not_ caught,
since it is the first operation which simply shall unwind the stack.
GLAutoDrawable.display() -> GLDrawableHelper.invokeGLImpl(..) method:
- invokeGLImpl(..) for display() follows disposeGL() mechanism, i.e.
it catches exception thrown at
GLEventListener's init(..), reshape(..) and display(..) methods
and re-throws it when operation is complete.
It also catches an exception thrown at context release
and re-throws it when operation is complete.
An early exception at context.makeCurrent() is _not_ caught,
since it is the first operation which simply shall unwind the stack.
++++
None of the above thrown exception shall be caught and suppressed
on the caller side.
If an operation must be completed while an exception is caught,
it shall be cached and re-thrown after the operations.
In case multiple exception at multiple places are caught within
an operation, they all shall be cached and the first one
shall be re-thrown.
In case of multiple exception from the same place,
i.e. a loop through all GLEventListener,
the first shall be cached and re-thrown after operation is completed.
It has to be determined, whether we like to dump the exceptions,
especially the ones who get suppressed in case of multiple exceptions.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLException01NEWT.java | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLException01NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLException01NEWT.java new file mode 100644 index 000000000..a2dca2dda --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLException01NEWT.java @@ -0,0 +1,235 @@ +/** + * Copyright 2011 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.test.junit.jogl.acore; + +import java.util.concurrent.atomic.AtomicInteger; + +import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.opengl.test.junit.util.MiscUtils; +import com.jogamp.opengl.test.junit.util.UITestCase; +import com.jogamp.opengl.util.Animator; +import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; + +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLProfile; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestGLException01NEWT extends UITestCase { + static GLProfile glp; + static int width, height; + + @BeforeClass + public static void initClass() { + glp = GLProfile.getGL2ES2(); + Assert.assertNotNull(glp); + width = 512; + height = 512; + } + + public static void dumpThrowable(final Throwable t) { + System.err.println("User caught exception "+t.getClass().getSimpleName()+": "+t.getMessage()+" on thread "+Thread.currentThread().getName()); + t.printStackTrace(); + } + + protected void runTestGL(final GLCapabilities caps, final boolean onThread, + final boolean throwInInit, final boolean throwInDisplay, + final boolean throwInReshape, final boolean throwInDispose) throws InterruptedException { + final GLWindow glWindow = GLWindow.create(caps); + Assert.assertNotNull(glWindow); + glWindow.setTitle("NEWT Exception Test"); + final GearsES2 demo1 = new GearsES2(); + demo1.setVerbose(false); + glWindow.addGLEventListener(demo1); + final AtomicInteger initCount = new AtomicInteger(); + final AtomicInteger disposeCount = new AtomicInteger(); + final AtomicInteger displayCount = new AtomicInteger(); + final AtomicInteger reshapeCount = new AtomicInteger(); + final AtomicInteger exceptionSent = new AtomicInteger(); + + glWindow.addGLEventListener(new GLEventListener() { + @Override + public void init(final GLAutoDrawable drawable) { + if( throwInInit && 0 == exceptionSent.get() ) { + exceptionSent.incrementAndGet(); + throw new RuntimeException("Injected GLEventListener exception in init"); + } + } + @Override + public void dispose(final GLAutoDrawable drawable) { + if( throwInDispose && 0 == exceptionSent.get() ) { + exceptionSent.incrementAndGet(); + throw new RuntimeException("Injected GLEventListener exception in dispose"); + } + } + @Override + public void display(final GLAutoDrawable drawable) { + if( throwInDisplay && 0 == exceptionSent.get() ) { + exceptionSent.incrementAndGet(); + throw new RuntimeException("Injected GLEventListener exception in display"); + } + } + @Override + public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { + if( throwInReshape && 0 == exceptionSent.get() ) { + exceptionSent.incrementAndGet(); + throw new RuntimeException("Injected GLEventListener exception in reshape"); + } + } + }); + glWindow.addGLEventListener(new GLEventListener() { + @Override + public void init(final GLAutoDrawable drawable) { + initCount.incrementAndGet(); + } + @Override + public void dispose(final GLAutoDrawable drawable) { + disposeCount.incrementAndGet(); + } + @Override + public void display(final GLAutoDrawable drawable) { + displayCount.incrementAndGet(); + } + @Override + public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { + reshapeCount.incrementAndGet(); + } + }); + + RuntimeException execptionAtInitReshapeDisplay = null; + RuntimeException execptionAtDispose = null; + + final Animator animator = !onThread ? new Animator(glWindow) : null; + + glWindow.setSize(width, height); + + if( !onThread ) { + animator.setUpdateFPSFrames(1, null); + animator.start(); + } + try { + glWindow.setVisible(true); + } catch (final RuntimeException re) { + execptionAtInitReshapeDisplay = re; + dumpThrowable(re); + } + + final long t0 = System.currentTimeMillis(); + long t1 = t0; + while(0 == exceptionSent.get() && ( onThread || animator.isAnimating() ) && t1-t0<duration ) { + if( onThread ) { + try { + glWindow.display(); + } catch (final RuntimeException re) { + execptionAtInitReshapeDisplay = re; + dumpThrowable(re); + } + } + Thread.sleep(100); + t1 = System.currentTimeMillis(); + } + + if( !onThread ) { + animator.stop(); + } + try { + glWindow.destroy(); + } catch (final RuntimeException re) { + execptionAtDispose = re; + dumpThrowable(re); + } + + if( throwInInit || throwInReshape || throwInDisplay || throwInDispose ) { + Assert.assertEquals("Not one exception sent", 1, exceptionSent.get()); + if( throwInInit ) { + Assert.assertNotNull("No exception forwarded from init", execptionAtInitReshapeDisplay); + } + if( throwInReshape ) { + Assert.assertNotNull("No exception forwarded from reshape", execptionAtInitReshapeDisplay); + } + if( throwInDisplay ) { + Assert.assertNotNull("No exception forwarded from display", execptionAtInitReshapeDisplay); + } + if( throwInDispose ) { + Assert.assertNotNull("No exception forwarded from dispose", execptionAtDispose); + } + } + } + + @Test + public void test01OnThreadAtInit() throws InterruptedException { + final GLCapabilities caps = new GLCapabilities(glp); + caps.setBackgroundOpaque(true); // default + runTestGL(caps, true /* onThread */, true /* init */, false /* display */, false /* reshape */, false /* dispose */); + } + @Test + public void test02OnThreadAtReshape() throws InterruptedException { + final GLCapabilities caps = new GLCapabilities(glp); + caps.setBackgroundOpaque(true); // default + runTestGL(caps, true /* onThread */, false /* init */, false /* display */, true /* reshape */, false /* dispose */); + } + @Test + public void test03OnThreadAtDisplay() throws InterruptedException { + final GLCapabilities caps = new GLCapabilities(glp); + caps.setBackgroundOpaque(true); // default + runTestGL(caps, true /* onThread */, false /* init */, true /* display */, false /* reshape */, false /* dispose */); + } + @Test + public void test04OnThreadAtDispose() throws InterruptedException { + final GLCapabilities caps = new GLCapabilities(glp); + caps.setBackgroundOpaque(true); // default + runTestGL(caps, true /* onThread */, false /* init */, false /* display */, false /* reshape */, true /* dispose */); + } + + static long duration = 500; // ms + + public static void main(final String args[]) { + boolean waitForKey = false; + + for(int i=0; i<args.length; i++) { + if(args[i].equals("-time")) { + i++; + duration = MiscUtils.atol(args[i], duration); + } else if(args[i].equals("-wait")) { + waitForKey = true; + } + } + if( waitForKey ) { + UITestCase.waitForKey("main"); + } + org.junit.runner.JUnitCore.main(TestGLException01NEWT.class.getName()); + } +} |