diff options
Diffstat (limited to 'jurt/test/com/sun/star/lib/uno/environments/remote')
9 files changed, 1148 insertions, 0 deletions
diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/JavaThreadPoolFactory_Test.java b/jurt/test/com/sun/star/lib/uno/environments/remote/JavaThreadPoolFactory_Test.java new file mode 100644 index 000000000000..8dc1a767b5fa --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/JavaThreadPoolFactory_Test.java @@ -0,0 +1,56 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org 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 Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package com.sun.star.lib.uno.environments.remote; + +import complexlib.ComplexTestCase; + +public final class JavaThreadPoolFactory_Test extends ComplexTestCase { + public String[] getTestMethodNames() { + return new String[] { "test" }; + } + + public void test() throws InterruptedException { + ThreadId i1 = JavaThreadPoolFactory.getThreadId(); + assure(i1.equals(JavaThreadPoolFactory.getThreadId())); + final ThreadId[] i2 = new ThreadId[1]; + new Thread() { + public void run() { + synchronized (i2) { + i2[0] = JavaThreadPoolFactory.getThreadId(); + i2.notify(); + } + } + }.start(); + synchronized (i2) { + while (i2[0] == null) { + i2.wait(); + } + } + assure(!i1.equals(i2[0])); + } +} diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/JobQueue_Test.java b/jurt/test/com/sun/star/lib/uno/environments/remote/JobQueue_Test.java new file mode 100644 index 000000000000..5e83098b8ecd --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/JobQueue_Test.java @@ -0,0 +1,274 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org 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 Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package com.sun.star.lib.uno.environments.remote; + +import com.sun.star.lib.uno.typedesc.MethodDescription; +import com.sun.star.lib.uno.typedesc.TypeDescription; +import complexlib.ComplexTestCase; + +public final class JobQueue_Test extends ComplexTestCase { + public String getTestObjectName() { + return getClass().getName(); + } + + public String[] getTestMethodNames() { + return new String[] { "testThreadLeavesJobQueueOnDispose0", + "testThreadLeavesJobQueueOnDispose5000", + "testThreadLeavesJobQueueOnReply0", + "testThreadLeavesJobQueueOnReply5000", + "testStaticThreadExecutesJobs0", + "testStaticThreadExecutesJobs5000", + "testDynamicThreadExecutesJob", + "testStaticThreadExecutesAsyncs", + "testDynamicThreadExecutesAsyncs" }; + } + + public void testThreadLeavesJobQueueOnDispose0() throws InterruptedException + { + testThreadLeavesJobQueueOnDispose(0); + } + + public void testThreadLeavesJobQueueOnDispose5000() + throws InterruptedException + { + testThreadLeavesJobQueueOnDispose(5000); + } + + private void testThreadLeavesJobQueueOnDispose(int waitTime) + throws InterruptedException + { + TestThread t = new TestThread(waitTime); + t.waitToStart(); + String msg = "xcxxxxxxxx"; + t._jobQueue.dispose(t._disposeId, new RuntimeException (msg)); + t.waitToTerminate(); + assure("", t._message.equals(msg)); + } + + public void testThreadLeavesJobQueueOnReply0() throws InterruptedException { + testThreadLeavesJobQueueOnReply(0); + } + + public void testThreadLeavesJobQueueOnReply5000() + throws InterruptedException + { + testThreadLeavesJobQueueOnReply(5000); + } + + private void testThreadLeavesJobQueueOnReply(int waitTime) + throws InterruptedException + { + TestThread t = new TestThread(waitTime); + t.waitToStart(); + // put reply job: + t._jobQueue.putJob( + new Job(null, __iReceiver, + new Message( + null, false, "oid", __workAt_td, null, false, null, + false, null, null)), + null); + t.waitToTerminate(); + assure("", true); // TODO! ??? + } + + public void testStaticThreadExecutesJobs0() throws InterruptedException { + testStaticThreadExecutesJobs(0); + } + + public void testStaticThreadExecutesJobs5000() throws InterruptedException { + testStaticThreadExecutesJobs(5000); + } + + private void testStaticThreadExecutesJobs(int waitTime) + throws InterruptedException + { + TestThread t = new TestThread(waitTime); + t.waitToStart(); + testExecuteJobs(t._jobQueue); + t._jobQueue.dispose(t._disposeId, + new RuntimeException("xxxxxxxxxxxxx")); + t.waitToTerminate(); + } + + public void testDynamicThreadExecutesJob() throws InterruptedException { + testExecuteJobs( + new JobQueue( + __javaThreadPoolFactory, ThreadId.createFresh(), true)); + } + + public void testStaticThreadExecutesAsyncs() throws InterruptedException { + TestThread t = new TestThread(); + JobQueue async_jobQueue = new JobQueue(__javaThreadPoolFactory, + t._threadId); + assure("", async_jobQueue._ref_count == 1); + t._jobQueue = __javaThreadPoolFactory.getJobQueue(t._threadId); + assure("", t._jobQueue._ref_count == 1); + t.waitToStart(); + TestWorkAt workAt = new TestWorkAt(); + testAsyncJobQueue(workAt, async_jobQueue, t._threadId); + t._jobQueue.dispose(t._disposeId, + new RuntimeException("xxxxxxxxxxxxx")); + t.waitToTerminate(); + assure("", workAt._async_counter == TestWorkAt.MESSAGES); + assure("", workAt._sync_counter == TestWorkAt.MESSAGES); + } + + public void testDynamicThreadExecutesAsyncs() throws InterruptedException { + ThreadId threadId = ThreadId.createFresh(); + JobQueue async_jobQueue = new JobQueue(__javaThreadPoolFactory, + threadId); + TestWorkAt workAt = new TestWorkAt(); + testAsyncJobQueue(workAt, async_jobQueue, threadId); + assure("", workAt._async_counter == TestWorkAt.MESSAGES); + assure("", workAt._sync_counter == TestWorkAt.MESSAGES); + } + + private void testExecuteJobs(JobQueue jobQueue) throws InterruptedException + { + TestWorkAt workAt = new TestWorkAt(); + testSendRequests(workAt, "increment", jobQueue); + synchronized (workAt) { + jobQueue.putJob(new Job(workAt, __iReceiver, + new Message( + null, true, "oid", __workAt_td, + ((MethodDescription) + __workAt_td.getMethodDescription( + "notifyme")), + true, null, false, null, null)), + null); + while (!workAt._notified) { + workAt.wait(); + } + } + assure("", workAt._counter == TestWorkAt.MESSAGES); + } + + private void testAsyncJobQueue(TestWorkAt workAt, JobQueue async_jobQueue, + ThreadId threadId) + throws InterruptedException + { + // put slow async calls first, followed by fast sync calls: + testSendRequests(workAt, "asyncCall", async_jobQueue); + testSendRequests(workAt, "syncCall", + __javaThreadPoolFactory.getJobQueue(threadId)); + synchronized (workAt) { + async_jobQueue._sync_jobQueue.putJob( + new Job(workAt, __iReceiver, + new Message( + null, true, "oid", __workAt_td, + ((MethodDescription) + __workAt_td.getMethodDescription("notifyme")), + true, null, false, null, null)), + null); + while (!workAt._notified) { + workAt.wait(); + } + } + assure("", workAt.passedAsyncTest()); + } + + private void testSendRequests(TestWorkAt workAt, String operation, + JobQueue jobQueue) { + Message iMessage = new Message( + null, true, "oid", __workAt_td, + (MethodDescription) __workAt_td.getMethodDescription(operation), + true, null, false, null, null); + for (int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + jobQueue.putJob(new Job(workAt, __iReceiver, iMessage), + new Object()); + } + } + + private static final class TestThread extends Thread { + public final ThreadId _threadId = JavaThreadPoolFactory.getThreadId(); + public final Object _disposeId = new Object(); + public JobQueue _jobQueue = null; + public String _message; + + public TestThread(int waitTime) { + this.waitTime = waitTime; + _jobQueue = new JobQueue(__javaThreadPoolFactory, _threadId, false); + } + + public TestThread() { + waitTime = 0; + } + + public void run() { + synchronized (lock) { + state = STATE_STARTED; + lock.notifyAll(); + } + try { + if (waitTime != 0) { + Thread.sleep(waitTime); + } + _jobQueue.enter(_disposeId); + } catch (Throwable e) { + _message = e.getMessage(); + } + synchronized (lock) { + state = STATE_DONE; + lock.notifyAll(); + } + } + + public void waitToStart() throws InterruptedException { + start(); + synchronized (lock) { + while (state == STATE_INITIAL) { + lock.wait(); + } + } + } + + public void waitToTerminate() throws InterruptedException { + synchronized (lock) { + while (state != STATE_DONE) { + lock.wait(); + } + } + join(); + } + + private final int waitTime; + + private final Object lock = new Object(); + private int state = STATE_INITIAL; + private static final int STATE_INITIAL = 0; + private static final int STATE_STARTED = 1; + private static final int STATE_DONE = 2; + } + + private static final JavaThreadPoolFactory __javaThreadPoolFactory + = new JavaThreadPoolFactory(); + private static final IReceiver __iReceiver = new TestReceiver(); + private static final TypeDescription __workAt_td + = TypeDescription.getTypeDescription(TestIWorkAt.class); +} diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/TestIWorkAt.java b/jurt/test/com/sun/star/lib/uno/environments/remote/TestIWorkAt.java new file mode 100644 index 000000000000..1c616a84541b --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/TestIWorkAt.java @@ -0,0 +1,49 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org 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 Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package com.sun.star.lib.uno.environments.remote; + + +import com.sun.star.lib.uno.typeinfo.MethodTypeInfo; +import com.sun.star.lib.uno.typeinfo.TypeInfo; +import com.sun.star.uno.XInterface; + +public interface TestIWorkAt extends XInterface { + void syncCall() throws Throwable ; + void asyncCall() throws Throwable ; + + void increment() throws Throwable; + + void notifyme(); + + public static final TypeInfo UNOTYPEINFO[] = { + new MethodTypeInfo("increment", 0, 0), + new MethodTypeInfo("notifyme", 1, 0), + new MethodTypeInfo("syncCall", 2, 0), + new MethodTypeInfo("asyncCall", 3, 0) + }; +} diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/TestMessage.java b/jurt/test/com/sun/star/lib/uno/environments/remote/TestMessage.java new file mode 100644 index 000000000000..f47048e423ed --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/TestMessage.java @@ -0,0 +1,89 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org 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 Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +package com.sun.star.lib.uno.environments.remote; + + +import com.sun.star.uno.Type; + +import com.sun.star.uno.ITypeDescription; + + +class TestMessage implements IMessage { + boolean _synchron; + ITypeDescription _iTypeDescription; + String _oid; + ThreadId _threadId; + Object _result; + String _operation; + Object _params[]; + + TestMessage(boolean synchron, ITypeDescription iTypeDescription, String oid, ThreadId threadId, Object result, String operation, Object params[]) { + _synchron = synchron; + _iTypeDescription = iTypeDescription; + _oid = oid; + _threadId = threadId; + _result = result; + _operation = operation; + _params = params; + } + + public String getOperation() { + return _operation; + } + + public ThreadId getThreadId() { + return _threadId; + } + + public ITypeDescription getInterface() { + return _iTypeDescription; + } + + public boolean isSynchron() { + return _synchron; + } + + public boolean mustReply() { + return _synchron; + } + + public boolean isException() { + return false; + } + + public String getOid() { + return _oid; + } + + public Object getData(Object params[][]) { + params[0] = _params; + return _result; +// return new Integer(_requestId); + } +} + + diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/TestReceiver.java b/jurt/test/com/sun/star/lib/uno/environments/remote/TestReceiver.java new file mode 100644 index 000000000000..32ba5ba7473b --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/TestReceiver.java @@ -0,0 +1,33 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org 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 Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package com.sun.star.lib.uno.environments.remote; + +final class TestReceiver implements IReceiver { + public void sendReply(boolean exception, ThreadId threadId, Object result) { + } +} diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/TestWorkAt.java b/jurt/test/com/sun/star/lib/uno/environments/remote/TestWorkAt.java new file mode 100644 index 000000000000..322d7573c697 --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/TestWorkAt.java @@ -0,0 +1,97 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org 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 Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package com.sun.star.lib.uno.environments.remote; + +class TestWorkAt implements TestIWorkAt { + /** + * When set to true, enables various debugging output. + */ + private static final boolean DEBUG = false; + + static final int MESSAGES = 35; + + + int _counter; + + int _sync_counter; + int _async_counter; + + Thread _sync_thread; + Thread _async_thread; + + boolean _passedAync = true; + boolean _notified = false; + + public void syncCall() throws Throwable { + ++ _sync_counter; + + if(_async_counter != MESSAGES) + _passedAync = false; + + if(_sync_thread == null) + _sync_thread = Thread.currentThread(); + +// if(_sync_thread != Thread.currentThread()) +// _passedAync = false; + + if(DEBUG) System.err.println("syncCall:" + _sync_counter + " " + _passedAync + " " + Thread.currentThread()); + } + + public void asyncCall() throws Throwable { +// Thread.sleep(50); + + ++ _async_counter; + + if(_async_thread == null) + _async_thread = Thread.currentThread(); + +// if(_async_thread != Thread.currentThread()) +// _passedAync = false; + + if(DEBUG) System.err.println("asyncCall:" + _async_counter + " " + Thread.currentThread()); + } + + public synchronized void increment() throws Throwable { + if(DEBUG) System.err.println("increment - " + Thread.currentThread()); + + ++ _counter; + notifyAll(); + } + + public synchronized void notifyme() { + if(DEBUG) System.err.println("\t\t\tnotifying me" + Thread.currentThread()); + + notifyAll(); + + _notified = true; + } + + public boolean passedAsyncTest() { + return _passedAync && (_sync_counter == MESSAGES); + } +} diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/ThreadId_Test.java b/jurt/test/com/sun/star/lib/uno/environments/remote/ThreadId_Test.java new file mode 100644 index 000000000000..d9154ce6b0ef --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/ThreadId_Test.java @@ -0,0 +1,63 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org 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 Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package com.sun.star.lib.uno.environments.remote; + +import complexlib.ComplexTestCase; +import java.util.Arrays; + +public final class ThreadId_Test extends ComplexTestCase { + public String[] getTestMethodNames() { + return new String[] { "test" }; + } + + public void test() { + ThreadId i1 = ThreadId.createFresh(); + assure(i1.equals(i1)); + assure(!i1.equals(null)); + assure(!i1.equals(new Object())); + assure(i1.hashCode() == i1.hashCode()); + byte[] i1bytes = i1.getBytes(); + assure(i1bytes != null); + assure( + i1bytes.length >= 5 && i1bytes[0] == 'j' && i1bytes[1] == 'a' + && i1bytes[2] == 'v' && i1bytes[3] == 'a' && i1bytes[4] == ':'); + assure(Arrays.equals(i1bytes, i1.getBytes())); + + ThreadId i2 = ThreadId.createFresh(); + assure(!i1.equals(i2)); + assure(!i2.equals(i1)); + assure(!Arrays.equals(i1bytes, i2.getBytes())); + + ThreadId i3 = new ThreadId(i1bytes); + assure(i3.equals(i1)); + assure(!i3.equals(i2)); + assure(i1.equals(i3)); + assure(i1.hashCode() == i3.hashCode()); + assure(Arrays.equals(i1bytes, i3.getBytes())); + } +} diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/ThreadPool_Test.java b/jurt/test/com/sun/star/lib/uno/environments/remote/ThreadPool_Test.java new file mode 100644 index 000000000000..bed90acfbbcb --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/ThreadPool_Test.java @@ -0,0 +1,443 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org 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 Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package com.sun.star.lib.uno.environments.remote; + +import com.sun.star.lib.uno.typedesc.MethodDescription; +import com.sun.star.lib.uno.typedesc.TypeDescription; +import complexlib.ComplexTestCase; + +public class ThreadPool_Test extends ComplexTestCase { + public String getTestObjectName() { + return getClass().getName(); + } + + public String[] getTestMethodNames() { + return new String[] { "testDispose", + "testThreadAsync", + "testDynamicThreadSync", + "testStaticThreadSync", + "testDynamicThreadAsyncSyncOrder", + "testStaticThreadAsyncSyncOrder", + "testStress", + "testAsyncSync" }; + } + + public void testDispose() throws InterruptedException { + IThreadPool iThreadPool = ThreadPoolManager.create(); + TestThread testThread = new TestThread(iThreadPool); + + ThreadId threadId = null; + + // start the test thread + synchronized(testThread) { + testThread.start(); + + testThread.wait(); + + threadId = testThread._threadId; + + // let the thread attach and enter the threadpool + testThread.notifyAll(); + } + + String message = "blabla"; + + // terminate the test thread + synchronized(testThread) { + // put reply job + iThreadPool.dispose(new RuntimeException(message)); + + testThread.wait(); + } + + testThread.join(); + + assure("", testThread._message.equals(message)); + } + + public void testThreadAsync() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + + ThreadId threadId = ThreadId.createFresh(); + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, false, threadId, "increment"); + } + + synchronized(workAt) { + putJob(workAt, false, threadId, "notifyme"); + + while(!workAt._notified) + workAt.wait(); + } + + assure("", workAt._counter == TestWorkAt.MESSAGES); + } + + public void testDynamicThreadSync() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + + ThreadId threadId = ThreadId.createFresh(); + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, true, threadId, "increment"); + } + + synchronized(workAt) { + putJob(workAt, true, threadId, "notifyme"); + + while(!workAt._notified) + workAt.wait(); + } + + assure("", workAt._counter == TestWorkAt.MESSAGES); + } + + public void testStaticThreadSync() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + + TestThread testThread = new TestThread(); + + ThreadId threadId = null; + + // start the test thread + synchronized(testThread) { + testThread.start(); + + testThread.wait(); + + threadId = testThread._threadId; + + // let the thread attach and enter the threadpool + testThread.notifyAll(); + } + + // queue syncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, true, threadId, "increment"); + } + + // terminate the test thread + synchronized(testThread) { + // put reply job + putJob(workAt, true, threadId, null); + + testThread.wait(); + } + + testThread.join(); + + assure("", workAt._counter == TestWorkAt.MESSAGES); + } + + public void testDynamicThreadAsyncSyncOrder() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + + ThreadId threadId = ThreadId.createFresh(); + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, false, threadId, "asyncCall"); + } + + // queue syncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, true, threadId, "syncCall"); + } + + synchronized(workAt) { + putJob(workAt, true, threadId, "notifyme"); + + while(!workAt._notified) + workAt.wait(); + } + + assure("", workAt.passedAsyncTest()); + } + + public void testStaticThreadAsyncSyncOrder() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + + TestThread testThread = new TestThread(); + + // start the test thread + synchronized(testThread) { + testThread.start(); + + testThread.wait(); + } + + ThreadId threadId = testThread._threadId; + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, false, threadId, "asyncCall"); + } + + // let the thread attach and enter the threadpool + synchronized(testThread) { + testThread.notifyAll(); + } + + // queue syncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, true, threadId, "syncCall"); + } + + // terminate the test thread + synchronized(testThread) { + // put reply job + putJob(workAt, true, threadId, null); + + testThread.wait(); + } + + testThread.join(); + + assure("", workAt.passedAsyncTest()); + } + + public void testStress() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + for (int i = 0; i < TestWorkAt.MESSAGES; ++i) { + Thread.yield(); // force scheduling + ThreadId threadID = ThreadId.createFresh(); + putJob(workAt, true, threadID, "increment"); + putJob(workAt, false, threadID, "increment"); + } + synchronized (workAt) { + while (workAt._counter < 2 * TestWorkAt.MESSAGES) { + workAt.wait(); + } + } + + abstract class Stress extends Thread { + public Stress(int count) { + this.count = count; + } + + public void run() { + try { + for (int i = 0; i < count; ++i) { + runTest(); + } + } catch (Throwable e) { + e.printStackTrace(System.err); + } + } + + protected abstract void runTest() throws InterruptedException; + + private final int count; + }; + + Stress stress1 = new Stress(50) { + protected void runTest() throws InterruptedException { + testThreadAsync(); + } + }; + stress1.start(); + + Stress stress2 = new Stress(50) { + protected void runTest() throws InterruptedException { + testDynamicThreadSync(); + } + }; + stress2.start(); + + Stress stress3 = new Stress(50) { + protected void runTest() throws InterruptedException { + testStaticThreadSync(); + } + }; + stress3.start(); + + Stress stress4 = new Stress(50) { + protected void runTest() throws InterruptedException { + testDynamicThreadAsyncSyncOrder(); + } + }; + stress4.start(); + + Stress stress5 = new Stress(50) { + protected void runTest() throws InterruptedException { + testStaticThreadAsyncSyncOrder(); + } + }; + stress5.start(); + + Stress stress6 = new Stress(500) { + protected void runTest() throws InterruptedException { + testDispose(); + } + }; + stress6.start(); + + stress1.join(); + stress2.join(); + stress3.join(); + stress4.join(); + stress5.join(); + stress6.join(); + } + + public void testAsyncSync() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + ThreadId threadId = ThreadId.createFresh(); + MyWorkAt myWorkAt = new MyWorkAt( workAt ); + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + if( i == 2 ) + { + putJob( myWorkAt, false , threadId, "asyncCall" ); + } + putJob(workAt, false, threadId, "asyncCall"); + } + + synchronized(workAt) { + putJob(workAt, false, threadId, "notifyme"); + + while(!workAt._notified) + workAt.wait(); + } + + assure("", + workAt._async_counter == TestWorkAt.MESSAGES + && myWorkAt._success); + } + + private static void putJob(TestIWorkAt iWorkAt, boolean synchron, + ThreadId threadId, String operation) { + __iThreadPool.putJob( + new Job(iWorkAt, __iReceiver, + new Message( + threadId, operation != null, "oid", __workAt_td, + (operation == null + ? null + : ((MethodDescription) + __workAt_td.getMethodDescription(operation))), + synchron, null, false, null, null))); + } + + private static final class TestThread extends Thread { + ThreadId _threadId; + Object _disposeId = new Object(); + String _message; + IThreadPool _iThreadPool; + + TestThread() { + this(__iThreadPool); + } + + TestThread(IThreadPool iThreadPool) { + _iThreadPool = iThreadPool; + } + + public void run() { + _threadId = _iThreadPool.getThreadId(); + + + try { + synchronized(this) { + // notify that we are running + notify(); + + _iThreadPool.attach(); + + // wait until we should continue + wait(); + } + + _iThreadPool.enter(); + } + catch(Throwable throwable) { + _message = throwable.getMessage(); + } + + _iThreadPool.detach(); + + synchronized(this) { + // notify the listeners that we are dying + notifyAll(); + } + } + } + + private static final class MyWorkAt implements TestIWorkAt { + public MyWorkAt( TestWorkAt async_WorkAt ) { + _async_WorkAt = async_WorkAt; + } + + public void syncCall() throws Throwable + { + Message iMessage = new Message( + __iThreadPool.getThreadId(), false, "oid", __workAt_td, null, + false, null, false, null, null); + + // marshal reply + ThreadPool_Test.__iThreadPool.putJob( + new Job(this, ThreadPool_Test. __iReceiver, iMessage)); + } + + public void asyncCall() throws Throwable { + for (int i = 0 ; i < 5 ; ++i) { + ThreadPool_Test.__iThreadPool.attach(); + ThreadPool_Test.putJob(this, true, __iThreadPool.getThreadId(), + "syncCall"); + // wait for reply + ThreadPool_Test.__iThreadPool.enter(); + ThreadPool_Test.__iThreadPool.detach(); + } + // async must have waited for this call + _success = _async_WorkAt._async_counter == 2; + } + + public void increment() throws Throwable {} + + public void notifyme() {} + + public boolean _success = false; + + private final TestWorkAt _async_WorkAt; + } + + private static final IThreadPool __iThreadPool = ThreadPoolManager.create(); + private static final IReceiver __iReceiver = new TestReceiver(); + private static final TypeDescription __workAt_td + = TypeDescription.getTypeDescription(TestIWorkAt.class); +} diff --git a/jurt/test/com/sun/star/lib/uno/environments/remote/makefile.mk b/jurt/test/com/sun/star/lib/uno/environments/remote/makefile.mk new file mode 100644 index 000000000000..e2323a54b497 --- /dev/null +++ b/jurt/test/com/sun/star/lib/uno/environments/remote/makefile.mk @@ -0,0 +1,44 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org 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 Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ := ..$/..$/..$/..$/..$/..$/..$/.. +PRJNAME := jurt +TARGET := test_com_sun_star_lib_uno_environments_remote + +PACKAGE := com$/sun$/star$/lib$/uno$/environments$/remote +JAVATESTFILES := \ + JavaThreadPoolFactory_Test.java \ + JobQueue_Test.java \ + ThreadId_Test.java \ + ThreadPool_Test.java +JAVAFILES := \ + TestIWorkAt.java \ + TestReceiver.java \ + TestWorkAt.java +JARFILES := ridl.jar + +.INCLUDE: javaunittest.mk |