4
* Copyright Ericsson AB 2004-2010. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
7
* Version 1.1, (the "License"); you may not use this file except in
8
* compliance with the License. You should have received a copy of the
9
* Erlang Public License along with this software. If not, it can be
10
* retrieved online at http://www.erlang.org/.
12
* Software distributed under the License is distributed on an "AS IS"
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
* the License for the specific language governing rights and limitations
20
import com.ericsson.otp.erlang.*;
22
class MboxLinkUnlink {
25
Implements test case jinterface_SUITE:mbox_link_unlink/1
31
private static final int java_link_and_exit = 1;
32
private static final int erl_link_and_exit = 2;
33
private static final int erl_link_java_exit = 3;
34
private static final int java_link_erl_exit = 4;
35
private static final int internal_link_linking_exits = 5;
36
private static final int internal_link_linked_exits = 6;
37
private static final int internal_unlink_linking_exits = 7;
38
private static final int internal_unlink_linked_exits = 8;
39
private static final int normal_exit = 9;
40
private static final int kill_mbox = 10;
41
private static final int kill_erl_proc_from_java = 11;
42
private static final int kill_mbox_from_erlang = 12;
43
private static final int erl_exit_with_reason_any_term = 13;
44
private static final int java_exit_with_reason_any_term = 14;
46
private static boolean dbg = true;
49
public static void main(String argv[]) {
52
//String cookie = argv[0];
53
String erlNode = argv[1];
54
OtpErlangObject expected = null;
55
boolean waiting = false;
58
OtpNode node = new OtpNode("javanode");
59
OtpMbox mainMbox = node.createMbox();
62
// Initiate and set up connection to erlang process
63
OtpMbox mbox = node.createMbox();
66
OtpErlangObject[] msg = {mainMbox.self(),mbox.self()};
67
mbox.send("erl_link_server", erlNode, new OtpErlangTuple(msg));
68
OtpErlangObject o = mbox.receive(1000);
69
if (o == null) System.exit(1);
70
OtpErlangTuple tuple = (OtpErlangTuple)o;
71
int tag = (int)((OtpErlangLong)tuple.elementAt(0)).longValue();
75
case java_exit_with_reason_any_term:
76
case java_link_and_exit:
77
dbg("Java got \"java_link_and_exit\" or " +
78
"\"java_exit_with_reason_any_term\"");
79
mbox.link((OtpErlangPid)tuple.elementAt(1));
80
mbox.send((OtpErlangPid)tuple.elementAt(1),
81
new OtpErlangAtom("ok"));
82
mbox.exit(tuple.elementAt(2));
84
case erl_exit_with_reason_any_term:
85
case erl_link_and_exit:
86
dbg("Java got \"erl_link_and_exit\" or " +
87
"\"erl_exit_with_reason_any_term\"");
88
mbox.send((OtpErlangPid)tuple.elementAt(1),
89
new OtpErlangAtom("ok"));
91
expected = tuple.elementAt(2);
94
case erl_link_java_exit:
95
dbg("Java got \"erl_link_java_exit\"");
96
mbox.exit(tuple.elementAt(2));
98
case java_link_erl_exit:
99
dbg("Java got \"java_link_erl_exit\"");
100
mbox.link((OtpErlangPid)tuple.elementAt(1));
101
mbox.send((OtpErlangPid)tuple.elementAt(1),
102
new OtpErlangAtom("ok"));
104
expected = tuple.elementAt(2);
107
case internal_link_linking_exits:
108
dbg("Java got \"internal_link_linking_exits\"");
109
mbox2 = node.createMbox();
110
mbox.link(mbox2.self());
111
mbox.exit(tuple.elementAt(2));
113
expected = tuple.elementAt(2);
114
mbox2.receive(1000); // hanging waiting for exit
115
System.exit(4); // got someting other than exit
116
case internal_link_linked_exits:
117
dbg("Java got \"internal_link_linked_exits\"");
118
mbox2 = node.createMbox();
119
mbox.link(mbox2.self());
120
mbox2.exit(tuple.elementAt(2));
122
expected = tuple.elementAt(2);
123
mbox.receive(1000); // hanging waiting for exit
124
System.exit(5); // got someting other than exit
125
case internal_unlink_linking_exits:
126
dbg("Java got \"internal_unlink_linking_exits\"");
127
mbox2 = node.createMbox();
128
mbox.link(mbox2.self());
129
mbox.unlink(mbox2.self());
130
mbox.link(mbox2.self());
131
mbox2.unlink(mbox.self());
132
mbox2.exit(tuple.elementAt(2));
133
if (mbox.receive(500)!=null) System.exit(6);
135
case internal_unlink_linked_exits:
136
dbg("Java got \"internal_unlink_linked_exits\"");
137
mbox2 = node.createMbox();
138
mbox.link(mbox2.self());
139
mbox.unlink(mbox2.self());
140
mbox.link(mbox2.self());
141
mbox2.unlink(mbox.self());
142
mbox.exit(tuple.elementAt(2));
143
if (mbox2.receive(500)!=null) System.exit(7);
146
dbg("Java got \"normal_exit\"");
150
dbg("Java got \"kill_mbox\"");
153
case kill_erl_proc_from_java:
154
dbg("Java got \"kill_erl_proc_from_java\"");
155
mbox.exit((OtpErlangPid)tuple.elementAt(1),"kill");
157
case kill_mbox_from_erlang:
158
dbg("Java got \"kill_mbox_from_erlang\"");
159
/* This will make the testcase successful, but it is
160
not the correct way to do it...
161
Mbox should probably just die when the kill signal is
162
received from erlang (or other mbox).
168
catch (OtpErlangExit exit) {
169
if(!(exit.reason().equals(new OtpErlangAtom("kill"))))
177
catch (OtpErlangExit exit) {
178
dbg("Java got exit: " + exit.reason());
179
if(!(waiting && exit.reason().equals(expected)))
183
OtpErlangAtom done = new OtpErlangAtom("done");
184
mainMbox.send("erl_link_server", erlNode, done);
185
OtpErlangObject o = mainMbox.receive(1000);
186
if (o == null) System.exit(11);
187
else if (!((OtpErlangAtom)o).equals(done))
191
catch (Exception e) {
192
System.out.println("EXCEPTION: " + e);
197
private static void dbg(String str) {
198
if (dbg) System.out.println(str);