Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/InstrumentationTest.java @ 16775:8c606e8053b8
Truffle/API test: additional documentation on the new instrumentation tests; move into project with other tests and add an entry in package-info.java
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Mon, 11 Aug 2014 10:35:38 -0700 |
parents | graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/WrapperTest.java@2a5ec181dad4 |
children | 7833417c8172 |
comparison
equal
deleted
inserted
replaced
16727:9d55732d0880 | 16775:8c606e8053b8 |
---|---|
1 /* | |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.api.test; | |
24 | |
25 import java.util.*; | |
26 | |
27 import org.junit.*; | |
28 | |
29 import com.oracle.truffle.api.*; | |
30 import com.oracle.truffle.api.frame.*; | |
31 import com.oracle.truffle.api.instrument.*; | |
32 import com.oracle.truffle.api.nodes.*; | |
33 import com.oracle.truffle.api.source.*; | |
34 | |
35 /** | |
36 * <h3>AST Instrumentation</h3> | |
37 * | |
38 * Instrumentation allows the insertion into Truffle ASTs language-specific instances of | |
39 * {@link Wrapper} that propagate {@link ExecutionEvents} through a {@link Probe} to any instances | |
40 * of {@link Instrument} that might be attached to the particular probe by tools. | |
41 * <ol> | |
42 * <li>Creates a simple add AST</li> | |
43 * <li>Verifies its structure</li> | |
44 * <li>"Probes" the add node by adding a {@link Wrapper} and associated {@link Probe}</li> | |
45 * <li>Attaches a simple {@link Instrument} to the node via its {@link Probe}</li> | |
46 * <li>Verifies the structure of the probed AST</li> | |
47 * <li>Verifies the execution of the probed AST</li> | |
48 * <li>Verifies the results observed by the instrument.</li> | |
49 * </ol> | |
50 * To do these tests, several required classes have been implemented in their most basic form, only | |
51 * implementing the methods necessary for the tests to pass, with stubs elsewhere. | |
52 */ | |
53 public class InstrumentationTest { | |
54 | |
55 @Test | |
56 public void test() { | |
57 // Build a tree | |
58 TruffleRuntime runtime = Truffle.getRuntime(); | |
59 TestChildNode leftChild = new TestChildNode(); | |
60 TestChildNode rightChild = new TestChildNode(); | |
61 TestSourceSection sourceSection = new TestSourceSection(); | |
62 TestAddNode addNode = new TestAddNode(leftChild, rightChild, sourceSection); | |
63 TestRootNode rootNode = new TestRootNode(addNode); | |
64 | |
65 // Have to create a call target before checking parent/child relationships | |
66 CallTarget target = runtime.createCallTarget(rootNode); | |
67 | |
68 // Check tree structure | |
69 Assert.assertEquals(addNode, leftChild.getParent()); | |
70 Assert.assertEquals(addNode, rightChild.getParent()); | |
71 Iterator<Node> iterator = addNode.getChildren().iterator(); | |
72 Assert.assertEquals(leftChild, iterator.next()); | |
73 Assert.assertEquals(rightChild, iterator.next()); | |
74 Assert.assertFalse(iterator.hasNext()); | |
75 Assert.assertEquals(rootNode, addNode.getParent()); | |
76 iterator = rootNode.getChildren().iterator(); | |
77 Assert.assertEquals(addNode, iterator.next()); | |
78 Assert.assertFalse(iterator.hasNext()); | |
79 Object result = target.call(); | |
80 Assert.assertEquals(42, result); | |
81 | |
82 // Create another call target, this time with the "probed" add node | |
83 TestExecutionContext context = new TestExecutionContext(); | |
84 TestWrapper wrapper = new TestWrapper(addNode, context); | |
85 rootNode = new TestRootNode(wrapper); | |
86 target = runtime.createCallTarget(rootNode); | |
87 | |
88 // Check the new tree structure | |
89 Assert.assertEquals(addNode, leftChild.getParent()); | |
90 Assert.assertEquals(addNode, rightChild.getParent()); | |
91 iterator = addNode.getChildren().iterator(); | |
92 Assert.assertEquals(leftChild, iterator.next()); | |
93 Assert.assertEquals(rightChild, iterator.next()); | |
94 Assert.assertFalse(iterator.hasNext()); | |
95 Assert.assertEquals(wrapper, addNode.getParent()); | |
96 iterator = wrapper.getChildren().iterator(); | |
97 Assert.assertEquals(addNode, iterator.next()); | |
98 Assert.assertFalse(iterator.hasNext()); | |
99 Assert.assertEquals(rootNode, wrapper.getParent()); | |
100 iterator = rootNode.getChildren().iterator(); | |
101 Assert.assertEquals(wrapper, iterator.next()); | |
102 Assert.assertFalse(iterator.hasNext()); | |
103 result = target.call(); | |
104 Assert.assertEquals(42, result); | |
105 | |
106 // Create some instruments | |
107 final TestInstrument instrumentA = new TestInstrument(); | |
108 final TestInstrument instrumentB = new TestInstrument(); | |
109 | |
110 wrapper.getProbe().addInstrument(instrumentA); | |
111 | |
112 result = target.call(); | |
113 Assert.assertEquals(instrumentA.numInstrumentEnter, 1); | |
114 Assert.assertEquals(instrumentA.numInstrumentLeave, 1); | |
115 Assert.assertEquals(instrumentB.numInstrumentEnter, 0); | |
116 Assert.assertEquals(instrumentB.numInstrumentLeave, 0); | |
117 Assert.assertEquals(42, result); | |
118 | |
119 wrapper.getProbe().addInstrument(instrumentB); | |
120 | |
121 result = target.call(); | |
122 Assert.assertEquals(instrumentA.numInstrumentEnter, 2); | |
123 Assert.assertEquals(instrumentA.numInstrumentLeave, 2); | |
124 Assert.assertEquals(instrumentB.numInstrumentEnter, 1); | |
125 Assert.assertEquals(instrumentB.numInstrumentLeave, 1); | |
126 Assert.assertEquals(42, result); | |
127 | |
128 wrapper.getProbe().removeInstrument(instrumentA); | |
129 | |
130 result = target.call(); | |
131 Assert.assertEquals(instrumentA.numInstrumentEnter, 2); | |
132 Assert.assertEquals(instrumentA.numInstrumentLeave, 2); | |
133 Assert.assertEquals(instrumentB.numInstrumentEnter, 2); | |
134 Assert.assertEquals(instrumentB.numInstrumentLeave, 2); | |
135 Assert.assertEquals(42, result); | |
136 | |
137 } | |
138 | |
139 private class TestRootNode extends RootNode { | |
140 @Child private RootNode child; | |
141 | |
142 public TestRootNode(RootNode child) { | |
143 super(null); | |
144 this.child = child; | |
145 } | |
146 | |
147 @Override | |
148 public Object execute(VirtualFrame frame) { | |
149 return child.execute(frame); | |
150 } | |
151 } | |
152 | |
153 private class TestAddNode extends RootNode { | |
154 | |
155 @Child private TestChildNode left; | |
156 @Child private TestChildNode right; | |
157 | |
158 public TestAddNode(TestChildNode left, TestChildNode right, TestSourceSection sourceSection) { | |
159 super(sourceSection); | |
160 this.left = left; | |
161 this.right = right; | |
162 } | |
163 | |
164 @Override | |
165 public Object execute(VirtualFrame frame) { | |
166 return left.execute() + right.execute(); | |
167 } | |
168 } | |
169 | |
170 private class TestChildNode extends Node { | |
171 | |
172 public TestChildNode() { | |
173 super(null); | |
174 } | |
175 | |
176 public int execute() { | |
177 return 21; | |
178 } | |
179 } | |
180 | |
181 /** | |
182 * The wrapper node class is usually language-specific and inherits from the language-specific | |
183 * subclass of {@link Node}, not {@RootNode}. | |
184 */ | |
185 private class TestWrapper extends RootNode implements Wrapper { | |
186 @Child private RootNode child; | |
187 private Probe probe; | |
188 | |
189 public TestWrapper(RootNode child, ExecutionContext context) { | |
190 this.child = insert(child); | |
191 this.probe = context.getProbe(child.getSourceSection()); | |
192 } | |
193 | |
194 public boolean isTaggedAs(SyntaxTag tag) { | |
195 return false; | |
196 } | |
197 | |
198 public Iterable<SyntaxTag> getSyntaxTags() { | |
199 return null; | |
200 } | |
201 | |
202 public Node getChild() { | |
203 return child; | |
204 } | |
205 | |
206 public Probe getProbe() { | |
207 return probe; | |
208 } | |
209 | |
210 @Override | |
211 public Object execute(VirtualFrame frame) { | |
212 probe.enter(child, frame); | |
213 Object result; | |
214 | |
215 try { | |
216 result = child.execute(frame); | |
217 probe.leave(child, frame, result); | |
218 } catch (Exception e) { | |
219 probe.leaveExceptional(child, frame, e); | |
220 throw (e); | |
221 } | |
222 return result; | |
223 } | |
224 } | |
225 | |
226 /** | |
227 * An "empty" description of the source code that might correspond to a particular AST node. The | |
228 * instrumentation framework tracks probes that have been inserted by their source location, | |
229 * using this as a key. | |
230 */ | |
231 private class TestSourceSection implements SourceSection { | |
232 | |
233 public Source getSource() { | |
234 return null; | |
235 } | |
236 | |
237 public int getStartLine() { | |
238 return 0; | |
239 } | |
240 | |
241 public LineLocation getLineLocation() { | |
242 return null; | |
243 } | |
244 | |
245 public int getStartColumn() { | |
246 return 0; | |
247 } | |
248 | |
249 public int getCharIndex() { | |
250 return 0; | |
251 } | |
252 | |
253 public int getCharLength() { | |
254 return 0; | |
255 } | |
256 | |
257 public int getCharEndIndex() { | |
258 return 0; | |
259 } | |
260 | |
261 public String getIdentifier() { | |
262 return null; | |
263 } | |
264 | |
265 public String getCode() { | |
266 return null; | |
267 } | |
268 | |
269 public String getShortDescription() { | |
270 return null; | |
271 } | |
272 | |
273 } | |
274 | |
275 private class TestExecutionContext extends ExecutionContext { | |
276 | |
277 @Override | |
278 public String getLanguageShortName() { | |
279 return "test"; | |
280 } | |
281 | |
282 @Override | |
283 protected void setSourceCallback(SourceCallback sourceCallback) { | |
284 } | |
285 | |
286 } | |
287 | |
288 private class TestInstrument extends Instrument { | |
289 | |
290 public int numInstrumentEnter = 0; | |
291 public int numInstrumentLeave = 0; | |
292 | |
293 @Override | |
294 public void enter(Node astNode, VirtualFrame frame) { | |
295 numInstrumentEnter++; | |
296 } | |
297 | |
298 @Override | |
299 public void leave(Node astNode, VirtualFrame frame, Object result) { | |
300 numInstrumentLeave++; | |
301 } | |
302 } | |
303 | |
304 } |