xref: /aosp_15_r20/art/test/522-checker-regression-monitor-exit/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method;
18*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Type;
19*795d594fSAndroid Build Coastguard Worker import java.util.ArrayList;
20*795d594fSAndroid Build Coastguard Worker import java.util.List;
21*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.Callable;
22*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.Executors;
23*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.ExecutorService;
24*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.Future;
25*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.TimeUnit;
26*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.CancellationException;
27*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.TimeoutException;
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker public class Main {
30*795d594fSAndroid Build Coastguard Worker   private static class HashCodeQuery implements Callable<Integer> {
HashCodeQuery(Object obj)31*795d594fSAndroid Build Coastguard Worker     public HashCodeQuery(Object obj) {
32*795d594fSAndroid Build Coastguard Worker       m_obj = obj;
33*795d594fSAndroid Build Coastguard Worker     }
34*795d594fSAndroid Build Coastguard Worker 
call()35*795d594fSAndroid Build Coastguard Worker     public Integer call() {
36*795d594fSAndroid Build Coastguard Worker       Integer result;
37*795d594fSAndroid Build Coastguard Worker       try {
38*795d594fSAndroid Build Coastguard Worker         Class<?> c = Class.forName("Test");
39*795d594fSAndroid Build Coastguard Worker         Method m = c.getMethod("synchronizedHashCode", Object.class);
40*795d594fSAndroid Build Coastguard Worker         result = (Integer) m.invoke(null, m_obj);
41*795d594fSAndroid Build Coastguard Worker       } catch (Exception e) {
42*795d594fSAndroid Build Coastguard Worker         System.out.println("Hash code query exception");
43*795d594fSAndroid Build Coastguard Worker         e.printStackTrace(System.out);
44*795d594fSAndroid Build Coastguard Worker         result = -1;
45*795d594fSAndroid Build Coastguard Worker       }
46*795d594fSAndroid Build Coastguard Worker       return result;
47*795d594fSAndroid Build Coastguard Worker     }
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker     private Object m_obj;
50*795d594fSAndroid Build Coastguard Worker     private int m_index;
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker 
main(String args[])53*795d594fSAndroid Build Coastguard Worker   public static void main(String args[]) throws Exception {
54*795d594fSAndroid Build Coastguard Worker     Object obj = new Object();
55*795d594fSAndroid Build Coastguard Worker     int numThreads = 10;
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker     ExecutorService pool = Executors.newFixedThreadPool(numThreads);
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker     List<HashCodeQuery> queries = new ArrayList<HashCodeQuery>(numThreads);
60*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < numThreads; ++i) {
61*795d594fSAndroid Build Coastguard Worker       queries.add(new HashCodeQuery(obj));
62*795d594fSAndroid Build Coastguard Worker     }
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker     try {
65*795d594fSAndroid Build Coastguard Worker       List<Future<Integer>> results = pool.invokeAll(queries);
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker       int hash = obj.hashCode();
68*795d594fSAndroid Build Coastguard Worker       for (int i = 0; i < numThreads; ++i) {
69*795d594fSAndroid Build Coastguard Worker         int result = results.get(i).get();
70*795d594fSAndroid Build Coastguard Worker         if (hash != result) {
71*795d594fSAndroid Build Coastguard Worker           throw new Error("Query #" + i + " wrong. Expected " + hash + ", got " + result);
72*795d594fSAndroid Build Coastguard Worker         }
73*795d594fSAndroid Build Coastguard Worker       }
74*795d594fSAndroid Build Coastguard Worker       pool.shutdown();
75*795d594fSAndroid Build Coastguard Worker     } catch (CancellationException ex) {
76*795d594fSAndroid Build Coastguard Worker       System.out.println("Job timeout");
77*795d594fSAndroid Build Coastguard Worker       System.exit(1);
78*795d594fSAndroid Build Coastguard Worker     }
79*795d594fSAndroid Build Coastguard Worker   }
80*795d594fSAndroid Build Coastguard Worker }
81