1178476Sjb/*
2178476Sjb * CDDL HEADER START
3178476Sjb *
4178476Sjb * The contents of this file are subject to the terms of the
5178476Sjb * Common Development and Distribution License (the "License").
6178476Sjb * You may not use this file except in compliance with the License.
7178476Sjb *
8178476Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178476Sjb * or http://www.opensolaris.org/os/licensing.
10178476Sjb * See the License for the specific language governing permissions
11178476Sjb * and limitations under the License.
12178476Sjb *
13178476Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178476Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178476Sjb * If applicable, add the following below this CDDL HEADER, with the
16178476Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178476Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178476Sjb *
19178476Sjb * CDDL HEADER END
20178476Sjb */
21178476Sjb
22178476Sjb/*
23178476Sjb * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24178476Sjb * Use is subject to license terms.
25178476Sjb *
26178476Sjb * ident	"%Z%%M%	%I%	%E% SMI"
27178476Sjb */
28178476Sjb
29178476Sjbimport org.opensolaris.os.dtrace.*;
30178476Sjbimport java.util.NoSuchElementException;
31178476Sjb
32178476Sjb/**
33178476Sjb * Regression for 6426129 abort() after close() throws
34178476Sjb * NoSuchElementException.
35178476Sjb */
36178476Sjbpublic class TestAbort {
37178476Sjb    static boolean aborted = false;
38178476Sjb
39178476Sjb    public static void
40178476Sjb    main(String[] args)
41178476Sjb    {
42178476Sjb	Consumer consumer = new LocalConsumer();
43178476Sjb
44178476Sjb	// Test for deadlock (bug 6419880)
45178476Sjb	try {
46178476Sjb	    consumer.open();
47178476Sjb	    consumer.compile("syscall:::entry { @[execname] = count(); } " +
48178476Sjb		    "tick-101ms { printa(@); }");
49178476Sjb	    consumer.enable();
50178476Sjb	    consumer.go();
51178476Sjb	    try {
52178476Sjb		Thread.currentThread().sleep(1000);
53178476Sjb	    } catch (InterruptedException e) {
54178476Sjb		e.printStackTrace();
55178476Sjb		System.exit(1);
56178476Sjb	    }
57178476Sjb	    consumer.close();
58178476Sjb	} catch (DTraceException e) {
59178476Sjb	    e.printStackTrace();
60178476Sjb	    System.exit(1);
61178476Sjb	}
62178476Sjb
63178476Sjb	consumer = new LocalConsumer();
64178476Sjb
65178476Sjb	// Should be able to abort an unopened consumer
66178476Sjb	try {
67178476Sjb	    aborted = false;
68178476Sjb	    consumer.addConsumerListener(new ConsumerAdapter() {
69178476Sjb		public void consumerStopped(ConsumerEvent e) {
70178476Sjb		    aborted = true;
71178476Sjb		}
72178476Sjb	    });
73178476Sjb	    consumer.abort();
74178476Sjb	    consumer.open();
75178476Sjb	    consumer.compile("syscall:::entry { @[execname] = count(); } " +
76178476Sjb		    "tick-101ms { printa(@); }");
77178476Sjb	    consumer.enable();
78178476Sjb	    consumer.go();
79178476Sjb	    try {
80178476Sjb		Thread.currentThread().sleep(1000);
81178476Sjb	    } catch (InterruptedException e) {
82178476Sjb		e.printStackTrace();
83178476Sjb		System.exit(1);
84178476Sjb	    }
85178476Sjb	    if (!aborted) {
86178476Sjb		throw new IllegalStateException("consumer not aborted");
87178476Sjb	    }
88178476Sjb	    consumer.close();
89178476Sjb	} catch (Exception e) {
90178476Sjb	    e.printStackTrace();
91178476Sjb	    System.exit(1);
92178476Sjb	}
93178476Sjb
94178476Sjb	consumer = new LocalConsumer();
95178476Sjb
96178476Sjb	// Should be safe to call abort() in any state
97178476Sjb	try {
98178476Sjb	    consumer.abort();
99178476Sjb	    consumer.open();
100178476Sjb	    consumer.abort();
101178476Sjb	    consumer.compile("syscall:::entry { @[execname] = count(); } " +
102178476Sjb		    "tick-101ms { printa(@); }");
103178476Sjb	    consumer.abort();
104178476Sjb	    consumer.enable();
105178476Sjb	    consumer.abort();
106178476Sjb	    consumer.go();
107178476Sjb	    consumer.abort();
108178476Sjb	    consumer.close();
109178476Sjb	    // Should be safe to call after close()
110178476Sjb	    try {
111178476Sjb		consumer.abort();
112178476Sjb	    } catch (NoSuchElementException e) {
113178476Sjb		e.printStackTrace();
114178476Sjb		System.exit(1);
115178476Sjb	    }
116178476Sjb	} catch (Exception e) {
117178476Sjb	    e.printStackTrace();
118178476Sjb	    System.exit(1);
119178476Sjb	}
120178476Sjb
121178476Sjb	consumer = new LocalConsumer();
122178476Sjb
123178476Sjb	// Tests that close() throws expected exception when called on
124178476Sjb	// synchronized consumer.
125178476Sjb	try {
126178476Sjb	    consumer.open();
127178476Sjb	    consumer.compile("syscall:::entry { @[execname] = count(); } " +
128178476Sjb		    "tick-101ms { printa(@); }");
129178476Sjb	    consumer.enable();
130178476Sjb	    consumer.go();
131178476Sjb	    try {
132178476Sjb		Thread.currentThread().sleep(1000);
133178476Sjb	    } catch (InterruptedException e) {
134178476Sjb		e.printStackTrace();
135178476Sjb		System.exit(1);
136178476Sjb	    }
137178476Sjb	    try {
138178476Sjb		synchronized (consumer) {
139178476Sjb		    consumer.close();
140178476Sjb		}
141178476Sjb	    } catch (IllegalThreadStateException e) {
142178476Sjb		try {
143178476Sjb		    consumer.close();
144178476Sjb		    System.out.println("Successful");
145178476Sjb		    System.exit(0);
146178476Sjb		} catch (NoSuchElementException x) {
147178476Sjb		    x.printStackTrace();
148178476Sjb		    System.exit(1);
149178476Sjb		}
150178476Sjb	    }
151178476Sjb	} catch (DTraceException e) {
152178476Sjb	    e.printStackTrace();
153178476Sjb	    System.exit(1);
154178476Sjb	}
155178476Sjb	System.err.println("Failed");
156178476Sjb	System.exit(1);
157178476Sjb    }
158178476Sjb}
159