Skip to content

Commit f2104b3

Browse files
Really exit accept thread in tests
1 parent d446bb4 commit f2104b3

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

tests/test/src/libdaemonjvm/tests/TestUtil.scala

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import java.net.ServerSocket
99
import scala.util.Properties
1010
import java.util.concurrent.CountDownLatch
1111
import java.net.Socket
12+
import java.util.concurrent.atomic.AtomicBoolean
13+
import libdaemonjvm.internal.SocketFile
1214

1315
object TestUtil {
1416
private lazy val testDirBase = {
@@ -61,12 +63,17 @@ object TestUtil {
6163
var maybeServerChannel: Either[LockError, Either[ServerSocket, ServerSocketChannel]] = null
6264
var acceptThreadOpt = Option.empty[Thread]
6365
val accepting = new CountDownLatch(1)
66+
val shouldStop = new AtomicBoolean(false)
6467
try {
6568
maybeServerChannel = Lock.tryAcquire(files, proc)
6669
if (Properties.isWin)
6770
// Windows named pipes seem no to accept clients unless accept is being called on the server socket
6871
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+
)))
7077
for (t <- acceptThreadOpt) {
7178
t.start()
7279
accepting.await()
@@ -75,14 +82,19 @@ object TestUtil {
7582
f(maybeServerChannel)
7683
}
7784
finally {
85+
shouldStop.set(true)
86+
SocketFile.canConnect(files.socketPaths) // unblock the server thread last accept
7887
for (e <- Option(maybeServerChannel); channel <- e)
7988
channel.merge.close()
80-
acceptThreadOpt.foreach(_.interrupt()) // not sure this has an effect... :|
8189
}
8290
}
8391

8492
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 =
8698
new Thread(
8799
s"libdaemonjvm-tests-accept-and-discard-${acceptAndDiscardCount.incrementAndGet()}"
88100
) {
@@ -99,7 +111,7 @@ object TestUtil {
99111
}
100112
override def run(): Unit = {
101113
accepting.countDown()
102-
while (true) {
114+
while (!shouldStop()) {
103115
val client = s.accept()
104116
// closing the client socket in the background, as this call seems to block a few seconds
105117
closeSocket(client)

0 commit comments

Comments
 (0)