-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87cdaa5
commit b6d181b
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.lyft.kronos.sntp | ||
|
||
import java.net.InetAddress | ||
import java.net.UnknownHostException | ||
|
||
internal interface Dns { | ||
|
||
fun resolve(host: String): Array<out InetAddress> | ||
|
||
class Impl : Dns { | ||
|
||
override fun resolve(host: String) = try { | ||
InetAddress.getAllByName(host).orEmpty() | ||
} catch (e: SecurityException) { | ||
emptyArray() | ||
} catch (e: UnknownHostException) { | ||
emptyArray() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.lyft.kronos.sntp | ||
|
||
import java.lang.Exception | ||
import java.net.DatagramPacket | ||
import java.net.DatagramSocket | ||
import java.net.SocketAddress | ||
import java.nio.ByteBuffer | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal interface Udp { | ||
|
||
enum class Result { Success, Failure } | ||
|
||
fun request(address: SocketAddress, request: ByteBuffer, response: ByteBuffer): Result | ||
|
||
class Impl : Udp { | ||
|
||
companion object { | ||
private val TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(3).toInt() | ||
} | ||
|
||
override fun request(address: SocketAddress, request: ByteBuffer, response: ByteBuffer): Result { | ||
val requestPacket = DatagramPacket(request.array(), request.capacity(), address) | ||
val responsePacket = DatagramPacket(response.array(), response.capacity()) | ||
|
||
return try { | ||
DatagramSocket().use { socket -> | ||
socket.soTimeout = TIMEOUT_MILLIS | ||
|
||
socket.send(requestPacket) | ||
socket.receive(responsePacket) | ||
} | ||
|
||
Result.Success | ||
} catch (e: Exception) { | ||
Result.Failure | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
kronos-java/src/test/java/com/lyft/kronos/sntp/DnsTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.lyft.kronos.sntp | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
import java.net.InetAddress | ||
|
||
class DnsTests { | ||
|
||
private val dns by lazy { Dns.Impl() } | ||
|
||
@Test | ||
fun resolveLocalhost() { | ||
assertThat(dns.resolve("localhost")).allMatch { it.isLoopbackAddress } | ||
} | ||
|
||
@Test | ||
fun resolveUnknown() { | ||
assertThat(dns.resolve("unknown")).isEqualTo(emptyArray<InetAddress>()) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
kronos-java/src/test/java/com/lyft/kronos/sntp/UdpTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.lyft.kronos.sntp | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
import java.net.InetAddress | ||
import java.net.InetSocketAddress | ||
import java.nio.ByteBuffer | ||
|
||
class UdpTests { | ||
|
||
private val udp by lazy { Udp.Impl() } | ||
|
||
@Test | ||
fun requestUnknown() { | ||
val address = InetSocketAddress(InetAddress.getLoopbackAddress(), 42) | ||
val request = ByteBuffer.allocate(42) | ||
val response = ByteBuffer.allocate(42) | ||
|
||
assertThat(udp.request(address, request, response)).isEqualTo(Udp.Result.Failure) | ||
} | ||
} |