12be43276SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 32be43276SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 42be43276SAndrew Rist * or more contributor license agreements. See the NOTICE file 52be43276SAndrew Rist * distributed with this work for additional information 62be43276SAndrew Rist * regarding copyright ownership. The ASF licenses this file 72be43276SAndrew Rist * to you under the Apache License, Version 2.0 (the 82be43276SAndrew Rist * "License"); you may not use this file except in compliance 92be43276SAndrew Rist * with the License. You may obtain a copy of the License at 102be43276SAndrew Rist * 112be43276SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 122be43276SAndrew Rist * 132be43276SAndrew Rist * Unless required by applicable law or agreed to in writing, 142be43276SAndrew Rist * software distributed under the License is distributed on an 152be43276SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 162be43276SAndrew Rist * KIND, either express or implied. See the License for the 172be43276SAndrew Rist * specific language governing permissions and limitations 182be43276SAndrew Rist * under the License. 192be43276SAndrew Rist * 202be43276SAndrew Rist *************************************************************/ 212be43276SAndrew Rist 222be43276SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.lib.uno.environments.remote; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.lib.uno.typedesc.MethodDescription; 27cdf0e10cSrcweir import com.sun.star.lib.uno.typedesc.TypeDescription; 28cdf0e10cSrcweir 29*9cbd97ceSDamjan Jovanovic import org.junit.Test; 30*9cbd97ceSDamjan Jovanovic import static org.junit.Assert.*; 31cdf0e10cSrcweir 32*9cbd97ceSDamjan Jovanovic public final class JobQueue_Test { 33*9cbd97ceSDamjan Jovanovic @Test testThreadLeavesJobQueueOnDispose0()34cdf0e10cSrcweir public void testThreadLeavesJobQueueOnDispose0() throws InterruptedException 35cdf0e10cSrcweir { 36cdf0e10cSrcweir testThreadLeavesJobQueueOnDispose(0); 37cdf0e10cSrcweir } 38cdf0e10cSrcweir 39*9cbd97ceSDamjan Jovanovic @Test testThreadLeavesJobQueueOnDispose5000()40cdf0e10cSrcweir public void testThreadLeavesJobQueueOnDispose5000() 41cdf0e10cSrcweir throws InterruptedException 42cdf0e10cSrcweir { 43cdf0e10cSrcweir testThreadLeavesJobQueueOnDispose(5000); 44cdf0e10cSrcweir } 45cdf0e10cSrcweir testThreadLeavesJobQueueOnDispose(int waitTime)46cdf0e10cSrcweir private void testThreadLeavesJobQueueOnDispose(int waitTime) 47cdf0e10cSrcweir throws InterruptedException 48cdf0e10cSrcweir { 49cdf0e10cSrcweir TestThread t = new TestThread(waitTime); 50cdf0e10cSrcweir t.waitToStart(); 51cdf0e10cSrcweir String msg = "xcxxxxxxxx"; 52cdf0e10cSrcweir t._jobQueue.dispose(t._disposeId, new RuntimeException (msg)); 53cdf0e10cSrcweir t.waitToTerminate(); 54*9cbd97ceSDamjan Jovanovic assertTrue("", t._message.equals(msg)); 55cdf0e10cSrcweir } 56cdf0e10cSrcweir 57*9cbd97ceSDamjan Jovanovic @Test testThreadLeavesJobQueueOnReply0()58cdf0e10cSrcweir public void testThreadLeavesJobQueueOnReply0() throws InterruptedException { 59cdf0e10cSrcweir testThreadLeavesJobQueueOnReply(0); 60cdf0e10cSrcweir } 61cdf0e10cSrcweir 62*9cbd97ceSDamjan Jovanovic @Test testThreadLeavesJobQueueOnReply5000()63cdf0e10cSrcweir public void testThreadLeavesJobQueueOnReply5000() 64cdf0e10cSrcweir throws InterruptedException 65cdf0e10cSrcweir { 66cdf0e10cSrcweir testThreadLeavesJobQueueOnReply(5000); 67cdf0e10cSrcweir } 68cdf0e10cSrcweir testThreadLeavesJobQueueOnReply(int waitTime)69cdf0e10cSrcweir private void testThreadLeavesJobQueueOnReply(int waitTime) 70cdf0e10cSrcweir throws InterruptedException 71cdf0e10cSrcweir { 72cdf0e10cSrcweir TestThread t = new TestThread(waitTime); 73cdf0e10cSrcweir t.waitToStart(); 74cdf0e10cSrcweir // put reply job: 75cdf0e10cSrcweir t._jobQueue.putJob( 76cdf0e10cSrcweir new Job(null, __iReceiver, 77cdf0e10cSrcweir new Message( 78cdf0e10cSrcweir null, false, "oid", __workAt_td, null, false, null, 79cdf0e10cSrcweir false, null, null)), 80cdf0e10cSrcweir null); 81cdf0e10cSrcweir t.waitToTerminate(); 82*9cbd97ceSDamjan Jovanovic assertTrue("", true); // TODO! ??? 83cdf0e10cSrcweir } 84cdf0e10cSrcweir 85*9cbd97ceSDamjan Jovanovic @Test testStaticThreadExecutesJobs0()86cdf0e10cSrcweir public void testStaticThreadExecutesJobs0() throws InterruptedException { 87cdf0e10cSrcweir testStaticThreadExecutesJobs(0); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90*9cbd97ceSDamjan Jovanovic @Test testStaticThreadExecutesJobs5000()91cdf0e10cSrcweir public void testStaticThreadExecutesJobs5000() throws InterruptedException { 92cdf0e10cSrcweir testStaticThreadExecutesJobs(5000); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir testStaticThreadExecutesJobs(int waitTime)95cdf0e10cSrcweir private void testStaticThreadExecutesJobs(int waitTime) 96cdf0e10cSrcweir throws InterruptedException 97cdf0e10cSrcweir { 98cdf0e10cSrcweir TestThread t = new TestThread(waitTime); 99cdf0e10cSrcweir t.waitToStart(); 100cdf0e10cSrcweir testExecuteJobs(t._jobQueue); 101cdf0e10cSrcweir t._jobQueue.dispose(t._disposeId, 102cdf0e10cSrcweir new RuntimeException("xxxxxxxxxxxxx")); 103cdf0e10cSrcweir t.waitToTerminate(); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106*9cbd97ceSDamjan Jovanovic @Test testDynamicThreadExecutesJob()107cdf0e10cSrcweir public void testDynamicThreadExecutesJob() throws InterruptedException { 108cdf0e10cSrcweir testExecuteJobs( 109cdf0e10cSrcweir new JobQueue( 110cdf0e10cSrcweir __javaThreadPoolFactory, ThreadId.createFresh(), true)); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113*9cbd97ceSDamjan Jovanovic @Test testStaticThreadExecutesAsyncs()114cdf0e10cSrcweir public void testStaticThreadExecutesAsyncs() throws InterruptedException { 115cdf0e10cSrcweir TestThread t = new TestThread(); 116cdf0e10cSrcweir JobQueue async_jobQueue = new JobQueue(__javaThreadPoolFactory, 117cdf0e10cSrcweir t._threadId); 118*9cbd97ceSDamjan Jovanovic assertTrue("", async_jobQueue._ref_count == 1); 119cdf0e10cSrcweir t._jobQueue = __javaThreadPoolFactory.getJobQueue(t._threadId); 120*9cbd97ceSDamjan Jovanovic assertTrue("", t._jobQueue._ref_count == 1); 121cdf0e10cSrcweir t.waitToStart(); 122cdf0e10cSrcweir TestWorkAt workAt = new TestWorkAt(); 123cdf0e10cSrcweir testAsyncJobQueue(workAt, async_jobQueue, t._threadId); 124cdf0e10cSrcweir t._jobQueue.dispose(t._disposeId, 125cdf0e10cSrcweir new RuntimeException("xxxxxxxxxxxxx")); 126cdf0e10cSrcweir t.waitToTerminate(); 127*9cbd97ceSDamjan Jovanovic assertTrue("", workAt._async_counter == TestWorkAt.MESSAGES); 128*9cbd97ceSDamjan Jovanovic assertTrue("", workAt._sync_counter == TestWorkAt.MESSAGES); 129cdf0e10cSrcweir } 130cdf0e10cSrcweir 131*9cbd97ceSDamjan Jovanovic @Test testDynamicThreadExecutesAsyncs()132cdf0e10cSrcweir public void testDynamicThreadExecutesAsyncs() throws InterruptedException { 133cdf0e10cSrcweir ThreadId threadId = ThreadId.createFresh(); 134cdf0e10cSrcweir JobQueue async_jobQueue = new JobQueue(__javaThreadPoolFactory, 135cdf0e10cSrcweir threadId); 136cdf0e10cSrcweir TestWorkAt workAt = new TestWorkAt(); 137cdf0e10cSrcweir testAsyncJobQueue(workAt, async_jobQueue, threadId); 138*9cbd97ceSDamjan Jovanovic assertTrue("", workAt._async_counter == TestWorkAt.MESSAGES); 139*9cbd97ceSDamjan Jovanovic assertTrue("", workAt._sync_counter == TestWorkAt.MESSAGES); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir testExecuteJobs(JobQueue jobQueue)142cdf0e10cSrcweir private void testExecuteJobs(JobQueue jobQueue) throws InterruptedException 143cdf0e10cSrcweir { 144cdf0e10cSrcweir TestWorkAt workAt = new TestWorkAt(); 145cdf0e10cSrcweir testSendRequests(workAt, "increment", jobQueue); 146cdf0e10cSrcweir synchronized (workAt) { 147cdf0e10cSrcweir jobQueue.putJob(new Job(workAt, __iReceiver, 148cdf0e10cSrcweir new Message( 149cdf0e10cSrcweir null, true, "oid", __workAt_td, 150cdf0e10cSrcweir ((MethodDescription) 151cdf0e10cSrcweir __workAt_td.getMethodDescription( 152cdf0e10cSrcweir "notifyme")), 153cdf0e10cSrcweir true, null, false, null, null)), 154cdf0e10cSrcweir null); 155cdf0e10cSrcweir while (!workAt._notified) { 156cdf0e10cSrcweir workAt.wait(); 157cdf0e10cSrcweir } 158cdf0e10cSrcweir } 159*9cbd97ceSDamjan Jovanovic assertTrue("", workAt._counter == TestWorkAt.MESSAGES); 160cdf0e10cSrcweir } 161cdf0e10cSrcweir testAsyncJobQueue(TestWorkAt workAt, JobQueue async_jobQueue, ThreadId threadId)162cdf0e10cSrcweir private void testAsyncJobQueue(TestWorkAt workAt, JobQueue async_jobQueue, 163cdf0e10cSrcweir ThreadId threadId) 164cdf0e10cSrcweir throws InterruptedException 165cdf0e10cSrcweir { 166cdf0e10cSrcweir // put slow async calls first, followed by fast sync calls: 167cdf0e10cSrcweir testSendRequests(workAt, "asyncCall", async_jobQueue); 168cdf0e10cSrcweir testSendRequests(workAt, "syncCall", 169cdf0e10cSrcweir __javaThreadPoolFactory.getJobQueue(threadId)); 170cdf0e10cSrcweir synchronized (workAt) { 171cdf0e10cSrcweir async_jobQueue._sync_jobQueue.putJob( 172cdf0e10cSrcweir new Job(workAt, __iReceiver, 173cdf0e10cSrcweir new Message( 174cdf0e10cSrcweir null, true, "oid", __workAt_td, 175cdf0e10cSrcweir ((MethodDescription) 176cdf0e10cSrcweir __workAt_td.getMethodDescription("notifyme")), 177cdf0e10cSrcweir true, null, false, null, null)), 178cdf0e10cSrcweir null); 179cdf0e10cSrcweir while (!workAt._notified) { 180cdf0e10cSrcweir workAt.wait(); 181cdf0e10cSrcweir } 182cdf0e10cSrcweir } 183*9cbd97ceSDamjan Jovanovic assertTrue("", workAt.passedAsyncTest()); 184cdf0e10cSrcweir } 185cdf0e10cSrcweir testSendRequests(TestWorkAt workAt, String operation, JobQueue jobQueue)186cdf0e10cSrcweir private void testSendRequests(TestWorkAt workAt, String operation, 187cdf0e10cSrcweir JobQueue jobQueue) { 188cdf0e10cSrcweir Message iMessage = new Message( 189cdf0e10cSrcweir null, true, "oid", __workAt_td, 190cdf0e10cSrcweir (MethodDescription) __workAt_td.getMethodDescription(operation), 191cdf0e10cSrcweir true, null, false, null, null); 192cdf0e10cSrcweir for (int i = 0; i < TestWorkAt.MESSAGES; ++ i) { 193cdf0e10cSrcweir Thread.yield(); // force scheduling 194cdf0e10cSrcweir jobQueue.putJob(new Job(workAt, __iReceiver, iMessage), 195cdf0e10cSrcweir new Object()); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir } 198cdf0e10cSrcweir 199cdf0e10cSrcweir private static final class TestThread extends Thread { 200cdf0e10cSrcweir public final ThreadId _threadId = JavaThreadPoolFactory.getThreadId(); 201cdf0e10cSrcweir public final Object _disposeId = new Object(); 202cdf0e10cSrcweir public JobQueue _jobQueue = null; 203cdf0e10cSrcweir public String _message; 204cdf0e10cSrcweir TestThread(int waitTime)205cdf0e10cSrcweir public TestThread(int waitTime) { 206cdf0e10cSrcweir this.waitTime = waitTime; 207cdf0e10cSrcweir _jobQueue = new JobQueue(__javaThreadPoolFactory, _threadId, false); 208cdf0e10cSrcweir } 209cdf0e10cSrcweir TestThread()210cdf0e10cSrcweir public TestThread() { 211cdf0e10cSrcweir waitTime = 0; 212cdf0e10cSrcweir } 213cdf0e10cSrcweir run()214cdf0e10cSrcweir public void run() { 215cdf0e10cSrcweir synchronized (lock) { 216cdf0e10cSrcweir state = STATE_STARTED; 217cdf0e10cSrcweir lock.notifyAll(); 218cdf0e10cSrcweir } 219cdf0e10cSrcweir try { 220cdf0e10cSrcweir if (waitTime != 0) { 221cdf0e10cSrcweir Thread.sleep(waitTime); 222cdf0e10cSrcweir } 223cdf0e10cSrcweir _jobQueue.enter(_disposeId); 224cdf0e10cSrcweir } catch (Throwable e) { 225cdf0e10cSrcweir _message = e.getMessage(); 226cdf0e10cSrcweir } 227cdf0e10cSrcweir synchronized (lock) { 228cdf0e10cSrcweir state = STATE_DONE; 229cdf0e10cSrcweir lock.notifyAll(); 230cdf0e10cSrcweir } 231cdf0e10cSrcweir } 232cdf0e10cSrcweir waitToStart()233cdf0e10cSrcweir public void waitToStart() throws InterruptedException { 234cdf0e10cSrcweir start(); 235cdf0e10cSrcweir synchronized (lock) { 236cdf0e10cSrcweir while (state == STATE_INITIAL) { 237cdf0e10cSrcweir lock.wait(); 238cdf0e10cSrcweir } 239cdf0e10cSrcweir } 240cdf0e10cSrcweir } 241cdf0e10cSrcweir waitToTerminate()242cdf0e10cSrcweir public void waitToTerminate() throws InterruptedException { 243cdf0e10cSrcweir synchronized (lock) { 244cdf0e10cSrcweir while (state != STATE_DONE) { 245cdf0e10cSrcweir lock.wait(); 246cdf0e10cSrcweir } 247cdf0e10cSrcweir } 248cdf0e10cSrcweir join(); 249cdf0e10cSrcweir } 250cdf0e10cSrcweir 251cdf0e10cSrcweir private final int waitTime; 252cdf0e10cSrcweir 253cdf0e10cSrcweir private final Object lock = new Object(); 254cdf0e10cSrcweir private int state = STATE_INITIAL; 255cdf0e10cSrcweir private static final int STATE_INITIAL = 0; 256cdf0e10cSrcweir private static final int STATE_STARTED = 1; 257cdf0e10cSrcweir private static final int STATE_DONE = 2; 258cdf0e10cSrcweir } 259cdf0e10cSrcweir 260cdf0e10cSrcweir private static final JavaThreadPoolFactory __javaThreadPoolFactory 261cdf0e10cSrcweir = new JavaThreadPoolFactory(); 262cdf0e10cSrcweir private static final IReceiver __iReceiver = new TestReceiver(); 263cdf0e10cSrcweir private static final TypeDescription __workAt_td 264cdf0e10cSrcweir = TypeDescription.getTypeDescription(TestIWorkAt.class); 265cdf0e10cSrcweir } 266