1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 *
26 * ident	"%Z%%M%	%I%	%E% SMI"
27 */
28
29import org.opensolaris.os.dtrace.*;
30import java.util.NoSuchElementException;
31
32/**
33 * Regression for 6426129 abort() after close() throws
34 * NoSuchElementException.
35 */
36public class TestAbort {
37    static boolean aborted = false;
38
39    public static void
40    main(String[] args)
41    {
42	Consumer consumer = new LocalConsumer();
43
44	// Test for deadlock (bug 6419880)
45	try {
46	    consumer.open();
47	    consumer.compile("syscall:::entry { @[execname] = count(); } " +
48		    "tick-101ms { printa(@); }");
49	    consumer.enable();
50	    consumer.go();
51	    try {
52		Thread.currentThread().sleep(1000);
53	    } catch (InterruptedException e) {
54		e.printStackTrace();
55		System.exit(1);
56	    }
57	    consumer.close();
58	} catch (DTraceException e) {
59	    e.printStackTrace();
60	    System.exit(1);
61	}
62
63	consumer = new LocalConsumer();
64
65	// Should be able to abort an unopened consumer
66	try {
67	    aborted = false;
68	    consumer.addConsumerListener(new ConsumerAdapter() {
69		public void consumerStopped(ConsumerEvent e) {
70		    aborted = true;
71		}
72	    });
73	    consumer.abort();
74	    consumer.open();
75	    consumer.compile("syscall:::entry { @[execname] = count(); } " +
76		    "tick-101ms { printa(@); }");
77	    consumer.enable();
78	    consumer.go();
79	    try {
80		Thread.currentThread().sleep(1000);
81	    } catch (InterruptedException e) {
82		e.printStackTrace();
83		System.exit(1);
84	    }
85	    if (!aborted) {
86		throw new IllegalStateException("consumer not aborted");
87	    }
88	    consumer.close();
89	} catch (Exception e) {
90	    e.printStackTrace();
91	    System.exit(1);
92	}
93
94	consumer = new LocalConsumer();
95
96	// Should be safe to call abort() in any state
97	try {
98	    consumer.abort();
99	    consumer.open();
100	    consumer.abort();
101	    consumer.compile("syscall:::entry { @[execname] = count(); } " +
102		    "tick-101ms { printa(@); }");
103	    consumer.abort();
104	    consumer.enable();
105	    consumer.abort();
106	    consumer.go();
107	    consumer.abort();
108	    consumer.close();
109	    // Should be safe to call after close()
110	    try {
111		consumer.abort();
112	    } catch (NoSuchElementException e) {
113		e.printStackTrace();
114		System.exit(1);
115	    }
116	} catch (Exception e) {
117	    e.printStackTrace();
118	    System.exit(1);
119	}
120
121	consumer = new LocalConsumer();
122
123	// Tests that close() throws expected exception when called on
124	// synchronized consumer.
125	try {
126	    consumer.open();
127	    consumer.compile("syscall:::entry { @[execname] = count(); } " +
128		    "tick-101ms { printa(@); }");
129	    consumer.enable();
130	    consumer.go();
131	    try {
132		Thread.currentThread().sleep(1000);
133	    } catch (InterruptedException e) {
134		e.printStackTrace();
135		System.exit(1);
136	    }
137	    try {
138		synchronized (consumer) {
139		    consumer.close();
140		}
141	    } catch (IllegalThreadStateException e) {
142		try {
143		    consumer.close();
144		    System.out.println("Successful");
145		    System.exit(0);
146		} catch (NoSuchElementException x) {
147		    x.printStackTrace();
148		    System.exit(1);
149		}
150	    }
151	} catch (DTraceException e) {
152	    e.printStackTrace();
153	    System.exit(1);
154	}
155	System.err.println("Failed");
156	System.exit(1);
157    }
158}
159