解決策を探しましたが、まだ見つかりません。プログラムの1つのインスタンスのみを一度に実行できるようにソケットロックをプログラムしようとしていますが、ソケットクローズ例外が発生します。これが私のSocketCodeコードです
package utilities;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import utilities.enums.ClientType;
public class SocketLocker {
private final int PORT;
private ServerSocket serverSocket;
private Socket clientSocket;
public SocketLocker(ClientType cType) {
serverSocket = null;
clientSocket = null;
switch (cType)
{
case PRODUCER:
PORT = 7856;
break;
case CONSUMER:
PORT = 7857;
break;
default:
PORT = 7859;
}
}
public boolean getLock() {
try {
serverSocket = new ServerSocket(PORT,1);
clientSocket = serverSocket.accept();
return true;
} catch (IOException e) {
// TODO: log event
e.printStackTrace();
}
return false;
}
public void releaseLock() {
try {
if (clientSocket != null)
clientSocket.close();
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
// TODO: Log event
}
}
}
そして、これが私のユニットテストコードです。これは、例外を取得するために実行しているものです。
package utilities;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import utilities.enums.ClientType;
public class SocketLockerTest {
private SocketLocker producerLocker;
private SocketLocker consumerLocker;
@Before
public void start() {
producerLocker = new SocketLocker(ClientType.PRODUCER);
consumerLocker = new SocketLocker(ClientType.CONSUMER);
}
@Test(timeout = 10000)
public void lockProducer() {
assertTrue(producerLocker.getLock());
}
@Test(timeout = 10000)
public void lockConsumer() {
assertTrue(consumerLocker.getLock());
}
@Test(timeout = 10000)
public void lockProducerTwice() {
producerLocker.getLock();
assertFalse(producerLocker.getLock());
}
@Test(timeout = 10000)
public void lockSeparately() {
producerLocker.getLock();
assertTrue(consumerLocker.getLock());
}
@Test(timeout = 10000)
public void lockProdcuerSeparateObject(){
producerLocker.getLock();
assertFalse(new SocketLocker(ClientType.PRODUCER).getLock());
}
@After
public void finish() {
producerLocker.releaseLock();
consumerLocker.releaseLock();
}
}
そして、ここに例外があります:
java.net.SocketException: socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(Unknown Source)
at java.net.ServerSocket.implAccept(Unknown Source)
at java.net.ServerSocket.accept(Unknown Source)
at utilities.SocketLocker.getLock(SocketLocker.java:34)
at utilities.SocketLockerTest.lockProducer(SocketLockerTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.FailOnTimeout$1.run(FailOnTimeout.java:28)