GlueGen: src/junit/com/jogamp/common/util/locks/TestSingletonServerSocket00.java Source File

GlueGen v2.6.0-rc-20250712 GlueGen, Native Binding Generator for Java™ (public API).

TestSingletonServerSocket00.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common.util.locks;
29
30import java.io.IOException;
31
32import org.junit.Assert;
33import org.junit.BeforeClass;
34import org.junit.Test;
35import org.junit.FixMethodOrder;
36import org.junit.runners.MethodSorters;
37
38import com.jogamp.common.util.InterruptSource;
39import com.jogamp.junit.util.JunitTracer;
40import com.jogamp.junit.util.SingletonJunitCase;
41
42@FixMethodOrder(MethodSorters.NAME_ASCENDING)
43public class TestSingletonServerSocket00 extends JunitTracer {
44 public static final long SINGLE_INSTANCE_LOCK_TO = SingletonJunitCase.SINGLE_INSTANCE_LOCK_TO;
45
46 public static final long SINGLE_INSTANCE_LOCK_POLL = 100; // poll every 100ms
47
48 private static volatile SingletonInstance singletonInstance;
49
50 @BeforeClass
51 public static void oneTimeSetUp() {
52 // one-time initialization code
53 singletonInstance = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL,
54 SingletonJunitCase.SINGLE_INSTANCE_LOCK_PORT);
55 }
56
57 @Test
58 public void test01_LockUnlock() {
59 Assert.assertTrue("Could not lock single instance: "+singletonInstance.getName(), singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO));
60 System.gc(); // force cleanup
61 singletonInstance.unlock();
62 }
63
64 @Test
65 public void test02_2ndInstanceLockTimeout() {
66 Assert.assertTrue("Could not lock single instance: "+singletonInstance.getName(), singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO));
67 final SingletonInstance instanceTwo = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL, SingletonJunitCase.SINGLE_INSTANCE_LOCK_PORT);
68 Assert.assertFalse("Could lock 2nd instance: "+instanceTwo.getName(), instanceTwo.tryLock(1000)); // 10x
69 System.gc(); // force cleanup
70 singletonInstance.unlock();
71 }
72
73 private Thread startLockUnlockOffThread(final int i) {
74 final Thread t = new InterruptSource.Thread(null, new Runnable() {
75 public void run() {
76 final SingletonInstance myLock = SingletonInstance.createServerSocket(10, SingletonJunitCase.SINGLE_INSTANCE_LOCK_PORT);
77 System.err.println(Thread.currentThread().getName()+" LOCK try ..");
78 Assert.assertTrue(Thread.currentThread().getName()+" - Could not lock instance: "+myLock.getName(), myLock.tryLock(1000));
79 System.err.println(Thread.currentThread().getName()+" LOCK ON");
80 try {
81 Thread.sleep(300);
82 } catch (final InterruptedException e) { }
83 myLock.unlock();
84 System.err.println(Thread.currentThread().getName()+" LOCK OFF");
85 }
86 }, "LockUnlock #"+i);
87 t.start();
88 return t;
89 }
90
91 @Test
92 public void testOffthreadLockUnlock() throws InterruptedException {
93 Assert.assertTrue("Could not lock single instance: "+singletonInstance.getName(), singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO));
94 final Thread t1 = startLockUnlockOffThread(1);
95 final Thread t2 = startLockUnlockOffThread(2);
96 Thread.sleep(300);
97 System.gc(); // force cleanup
98 singletonInstance.unlock();
99 while(t1.isAlive() || t2.isAlive()) {
100 Thread.sleep(100);
101 }
102 }
103
104 public static void main(final String args[]) throws IOException, InterruptedException {
105 final String tstname = TestSingletonServerSocket00.class.getName();
106 org.junit.runner.JUnitCore.main(tstname);
107 }
108}
com.jogamp.common.util.InterruptSource.Thread
java.lang.Thread specialization implementing InterruptSource to track java.lang.Thread#interrupt() ca...
Definition: InterruptSource.java:80
com.jogamp.common.util.locks.SingletonInstance
Definition: SingletonInstance.java:36
com.jogamp.common.util.locks.SingletonInstance.getName
abstract String getName()
com.jogamp.common.util.locks.SingletonInstance.tryLock
synchronized boolean tryLock(long maxwait)
Blocking until the lock is acquired by this Thread or maxwait in ms is reached.
Definition: SingletonInstance.java:88
com.jogamp.common.util.locks.SingletonInstance.createServerSocket
static SingletonInstance createServerSocket(final long poll_ms, final int portNumber)
A user shall use ephemeral ports:
Definition: SingletonInstance.java:61
com.jogamp.common.util.locks.SingletonInstance.unlock
void unlock()
Release the lock.
Definition: SingletonInstance.java:125
com.jogamp.common.util.locks.TestSingletonServerSocket00
Definition: TestSingletonServerSocket00.java:43
com.jogamp.common.util.locks.TestSingletonServerSocket00.test02_2ndInstanceLockTimeout
void test02_2ndInstanceLockTimeout()
Definition: TestSingletonServerSocket00.java:65
com.jogamp.common.util.locks.TestSingletonServerSocket00.main
static void main(final String args[])
Definition: TestSingletonServerSocket00.java:104
com.jogamp.common.util.locks.TestSingletonServerSocket00.oneTimeSetUp
static void oneTimeSetUp()
Definition: TestSingletonServerSocket00.java:51
com.jogamp.common.util.locks.TestSingletonServerSocket00.test01_LockUnlock
void test01_LockUnlock()
Definition: TestSingletonServerSocket00.java:58
com.jogamp.common.util.locks.TestSingletonServerSocket00.testOffthreadLockUnlock
void testOffthreadLockUnlock()
Definition: TestSingletonServerSocket00.java:92
com.jogamp.junit.util.JunitTracer
Definition: JunitTracer.java:47
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39
com.jogamp.junit.util.SingletonJunitCase.SINGLE_INSTANCE_LOCK_PORT
static final int SINGLE_INSTANCE_LOCK_PORT
Definition: SingletonJunitCase.java:41
com.jogamp.junit.util.SingletonJunitCase.SINGLE_INSTANCE_LOCK_TO
static final long SINGLE_INSTANCE_LOCK_TO
Definition: SingletonJunitCase.java:43
com.jogamp.common.util.InterruptSource
Interface exposing java.lang.Thread#interrupt() source, intended for java.lang.Thread specializations...
Definition: InterruptSource.java:36