I have gotten an issue when i using gradle and jmockit for unit test

04:44:42.961 [ERROR] [system.err] 04:44:42.958 [ERROR] [org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection] Unexpected exception thrown.
04:44:42.961 [ERROR] [system.err] org.gradle.internal.remote.internal.MessageIOException: Could not write '/0:0:0:0:0:0:0:1:57383'.
04:44:42.961 [ERROR] [system.err]       at org.gradle.internal.remote.internal.inet.SocketConnection.flush(SocketConnection.java:121)
04:44:42.961 [ERROR] [system.err]       at org.gradle.internal.remote.internal.hub.MessageHub$ConnectionDispatch.run(MessageHub.java:304)
04:44:42.961 [ERROR] [system.err]       at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
04:44:42.961 [ERROR] [system.err]       at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
04:44:42.961 [ERROR] [system.err]       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
04:44:42.961 [ERROR] [system.err]       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
04:44:42.961 [ERROR] [system.err]       at java.lang.Thread.run(Thread.java:745)
04:44:42.961 [ERROR] [system.err] Caused by: java.nio.channels.ClosedChannelException
04:44:42.961 [ERROR] [system.err]       at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:270)
04:44:42.961 [ERROR] [system.err]       at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:461)
04:44:42.961 [ERROR] [system.err]       at org.gradle.internal.remote.internal.inet.SocketConnection$SocketOutputStream.writeWithNonBlockingRetry(SocketConnection.java:259)
04:44:42.962 [ERROR] [system.err]       at org.gradle.internal.remote.internal.inet.SocketConnection$SocketOutputStream.writeBufferToChannel(SocketConnection.java:247)
04:44:42.962 [ERROR] [system.err]       at org.gradle.internal.remote.internal.inet.SocketConnection$SocketOutputStream.flush(SocketConnection.java:241)
04:44:42.962 [ERROR] [system.err]       at org.gradle.internal.remote.internal.inet.SocketConnection.flush(SocketConnection.java:119)
04:44:42.962 [ERROR] [system.err]       ... 6 more
04:44:49.217 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] DaemonExpirationPeriodicCheck running
04:44:49.217 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
04:44:49.217 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired.
04:44:49.217 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
04:44:49.217 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
04:44:49.217 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired.
04:44:49.217 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.

above is the log, when exception happened gradle will keep acquiring and releasing lock.

my unit code is

@Test
  public void test_getAddress1(@Mocked SocketChannel sc, @Mocked Socket so)
      throws UnknownHostException {
    InetAddress iadd = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
    new NonStrictExpectations() {
      {
        sc.socket();
        returns(so);
        
        so.getInetAddress();
        returns(iadd);
        
        Deencapsulation.setField(dc, "socketChannel", sc);
      }
    };
    
    String ip = sc.socket().getInetAddress().getHostAddress();
    
    assertEquals(ip, "127.0.0.1");
  }

Gradle Version: 3.1 and 3.2-r3
Operating System and JVM version: Linux and jdk1.8, jmockit 1.15
Is this a regression? If yes, which version of Gradle do you know it last worked for?

i don’t know why gradle threw a exception and keep doing the get lock and free lock.
for my instances of socket and socketchannel are mocked.
please help me. thanks.

Hi @pmmhjj,

I am not very familiar with JMockit but it looks like your code has some side effects on real sockets and closes the socket of the daemon. Can you confirm that?

Cheers,
Stefan