@@ -9,6 +9,8 @@ import java.net.ServerSocket
9
9
import scala .util .Properties
10
10
import java .util .concurrent .CountDownLatch
11
11
import java .net .Socket
12
+ import java .util .concurrent .atomic .AtomicBoolean
13
+ import libdaemonjvm .internal .SocketFile
12
14
13
15
object TestUtil {
14
16
private lazy val testDirBase = {
@@ -61,12 +63,17 @@ object TestUtil {
61
63
var maybeServerChannel : Either [LockError , Either [ServerSocket , ServerSocketChannel ]] = null
62
64
var acceptThreadOpt = Option .empty[Thread ]
63
65
val accepting = new CountDownLatch (1 )
66
+ val shouldStop = new AtomicBoolean (false )
64
67
try {
65
68
maybeServerChannel = Lock .tryAcquire(files, proc)
66
69
if (Properties .isWin)
67
70
// Windows named pipes seem no to accept clients unless accept is being called on the server socket
68
71
acceptThreadOpt =
69
- maybeServerChannel.toOption.flatMap(_.left.toOption.map(acceptAndDiscard(_, accepting)))
72
+ maybeServerChannel.toOption.flatMap(_.left.toOption.map(acceptAndDiscard(
73
+ _,
74
+ accepting,
75
+ () => shouldStop.get()
76
+ )))
70
77
for (t <- acceptThreadOpt) {
71
78
t.start()
72
79
accepting.await()
@@ -75,14 +82,19 @@ object TestUtil {
75
82
f(maybeServerChannel)
76
83
}
77
84
finally {
85
+ shouldStop.set(true )
86
+ SocketFile .canConnect(files.socketPaths) // unblock the server thread last accept
78
87
for (e <- Option (maybeServerChannel); channel <- e)
79
88
channel.merge.close()
80
- acceptThreadOpt.foreach(_.interrupt()) // not sure this has an effect... :|
81
89
}
82
90
}
83
91
84
92
val acceptAndDiscardCount = new AtomicInteger
85
- def acceptAndDiscard (s : ServerSocket , accepting : CountDownLatch ): Thread =
93
+ def acceptAndDiscard (
94
+ s : ServerSocket ,
95
+ accepting : CountDownLatch ,
96
+ shouldStop : () => Boolean
97
+ ): Thread =
86
98
new Thread (
87
99
s " libdaemonjvm-tests-accept-and-discard- ${acceptAndDiscardCount.incrementAndGet()}"
88
100
) {
@@ -99,7 +111,7 @@ object TestUtil {
99
111
}
100
112
override def run (): Unit = {
101
113
accepting.countDown()
102
- while (true ) {
114
+ while (! shouldStop() ) {
103
115
val client = s.accept()
104
116
// closing the client socket in the background, as this call seems to block a few seconds
105
117
closeSocket(client)
0 commit comments