support negative short ids in async DNS resolver code (#401)

This commit is contained in:
PJ Fanning 2023-06-15 20:32:35 +01:00 committed by GitHub
parent 6bdce7f95b
commit ead8c7e889
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View file

@ -16,12 +16,14 @@ class IdGeneratorSpec extends PekkoSpec {
"IdGenerator" must {
"provide a thread-local-random" in {
val gen = IdGenerator(IdGenerator.Policy.ThreadLocalRandom)
gen.nextId() should be < Short.MaxValue
gen.nextId() should be <= Short.MaxValue
gen.nextId() should be >= Short.MinValue
}
"provide a secure-random" in {
val gen = IdGenerator(IdGenerator.Policy.SecureRandom)
gen.nextId() should be < Short.MaxValue
gen.nextId() should be <= Short.MaxValue
gen.nextId() should be >= Short.MinValue
}
}
}

View file

@ -30,6 +30,8 @@ private[pekko] trait IdGenerator {
*/
@InternalApi
private[pekko] object IdGenerator {
private val MaxUnsignedShort = 65535
sealed trait Policy
object Policy {
@ -54,7 +56,6 @@ private[pekko] object IdGenerator {
/**
* @return a random sequence of ids for production
*/
def random(rand: java.util.Random): IdGenerator = new IdGenerator {
override def nextId(): Short = rand.nextInt(Short.MaxValue).toShort
}
def random(rand: java.util.Random): IdGenerator =
() => (rand.nextInt(MaxUnsignedShort) - Short.MinValue).toShort
}