Skip to content

Commit

Permalink
chore: Update Server Discovery test
Browse files Browse the repository at this point in the history
Updates the server discovery test to correctly check
if a server is found or not from a Broadcast message.
  • Loading branch information
kingargyle committed Dec 30, 2024
1 parent a701ca4 commit 06f8a76
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions emby-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ dependencies {

testImplementation(libs.toothpick.testing)
kspTest(libs.toothpick.ksp.compiler)
testImplementation(libs.turbine)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import org.joda.time.LocalDateTime
import org.joda.time.format.DateTimeFormat

class LocalDateJsonAdapter : JsonAdapter<LocalDateTime>() {
override fun fromJson(reader: JsonReader?): LocalDateTime? {
override fun fromJson(reader: JsonReader): LocalDateTime? {

val dateTime = reader?.nextString()!!.replaceAfter(".", "")
val dateTime = reader.nextString()!!.replaceAfter(".", "")
val dateformater = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH':'mm':'ss'.'")
// DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'")
// } else if(dateTime!!.contains("+")) {
Expand All @@ -20,7 +20,7 @@ class LocalDateJsonAdapter : JsonAdapter<LocalDateTime>() {
return LocalDateTime.parse(dateTime, dateformater)
}

override fun toJson(writer: JsonWriter?, value: LocalDateTime?) {
writer?.value(value?.toString())
override fun toJson(writer: JsonWriter, value: LocalDateTime?) {
writer.value(value?.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,25 @@ class EmbyServerDiscover {
//Try the 255.255.255.255 first

try {
val sendPacket = DatagramPacket(sendData, sendData.size, InetAddress.getByName("255.255.255.255"), port)
val sendPacket = DatagramPacket(
sendData,
sendData.size,
InetAddress.getByName("255.255.255.255"),
port
)
c.send(sendPacket)
Timber.d(">>> Request packet sent to: 255.255.255.255 (DEFAULT)")
} catch (e: Exception) {
Timber.e(e, "Error sending DatagramPacket")
}

// try {
// val sendPacket = DatagramPacket(sendData, sendData.size, useMultiCastAddress(), port)
// c.send(sendPacket)
// } catch (e: Exception) {
// Timber.e(e, "error sending to multicast address")
// }
try {
val sendPacket =
DatagramPacket(sendData, sendData.size, useMultiCastAddress(), port)
c.send(sendPacket)
} catch (e: Exception) {
Timber.e(e, "error sending to multicast address")
}

// Broadcast the message over all the network interfaces
val interfaces = NetworkInterface.getNetworkInterfaces()
Expand All @@ -65,7 +71,7 @@ class EmbyServerDiscover {
}

Timber.d(
">>> Request packet sent to: " + broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName()
">>> Request packet sent to: " + broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName()
)
}
}
Expand Down Expand Up @@ -126,6 +132,7 @@ class EmbyServerDiscover {
server.serverName = "Emby - " + embyServerInfo.name

ServerChannel.invokeServerEvent(server)
servers.add(server)

// TODO: Once completely migrated to the ServerChannel remove the event bus.
eventBus.post(server)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
package us.nineworlds.serenity.emby.server

import app.cash.turbine.turbineScope
import assertk.assertThat
import assertk.assertions.isEqualTo
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.test.runTest
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.shadows.ShadowLog
import timber.log.Timber
import us.nineworlds.serenity.common.channels.ServerChannel

@RunWith(RobolectricTestRunner::class)
class EmbyServerDiscoverTest {

private lateinit var serverDiscovery : EmbyServerDiscover

@Before
fun setUp() {
Timber.plant(Timber.DebugTree())
ShadowLog.stream = System.out
serverDiscovery = EmbyServerDiscover()
}

@Test
fun `discover any known servers`() = runTest {
serverDiscovery.findServers()
turbineScope {
val servers = ServerChannel.serverEvents.distinctUntilChanged().testIn(backgroundScope)
serverDiscovery.findServers()
val serverItem = servers.awaitItem()
assertThat(serverItem.serverName).isEqualTo("Emby - Emby Server")
servers.cancelAndIgnoreRemainingEvents()
}
}

}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ androidxTestCoreVersion = "1.6.1"
androidxRecyclerViewVersion = "1.3.2"
resourcefulVersion = "1.1.0"
mockkVersion = "1.13.13"
turbine = "1.2.0"

[libraries]
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "annotationVersion" }
Expand Down Expand Up @@ -133,5 +134,6 @@ kotlin-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coro

toothpick-testing = { group = "com.github.stephanenicolas.toothpick", name = "toothpick-testing", version.ref = "toothPickVersion"}
assertk-jvm = { module = "com.willowtreeapps.assertk:assertk", version.ref = "assertkJvmVersion" }
turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine"}

[plugins]

0 comments on commit 06f8a76

Please sign in to comment.