1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.lib.uno.bridges.javaremote;
25 
26 import com.sun.star.bridge.XInstanceProvider;
27 import com.sun.star.lang.DisposedException;
28 import com.sun.star.lib.TestBed;
29 import com.sun.star.lib.uno.typeinfo.MethodTypeInfo;
30 import com.sun.star.lib.uno.typeinfo.TypeInfo;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XComponentContext;
33 import com.sun.star.uno.XInterface;
34 import complexlib.ComplexTestCase;
35 
36 /* This test has to detect whether the spawned client process hangs, which can
37  * not be done reliably.  As an approximation, it waits for 10 sec and considers
38  * the process hanging if it has not terminated by then.
39  */
40 public final class StopMessageDispatcherTest extends ComplexTestCase {
StopMessageDispatcherTest()41     public StopMessageDispatcherTest() {}
42 
getTestMethodNames()43     public String[] getTestMethodNames() {
44         return new String[] { "test" };
45     }
46 
test()47     public void test() throws Exception {
48         assure(
49             "test",
50             new TestBed().execute(new Provider(), false, Client.class, 10000));
51     }
52 
53     public static final class Client extends TestBed.Client {
main(String[] args)54         public static void main(String[] args) {
55             new Client().execute();
56         }
57 
run(XComponentContext context)58         protected boolean run(XComponentContext context) throws Throwable {
59             XTest test = UnoRuntime.queryInterface(
60                 XTest.class, getBridge(context).getInstance("Test"));
61             Thread[] threads = new Thread[101];
62             int n = Thread.enumerate(threads);
63             if (n > 100) {
64                 System.err.println("ERROR: too many threads");
65                 return false;
66             }
67             boolean stopped = false;
68             for (int i = 0; i < n; ++i) {
69                 if (threads[i].getName().equals("MessageDispatcher")) {
70                     threads[i].stop();
71                     stopped = true;
72                     break;
73                 }
74             }
75             if (!stopped) {
76                 System.err.println("ERROR: thread not found");
77                 return false;
78             }
79             try {
80                 test.call();
81                 System.err.println("ERROR: no DisposedException");
82                 return false;
83             } catch (DisposedException e) {
84                 return true;
85             }
86         }
87 
Client()88         private Client() {}
89     }
90 
91     private static final class Provider implements XInstanceProvider {
getInstance(String instanceName)92         public Object getInstance(String instanceName) {
93             return new XTest() {
94                     public void call() {}
95                 };
96         }
97     }
98 
99     public interface XTest extends XInterface {
call()100         void call();
101 
102         TypeInfo[] UNOTYPEINFO = { new MethodTypeInfo("call", 0, 0) };
103     }
104 }
105