|
| 1 | +package jamule |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.FunSpec |
| 4 | +import io.kotest.matchers.shouldBe |
| 5 | +import io.mockk.every |
| 6 | +import io.mockk.mockk |
| 7 | +import io.mockk.verify |
| 8 | +import jamule.request.StatsRequest |
| 9 | +import org.slf4j.LoggerFactory |
| 10 | +import java.io.ByteArrayInputStream |
| 11 | +import java.io.OutputStream |
| 12 | +import java.net.Socket |
| 13 | +import java.util.concurrent.CountDownLatch |
| 14 | + |
| 15 | +@OptIn(ExperimentalStdlibApi::class) |
| 16 | +class AmuleConnectionTest : FunSpec({ |
| 17 | + |
| 18 | + val socket = mockk<Socket>() |
| 19 | + val logger = LoggerFactory.getLogger(this::class.java) |
| 20 | + val outputStream = OutputStream.nullOutputStream() |
| 21 | + every { socket.getOutputStream() } returns outputStream |
| 22 | + every { socket.close() } returns Unit |
| 23 | + val authSaltResponse = ByteArrayInputStream("000000220000000d4f0116050855099a4aea510c43".hexToByteArray()) |
| 24 | + val authOkResponse = |
| 25 | + ByteArrayInputStream("000000220000001d0401e0a8960616322e332e31204164756e616e7a4120323031322e3100".hexToByteArray()) |
| 26 | + val statusResponse = ByteArrayInputStream( |
| 27 | + ("000000220000008c0c10d08003021664d082020100d484020100d4860302" + |
| 28 | + "1664d488020100d48a020100d084020100d086020100d09002010" + |
| 29 | + "0d08c020100d092040400017cbbd09402010ad096040402e2740f" + |
| 30 | + "d09803020438d0b60201000b023f03e0a881081f01e0a88206124" + |
| 31 | + "16b74656f6e20536572766572204e6f3200b07de76247b50c0404" + |
| 32 | + "1d4e48541404041d4e485419") |
| 33 | + .hexToByteArray() |
| 34 | + ) |
| 35 | + |
| 36 | + test("single request works ok") { |
| 37 | + val amule = AmuleConnection({ socket }, "password", logger) |
| 38 | + every { socket.getInputStream() } returnsMany listOf( |
| 39 | + authSaltResponse, |
| 40 | + authOkResponse, |
| 41 | + statusResponse |
| 42 | + ) |
| 43 | + amule.sendRequest(StatsRequest()) |
| 44 | + // Called 3 times: 1 for salt, 1 for auth, 1 for stats |
| 45 | + verify(exactly = 3) { socket.getOutputStream() } |
| 46 | + } |
| 47 | + |
| 48 | + test("multiple parallel requests are synchronised") { |
| 49 | + val amule = AmuleConnection({ socket }, "password", logger) |
| 50 | + val firstRequestArrivedLatch = CountDownLatch(1) |
| 51 | + val firstRequestLatch = CountDownLatch(1) |
| 52 | + val secondRequestLatch = CountDownLatch(1) |
| 53 | + var requestCount = 0 |
| 54 | + every { socket.getInputStream() } answers { |
| 55 | + when (requestCount++) { |
| 56 | + 0 -> authSaltResponse |
| 57 | + 1 -> authOkResponse |
| 58 | + 2 -> { |
| 59 | + firstRequestArrivedLatch.countDown() |
| 60 | + firstRequestLatch.await() |
| 61 | + statusResponse |
| 62 | + } |
| 63 | + |
| 64 | + 3 -> { |
| 65 | + secondRequestLatch.await() |
| 66 | + statusResponse |
| 67 | + } |
| 68 | + |
| 69 | + else -> throw IllegalStateException("Unexpected request count: $requestCount") |
| 70 | + }.also { requestCount++ } |
| 71 | + } |
| 72 | + // Send two requests from two separate threads |
| 73 | + Thread { amule.sendRequest(StatsRequest()) }.start() |
| 74 | + Thread { amule.sendRequest(StatsRequest()) }.start() |
| 75 | + // Wait for the first request to arrive |
| 76 | + firstRequestArrivedLatch.await() |
| 77 | + Thread.sleep(50) // Allow for the second request to arrive if it's not synchronised |
| 78 | + requestCount shouldBe 3 |
| 79 | + firstRequestLatch.countDown() |
| 80 | + secondRequestLatch.countDown() |
| 81 | + } |
| 82 | + |
| 83 | +}) |
0 commit comments