Actually sync delegates

This commit is contained in:
DBotThePony 2024-02-24 22:57:42 +07:00
parent 6c56dfce33
commit e9062ab21c
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 131 additions and 6 deletions

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons
projectVersion=2.8.0
projectVersion=2.8.1
guavaDepVersion=33.0.0
gsonDepVersion=2.8.9

View File

@ -58,6 +58,14 @@ class DelegateSyncher : Observer {
fun read(stream: DataInputStream) {
isRemote = true
var readID = stream.readVarInt()
while (readID > 0) {
val slot = slots.getOrNull(readID - 1) ?: throw IndexOutOfBoundsException("Unknown networked slot ${readID - 1}!")
slot.read(stream)
readID = stream.readVarInt()
}
}
fun read(stream: InputStream) {
@ -682,7 +690,7 @@ class DelegateSyncher : Observer {
}
for (v in sorted) {
stream.writeVarInt(v.parent.id)
stream.writeVarInt(v.parent.id + 1)
v.write(stream)
}

View File

@ -16,20 +16,20 @@ object BTreeDB6Tests {
if (file.exists()) file.delete()
val create = BTreeDB6.create(file, 4096, sync = false)
for (i in 0 .. 8000) {
for (i in 0 .. 800) {
val s = "This is key $i"
val k = ByteKey("This is key $i")
create.write(k, s.toByteArray())
assertEquals(s, String(create.read(k).get()))
}
for (i in 0 .. 8000) {
for (i in 0 .. 800) {
val s = "This is key $i"
val k = ByteKey("This is key $i")
assertEquals(s, String(create.read(k).get()))
}
for (i in 0 .. 8000) {
for (i in 0 .. 800) {
val s = "This is key $i"
val k = ByteKey("This is key $i")
create.write(k, s.toByteArray())
@ -40,7 +40,7 @@ object BTreeDB6Tests {
val create2 = BTreeDB6(file)
for (i in 0 .. 8000) {
for (i in 0 .. 800) {
val s = "This is key $i"
val k = ByteKey("This is key $i")
assertEquals(s, String(create2.read(k).get()))

View File

@ -0,0 +1,117 @@
package ru.dbotthepony.kommons.test
import it.unimi.dsi.fastutil.io.FastByteArrayInputStream
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import ru.dbotthepony.kommons.io.DelegateSyncher
import ru.dbotthepony.kommons.util.Delegate
import kotlin.test.assertEquals
object DelegateSyncherTests {
@Test
@DisplayName("Delegate Syncher basic test")
fun test() {
val a = DelegateSyncher()
val b = DelegateSyncher()
a.int(0).delegate.accept(4)
val remote = a.Remote()
val payload = remote.write()!!
val v = b.int(0)
b.read(FastByteArrayInputStream(payload.array, 0, payload.length))
assertEquals(4, v.delegate.get())
}
@Test
@DisplayName("Delegate Syncher multiple delegates")
fun multiple() {
val a = DelegateSyncher()
val b = DelegateSyncher()
val ints = ArrayList<Delegate<Int>>()
val longs = ArrayList<Delegate<Long>>()
for (i in 0 .. 10) {
a.int().delegate.accept(i)
a.long().delegate.accept(i.toLong())
ints.add(b.int())
longs.add(b.long())
}
val remote = a.Remote()
val payload = remote.write()!!
b.read(FastByteArrayInputStream(payload.array, 0, payload.length))
for (i in 0 .. 10) {
assertEquals(i, ints[i].get())
assertEquals(i.toLong(), longs[i].get())
}
}
@Test
@DisplayName("Delegate Syncher network half of delegates")
fun half() {
val a = DelegateSyncher()
val b = DelegateSyncher()
val ints = ArrayList<Delegate<Int>>()
val longs = ArrayList<Delegate<Long>>()
for (i in 0 .. 10) {
if (i % 2 == 0) {
a.int()
a.long()
} else {
a.int().delegate.accept(i)
a.long().delegate.accept(i.toLong())
}
ints.add(b.int())
longs.add(b.long())
}
val remote = a.Remote()
val payload = remote.write()!!
b.read(FastByteArrayInputStream(payload.array, 0, payload.length))
for (i in 0 .. 10) {
if (i % 2 == 0) {
assertEquals(0, ints[i].get())
assertEquals(0L, longs[i].get())
} else {
assertEquals(i, ints[i].get())
assertEquals(i.toLong(), longs[i].get())
}
}
}
@Test
@DisplayName("Delegate Syncher >127 delegates")
fun moreThanByte() {
val a = DelegateSyncher()
val b = DelegateSyncher()
val ints = ArrayList<Delegate<Int>>()
val longs = ArrayList<Delegate<Long>>()
for (i in 0 .. 400) {
a.int().delegate.accept(i)
a.long().delegate.accept(i.toLong())
ints.add(b.int())
longs.add(b.long())
}
val remote = a.Remote()
val payload = remote.write()!!
b.read(FastByteArrayInputStream(payload.array, 0, payload.length))
for (i in 0 .. 400) {
assertEquals(i, ints[i].get())
assertEquals(i.toLong(), longs[i].get())
}
}
}