Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestRunnableTask01.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2011 Gothel Software e.K.
5 * Copyright (c) 2011 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.util.parallel;
28
29import java.io.IOException;
30import java.lang.reflect.InvocationTargetException;
31
32import org.jau.lang.InterruptSource;
33import org.jau.util.parallel.RunnableTask;
34import org.junit.Assert;
35import org.junit.FixMethodOrder;
36import org.junit.Test;
37import org.junit.runners.MethodSorters;
38
39import jau.test.junit.util.JunitTracer;
40
41@FixMethodOrder(MethodSorters.NAME_ASCENDING)
42public class TestRunnableTask01 extends JunitTracer {
43
44 @Test
45 public void testInvokeAndWait00() throws IOException, InterruptedException, InvocationTargetException {
46 final Object syncObject = new Object();
47 final boolean[] done = {false};
48 final Runnable clientAction = new Runnable() {
49 @Override
50 public void run() {
51 synchronized(syncObject) {
52 System.err.println("CA.1: "+syncObject);
53 done[ 0 ] = true;
54 System.err.println("CA.X");
55 syncObject.notifyAll();
56 }
57 }
58 };
59
60 System.err.println("BB.0: "+syncObject);
61 synchronized (syncObject) {
62 System.err.println("BB.1: "+syncObject);
63 new InterruptSource.Thread(null, clientAction, Thread.currentThread().getName()+"-clientAction").start();
64 try {
65 System.err.println("BB.2");
66 syncObject.wait();
67 System.err.println("BB.3");
68 } catch (final InterruptedException e) {
69 throw new RuntimeException(e);
70 }
71 Assert.assertTrue(done[0]);
72 System.err.println("BB.X");
73 }
74 }
75
76 @Test
77 public void testInvokeAndWait01() throws IOException, InterruptedException, InvocationTargetException {
78 final boolean[] done = {false};
79 final Runnable clientAction = new Runnable() {
80 @Override
81 public void run() {
82 System.err.println("CA.1");
83 done[ 0 ] = true;
84 System.err.println("CA.X");
85 }
86 };
87
88 final RunnableTask rTask = new RunnableTask(clientAction, new Object(), false, null);
89 System.err.println("BB.0: "+rTask.getSyncObject());
90 synchronized (rTask.getSyncObject()) {
91 System.err.println("BB.1: "+rTask.getSyncObject());
92 new InterruptSource.Thread(null, rTask, Thread.currentThread().getName()+"-clientAction").start();
93 try {
94 System.err.println("BB.2");
95 rTask.getSyncObject().wait();
96 System.err.println("BB.3");
97 } catch (final InterruptedException e) {
98 throw new RuntimeException(e);
99 }
100 Assert.assertTrue(done[0]);
101 System.err.println("BB.X");
102 }
103 }
104
105 public static void main(final String args[]) throws IOException {
106 final String tstname = TestRunnableTask01.class.getName();
107 org.junit.runner.JUnitCore.main(tstname);
108 }
109
110}
static void main(final String args[])