@@ -7,7 +7,6 @@ import java.nio.channels.ServerSocketChannel
77import libdaemonjvm ._
88import libdaemonjvm .internal ._
99import libdaemonjvm .server ._
10- import java .net .ServerSocket
1110import scala .util .Properties
1211import java .util .concurrent .CountDownLatch
1312import java .net .Socket
@@ -49,89 +48,36 @@ object TestUtil {
4948 }
5049 def tryAcquire [T ](dir : os.Path )(f : (
5150 LockFiles ,
52- Either [LockError , Either [ ServerSocket , ServerSocketChannel ] ]
51+ Either [LockError , ServerSocketChannel ]
5352 ) => T ): T = {
5453 val files = lockFiles(dir)
5554 tryAcquire(files) { maybeChannel =>
5655 f(files, maybeChannel)
5756 }
5857 }
59- def tryAcquire [T ](files : LockFiles )(f : Either [
60- LockError ,
61- Either [ServerSocket , ServerSocketChannel ]
62- ] => T ): T =
58+ def tryAcquire [T ](files : LockFiles )(f : Either [LockError , ServerSocketChannel ] => T ): T =
6359 tryAcquire(files, LockProcess .default)(f)
6460 def tryAcquire [T ](
6561 files : LockFiles ,
6662 proc : LockProcess
67- )(f : Either [LockError , Either [ServerSocket , ServerSocketChannel ]] => T ): T = {
68- var serverChannel : Either [ServerSocket , ServerSocketChannel ] = null
69- var acceptThreadOpt = Option .empty[Thread ]
70- val accepting = new CountDownLatch (1 )
71- val shouldStop = new AtomicBoolean (false )
63+ )(f : Either [LockError , ServerSocketChannel ] => T ): T = {
64+ var serverChannel : ServerSocketChannel = null
65+ val accepting = new CountDownLatch (1 )
7266 try {
7367 val maybeServerChannel = Lock .tryAcquire(files, proc) {
7468 serverChannel = SocketHandler .server(files.socketPaths)
75- if (Properties .isWin)
76- // Windows named pipes seem no to accept clients unless accept is being called on the server socket
77- acceptThreadOpt =
78- serverChannel.left.toOption.map(acceptAndDiscard(
79- _,
80- accepting,
81- () => shouldStop.get()
82- ))
83- for (t <- acceptThreadOpt) {
84- t.start()
85- accepting.await()
86- // waiting so that the accept call below effectively awaits client... :|
87- Thread .sleep(
88- 1000L
89- )
90- }
9169 serverChannel
9270 }
9371 f(maybeServerChannel)
9472 }
9573 finally {
96- shouldStop.set(true )
9774 try SocketFile .canConnect(files.socketPaths) // unblock the server thread last accept
9875 catch {
9976 case NonFatal (e) =>
10077 System .err.println(s " Ignoring $e while trying to unblock last accept " )
10178 }
10279 for (channel <- Option (serverChannel))
103- channel.merge. close()
80+ channel.close()
10481 }
10582 }
106-
107- val acceptAndDiscardCount = new AtomicInteger
108- def acceptAndDiscard (
109- s : ServerSocket ,
110- accepting : CountDownLatch ,
111- shouldStop : () => Boolean
112- ): Thread =
113- new Thread (
114- s " libdaemonjvm-tests-accept-and-discard- ${acceptAndDiscardCount.incrementAndGet()}"
115- ) {
116- setDaemon(true )
117- val closeCount = new AtomicInteger
118- def closeSocket (socket : Socket ): Unit = {
119- val t = new Thread (s " $getName-close- ${closeCount.incrementAndGet()}" ) {
120- setDaemon(true )
121- override def run (): Unit = {
122- socket.close()
123- }
124- }
125- t.start()
126- }
127- override def run (): Unit = {
128- accepting.countDown()
129- while (! shouldStop()) {
130- val client = s.accept()
131- // closing the client socket in the background, as this call seems to block a few seconds
132- closeSocket(client)
133- }
134- }
135- }
136-
13783}
0 commit comments