2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.test.java.concurrent;
21 import gov.nasa.jpf.util.test.TestJPF;
22 import gov.nasa.jpf.vm.Verify;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.Exchanger;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
29 import org.junit.Test;
31 public class CountDownLatchTest extends TestJPF {
33 private static final int N = 2; // Too many states to test if set higher than 2. jpf-concurrent's gov.nasa.jpf.test.java.concurrent.CountDownLatchTest can handle more threads.
34 private static final int COUNTER_SUCCESS = 0;
35 private static final int COUNTER_EXCHANGED = 1;
38 //@Ignore("detects deadlock with exposure CG??")
40 public void testCountDown() throws InterruptedException {
41 if (verifyNoPropertyViolation("+vm.time.model=ConstantZero", "+vm.por.break_on_exposure=true")) {
43 final CountDownLatch latch = new CountDownLatch(N);
44 final Exchanger<Object> exchanger = new Exchanger<Object>();
45 final ExecutorService service = Executors.newFixedThreadPool(N);
47 Runnable task = new Runnable() {
51 Object source = new Object();
52 Object result = exchanger.exchange(source);
53 //Object result = exchanger.exchange(source, 1L, TimeUnit.SECONDS); // If N==2 and without jpf-concurrent, the timeout causes 149,808 states to be explored.
54 assert source != result : "source != result";
55 assert result != null : "result != null";
57 Verify.incrementCounter(COUNTER_EXCHANGED);
58 } catch (InterruptedException e) {
64 for (int i = 0; i < N; i++) {
65 service.execute(task);
71 Verify.incrementCounter(COUNTER_SUCCESS);
73 } else { // outside JPF
74 assert Verify.getCounter(COUNTER_SUCCESS) > 0 : "never succeeded";
75 assert Verify.getCounter(COUNTER_EXCHANGED) > 0 : "never exchanged";