comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/interop/ForeignAccessSingleThreadedTest.java @ 21969:9124ca6c46b0

ForeignAccess is singlethreaded. Accessing objects from other languages (via ForeignAccess) will only be possible from a previously selected thread.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Tue, 23 Jun 2015 10:23:10 +0200
parents truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/TruffleVMSingleThreadedTest.java@9c8c0937da41
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21968:112aba5e3e12 21969:9124ca6c46b0
1 /*
2 * Copyright (c) 2012, 2015, 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.interop;
24
25 import com.oracle.truffle.api.CallTarget;
26 import com.oracle.truffle.api.interop.ForeignAccess;
27 import com.oracle.truffle.api.interop.Message;
28 import com.oracle.truffle.api.interop.TruffleObject;
29 import com.oracle.truffle.api.nodes.Node;
30
31 import org.junit.*;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.fail;
35
36 public class ForeignAccessSingleThreadedTest implements ForeignAccess.Factory, TruffleObject {
37 ForeignAccess fa;
38 private int cnt;
39
40 @Before
41 public void initInDifferentThread() throws InterruptedException {
42 Thread t = new Thread("Initializer") {
43 @Override
44 public void run() {
45 fa = ForeignAccess.create(ForeignAccessSingleThreadedTest.this);
46 }
47 };
48 t.start();
49 t.join();
50 }
51
52 @Test(expected = IllegalStateException.class)
53 public void accessNodeFromWrongThread() {
54 Node n = Message.IS_EXECUTABLE.createNode();
55 Object ret = ForeignAccess.execute(n, null, this);
56 fail("Should throw an exception: " + ret);
57 }
58
59 @After
60 public void noCallsToFactory() {
61 assertEquals("No calls to accessMessage or canHandle", 0, cnt);
62 }
63
64 @Override
65 public boolean canHandle(TruffleObject obj) {
66 cnt++;
67 return true;
68 }
69
70 @Override
71 public CallTarget accessMessage(Message tree) {
72 cnt++;
73 return null;
74 }
75
76 @Override
77 public ForeignAccess getForeignAccess() {
78 return fa;
79 }
80 }