Attempts to fix "Already bound" exceptions in the unit tests.
This commit is contained in:
parent
ef562bd688
commit
3d5ead7341
@ -129,7 +129,7 @@
|
||||
|
||||
<!-- Run the tests-->
|
||||
<target name="test">
|
||||
<junit haltonfailure="true">
|
||||
<junit haltonfailure="true" fork="true" forkmode="once">
|
||||
<formatter type="xml"/>
|
||||
<formatter type="brief" usefile="false"/>
|
||||
<test name="org.ice4j.StunTestSuite" todir="${junit.reports}"/>
|
||||
@ -152,6 +152,6 @@
|
||||
|
||||
|
||||
<!-- Cruise Control Target-->
|
||||
<target name="cc-buildloop" depends="rebuild"/>
|
||||
<target name="cc-buildloop" depends="rebuild,test"/>
|
||||
|
||||
</project>
|
||||
|
||||
126
src/org/ice4j/socket/SafeCloseDatagramSocket.java
Normal file
126
src/org/ice4j/socket/SafeCloseDatagramSocket.java
Normal file
@ -0,0 +1,126 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
* Represents a <tt>DatagramSocket</tt> which fixes the following problem: when
|
||||
* {@link DatagramSocket#close()} is called while another thread is blocked in
|
||||
* its {@link DatagramSocket#receive(DatagramPacket)}, calling
|
||||
* {@link DatagramSocket#bind(SocketAddress) throws an "Already bound" exception
|
||||
* until (the native counterpart of) the <tt>receive</tt> method returns.
|
||||
*
|
||||
* @author Lubomir Marinov
|
||||
*/
|
||||
public class SafeCloseDatagramSocket
|
||||
extends DatagramSocket
|
||||
{
|
||||
|
||||
/**
|
||||
* The number of {@link #receive(DatagramPacket)} calls that have to return
|
||||
* before {@link #close()} returns.
|
||||
*/
|
||||
private int inReceive = 0;
|
||||
|
||||
/**
|
||||
* The <tt>Object</tt> which synchronizes the access to {@link #inReceive}
|
||||
* and implements the related inter-thread communication.
|
||||
*/
|
||||
private final Object inReceiveSyncRoot = new Object();
|
||||
|
||||
/**
|
||||
* Creates a datagram socket, bound to the specified local socket address.
|
||||
* <p>
|
||||
* If, if the address is <tt>null</tt>, creates an unbound socket.
|
||||
* </p>
|
||||
*
|
||||
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
|
||||
* unbound socket
|
||||
* @throws SocketException if the socket could not be opened, or the socket
|
||||
* could not bind to the specified local port
|
||||
*/
|
||||
public SafeCloseDatagramSocket(SocketAddress bindaddr)
|
||||
throws SocketException
|
||||
{
|
||||
super(bindaddr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes this datagram socket.
|
||||
* <p>
|
||||
* Any thread currently blocked in {@link #receive} upon this socket will
|
||||
* throw a {@link SocketException} and this datagram socket will wait for it
|
||||
* to return.
|
||||
* </p>
|
||||
*
|
||||
* @see DatagramSocket#close()
|
||||
*/
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
super.close();
|
||||
|
||||
synchronized (inReceiveSyncRoot)
|
||||
{
|
||||
boolean interrupted = false;
|
||||
|
||||
while (inReceive > 0)
|
||||
try
|
||||
{
|
||||
inReceiveSyncRoot.wait();
|
||||
}
|
||||
catch (InterruptedException iex)
|
||||
{
|
||||
interrupted = true;
|
||||
}
|
||||
if (interrupted)
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives a datagram packet from this socket. When this method returns,
|
||||
* the <tt>DatagramPacket</tt>'s buffer is filled with the data received.
|
||||
* The datagram packet also contains the sender's IP address, and the port
|
||||
* number on the sender's machine.
|
||||
* <p>
|
||||
* This method blocks until a datagram is received. The <tt>length</tt>
|
||||
* field of the datagram packet object contains the length of the received
|
||||
* message. If the message is longer than the packet's length, the message
|
||||
* is truncated.
|
||||
* </p>
|
||||
*
|
||||
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
|
||||
* data
|
||||
* @throws IOException if an I/O error occurs
|
||||
* @see DatagramSocket#receive(DatagramPacket)
|
||||
*/
|
||||
@Override
|
||||
public void receive(DatagramPacket p)
|
||||
throws IOException
|
||||
{
|
||||
synchronized (inReceiveSyncRoot)
|
||||
{
|
||||
inReceive++;
|
||||
}
|
||||
try
|
||||
{
|
||||
super.receive(p);
|
||||
}
|
||||
finally
|
||||
{
|
||||
synchronized (inReceiveSyncRoot)
|
||||
{
|
||||
inReceive--;
|
||||
inReceiveSyncRoot.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ import java.util.logging.*;
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.attribute.*;
|
||||
import org.ice4j.message.*;
|
||||
import org.ice4j.socket.*;
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
/**
|
||||
@ -165,7 +166,7 @@ public class NetworkConfigurationDiscoveryProcess
|
||||
{
|
||||
stunStack = StunStack.getInstance();
|
||||
|
||||
sock = new DatagramSocket(localAddress);
|
||||
sock = new SafeCloseDatagramSocket(localAddress);
|
||||
|
||||
stunStack.addSocket(sock);
|
||||
|
||||
|
||||
@ -8,11 +8,11 @@ package org.ice4j;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
import junit.framework.*;
|
||||
|
||||
import org.ice4j.message.*;
|
||||
import org.ice4j.socket.*;
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
/**
|
||||
@ -91,9 +91,9 @@ public class MessageEventDispatchingTest extends TestCase
|
||||
|
||||
stunStack = StunStack.getInstance();
|
||||
|
||||
clientSock = new DatagramSocket(clientAddress);
|
||||
serverSock = new DatagramSocket(serverAddress);
|
||||
serverSock2 = new DatagramSocket(serverAddress2);
|
||||
clientSock = new SafeCloseDatagramSocket(clientAddress);
|
||||
serverSock = new SafeCloseDatagramSocket(serverAddress);
|
||||
serverSock2 = new SafeCloseDatagramSocket(serverAddress2);
|
||||
|
||||
stunStack.addSocket(clientSock);
|
||||
stunStack.addSocket(serverSock);
|
||||
|
||||
@ -8,11 +8,11 @@ package org.ice4j;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
import junit.framework.*;
|
||||
|
||||
import org.ice4j.message.*;
|
||||
import org.ice4j.socket.*;
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
/**
|
||||
@ -63,8 +63,8 @@ public class TransactionSupportTests extends TestCase
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
clientSock = new DatagramSocket(clientAddress);
|
||||
serverSock = new DatagramSocket(serverAddress);
|
||||
clientSock = new SafeCloseDatagramSocket(clientAddress);
|
||||
serverSock = new SafeCloseDatagramSocket(serverAddress);
|
||||
|
||||
StunStack.getInstance().addSocket(clientSock);
|
||||
StunStack.getInstance().addSocket(serverSock);
|
||||
|
||||
@ -14,6 +14,7 @@ import junit.framework.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.message.*;
|
||||
import org.ice4j.socket.*;
|
||||
|
||||
/**
|
||||
* All unit stack tests should be provided later. I just don't have the time now.
|
||||
@ -60,7 +61,7 @@ public class ShallowStackTest extends TestCase {
|
||||
stunStack = StunStack.getInstance();
|
||||
|
||||
//access point
|
||||
localSock = new DatagramSocket(localAddress);
|
||||
localSock = new SafeCloseDatagramSocket(localAddress);
|
||||
stunStack.addSocket(localSock);
|
||||
|
||||
//init the dummy server
|
||||
@ -70,14 +71,13 @@ public class ShallowStackTest extends TestCase {
|
||||
protected void tearDown()
|
||||
throws Exception
|
||||
{
|
||||
msgFixture.tearDown();
|
||||
stunStack.removeSocket(localAddress);
|
||||
|
||||
localSock.close();
|
||||
|
||||
dummyServerSocket.close();
|
||||
|
||||
|
||||
msgFixture.tearDown();
|
||||
msgFixture = null;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ import java.util.logging.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.message.*;
|
||||
import org.ice4j.socket.*;
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
/**
|
||||
@ -56,7 +57,7 @@ public class ResponseSequenceServer
|
||||
{
|
||||
stunStack = StunStack.getInstance();
|
||||
|
||||
localSocket = new DatagramSocket(serverAddress);
|
||||
localSocket = new SafeCloseDatagramSocket(serverAddress);
|
||||
|
||||
stunStack.addSocket(localSocket);
|
||||
stunStack.addRequestListener(serverAddress, this);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user