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 2007 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.*;
30178476Sjb
31178476Sjb/**
32178476Sjb * Regression for 6506495 -DJAVA_DTRACE_MAX_CONSUMERS=N for any N < 8
33178476Sjb * is treated as if it were 8.
34178476Sjb */
35178476Sjbpublic class TestMaxConsumers {
36178476Sjb    static final String MAX_CONSUMERS_PROPERTY_NAME =
37178476Sjb	    "JAVA_DTRACE_MAX_CONSUMERS";
38178476Sjb
39178476Sjb    static Integer
40178476Sjb    getIntegerProperty(String name)
41178476Sjb    {
42178476Sjb	Integer value = null;
43178476Sjb	String property = System.getProperty(name);
44178476Sjb	if (property != null && property.length() != 0) {
45178476Sjb	    try {
46178476Sjb		value = Integer.parseInt(property);
47178476Sjb	    } catch (NumberFormatException e) {
48178476Sjb		e.printStackTrace();
49178476Sjb	    }
50178476Sjb	}
51178476Sjb	return value;
52178476Sjb    }
53178476Sjb
54178476Sjb    public static void
55178476Sjb    main(String[] args)
56178476Sjb    {
57178476Sjb	Integer property = getIntegerProperty(MAX_CONSUMERS_PROPERTY_NAME);
58178476Sjb	int max = (property == null ? 0 : property);
59178476Sjb	int n = (property == null ? 11 : (max < 1 ? 1 : max));
60178476Sjb
61178476Sjb	Consumer[] consumers = new Consumer[n];
62178476Sjb	try {
63178476Sjb	    for (int i = 0; i < n; ++i) {
64178476Sjb		consumers[i] = new LocalConsumer();
65178476Sjb		consumers[i].open();
66178476Sjb	    }
67178476Sjb	    for (int i = 0; i < n; ++i) {
68178476Sjb		consumers[i].close();
69178476Sjb	    }
70178476Sjb	    for (int i = 0; i < n; ++i) {
71178476Sjb		consumers[i] = new LocalConsumer();
72178476Sjb		consumers[i].open();
73178476Sjb	    }
74178476Sjb	} catch (Exception e) {
75178476Sjb	    e.printStackTrace();
76178476Sjb	    System.exit(1);
77178476Sjb	}
78178476Sjb
79178476Sjb	try {
80178476Sjb	    Consumer consumer = new LocalConsumer();
81178476Sjb	    consumer.open();
82178476Sjb	    if (max > 0) {
83178476Sjb		System.out.println("Error: " + (max + 1) + " > " +
84178476Sjb			MAX_CONSUMERS_PROPERTY_NAME);
85178476Sjb	    } else {
86178476Sjb		System.out.println("Success");
87178476Sjb	    }
88178476Sjb	    consumer.close();
89178476Sjb	} catch (Exception e) {
90178476Sjb	    System.out.println("Success");
91178476Sjb	} finally {
92178476Sjb	    for (int i = 0; i < n; ++i) {
93178476Sjb		consumers[i].close();
94178476Sjb	    }
95178476Sjb	}
96178476Sjb    }
97178476Sjb}
98