Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
SingletonJunitCase.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.junit.util;
28
29import org.junit.BeforeClass;
30import org.junit.AfterClass;
31import org.junit.FixMethodOrder;
32import org.junit.runners.MethodSorters;
33
34import jau.test.util.parallel.locks.SingletonInstance;
35
36@FixMethodOrder(MethodSorters.NAME_ASCENDING)
37public abstract class SingletonJunitCase extends JunitTracer {
38 public static final String SINGLE_INSTANCE_LOCK_FILE = "SingletonTestCase.lock";
39 public static final int SINGLE_INSTANCE_LOCK_PORT = 59999;
40
41 public static final long SINGLE_INSTANCE_LOCK_TO = 15*60*1000; // wait up to 15 mins
42 public static final long SINGLE_INSTANCE_LOCK_POLL = 500; // poll every 500 ms
43
44 private static SingletonInstance singletonInstance = null; // system wide lock via port locking
45 private static final Object singletonSync = new Object(); // classloader wide lock
46 private static boolean enabled = true;
47
48 /**
49 * Default is {@code true}.
50 */
51 public static final void enableSingletonLock(final boolean v) {
52 enabled = v;
53 }
54
55 @BeforeClass
56 public static final void oneTimeSetUpSingleton() {
57 // one-time initialization code
58 synchronized( singletonSync ) {
59 if( enabled ) {
60 if( null == singletonInstance ) {
61 System.err.println("++++ Test Singleton.ctor()");
62 // singletonInstance = SingletonInstance.createFileLock(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_FILE);
63 singletonInstance = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_PORT);
64 }
65 System.err.println("++++ Test Singleton.lock()");
66 if(!singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO)) {
67 throw new RuntimeException("Fatal: Could not lock single instance: "+singletonInstance.getName());
68 }
69 }
70 }
71 }
72
73 @AfterClass
74 public static final void oneTimeTearDownSingleton() {
75 // one-time cleanup code
76 synchronized( singletonSync ) {
77 System.gc(); // force cleanup
78 if( enabled ) {
79 System.err.println("++++ Test Singleton.unlock()");
80 singletonInstance.unlock();
81 try {
82 // allowing other JVM instances to pick-up socket
83 Thread.sleep( SINGLE_INSTANCE_LOCK_POLL );
84 } catch (final InterruptedException e) { }
85 }
86 }
87 }
88}
89
static final void enableSingletonLock(final boolean v)
Default is true.
static SingletonInstance createServerSocket(final long poll_ms, final int portNumber)
A user shall use ephemeral ports:
synchronized boolean tryLock(long maxwait)
Blocking until the lock is acquired by this Thread or maxwait in ms is reached.