From 78dbfe5a14cc0ac2948590856521eacdaf22d613 Mon Sep 17 00:00:00 2001 From: akhil Date: Tue, 4 Feb 2014 16:38:39 -0800 Subject: [PATCH] do not exit if jobs are pending, add multiple loop test --- .../oracle/avatar/js/eventloop/EventLoop.java | 10 +- src/test/java/MultipleEventLoopTest.java | 91 +++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/test/java/MultipleEventLoopTest.java diff --git a/src/main/java/com/oracle/avatar/js/eventloop/EventLoop.java b/src/main/java/com/oracle/avatar/js/eventloop/EventLoop.java index 84c22c0..56c6072 100644 --- a/src/main/java/com/oracle/avatar/js/eventloop/EventLoop.java +++ b/src/main/java/com/oracle/avatar/js/eventloop/EventLoop.java @@ -95,14 +95,15 @@ public final class EventLoop { this.hooks = hooks; this.refHandle = refHandle; this.unrefHandle = unrefHandle; - if (hooks.incrementAndGet() == 1) { + + if (this.hooks.incrementAndGet() == 1) { this.refHandle.send(); } } @Override public void close() { - if (hooks.decrementAndGet() == 0) { + if (this.hooks.decrementAndGet() == 0) { this.unrefHandle.send(); } } @@ -156,7 +157,10 @@ public final class EventLoop { pendingException = null; throw pex; } - } while (hooks.get() > 0 || eventQueue.peek() != null); + } while (hooks.get() > 0 || + eventQueue.peek() != null || + executor.hasActiveTasks() || + executor.hasQueuedTasks()); } /** diff --git a/src/test/java/MultipleEventLoopTest.java b/src/test/java/MultipleEventLoopTest.java new file mode 100644 index 0000000..af125e0 --- /dev/null +++ b/src/test/java/MultipleEventLoopTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2013, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +import java.io.FileReader; +import java.io.Reader; +import java.util.Properties; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import com.oracle.avatar.js.eventloop.EventLoop; +import com.oracle.avatar.js.eventloop.ThreadPool; +import com.oracle.avatar.js.log.Logging; + +public class MultipleEventLoopTest { + + @Test + public void testSubmit() throws Throwable { + final Properties properties = new Properties(); + try (Reader reader = new FileReader("project.properties")) { + properties.load(reader); + } + + final int CONCURRENCY = 256; + final Logging logging = new Logging(false); + final EventLoop[] loops = new EventLoop[CONCURRENCY]; + final boolean[] didRun = new boolean[loops.length]; + for (int i=0; i < loops.length; i++) { + didRun[i] = false; + final EventLoop loop = loops[i] = new EventLoop( + properties.getProperty("source.compatible.version"), + properties.getProperty("libuv.compatible.version"), + logging, + System.getProperty("user.dir"), + i, + ThreadPool.getInstance(), + true); + final int fi = i; + try (EventLoop.Handle handle = loop.acquire()) { + loop.submit(new Runnable() { + @Override + public void run() { + System.out.println("++ " + fi); + try { + Thread.sleep((long) (Math.random() * 1000)); + didRun[fi] = true; + } catch (InterruptedException ex) { + } finally { + System.out.println("-- " + fi); + } + } + }); + } + } + + for (int i=0; i < loops.length; i++) { + loops[i].run(); + } + + for (int i=0; i < loops.length; i++) { + Assert.assertTrue(didRun[i]); + } + } + + public static void main(String[] args) throws Throwable { + MultipleEventLoopTest test = new MultipleEventLoopTest(); + test.testSubmit(); + } +}