2023-05-28 18:41:46 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2023 dorkbox, llc
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-04 14:46:22 +02:00
|
|
|
package dorkboxTest.network
|
|
|
|
|
2023-07-16 14:57:04 +02:00
|
|
|
import dorkbox.bytes.sha256
|
2022-04-04 14:46:22 +02:00
|
|
|
import dorkbox.network.Client
|
|
|
|
import dorkbox.network.Server
|
|
|
|
import dorkbox.network.connection.Connection
|
2023-06-29 19:26:27 +02:00
|
|
|
import dorkbox.util.Sys
|
2022-06-11 23:54:40 +02:00
|
|
|
import org.agrona.ExpandableDirectByteBuffer
|
|
|
|
import org.junit.Assert
|
2022-04-04 14:46:22 +02:00
|
|
|
import org.junit.Test
|
2023-07-16 14:57:04 +02:00
|
|
|
import java.io.File
|
2023-09-13 15:57:54 +02:00
|
|
|
import java.rmi.server.RemoteObject
|
2022-06-11 23:54:40 +02:00
|
|
|
import java.security.SecureRandom
|
2022-04-04 14:46:22 +02:00
|
|
|
|
|
|
|
class StreamingTest : BaseTest() {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun sendStreamingObject() {
|
2023-07-16 14:57:04 +02:00
|
|
|
// if this number is too high, we will run out of memory
|
2023-06-29 12:11:17 +02:00
|
|
|
// ExpandableDirectByteBuffer.MAX_BUFFER_LENGTH = 1073741824
|
|
|
|
val sizeToTest = ExpandableDirectByteBuffer.MAX_BUFFER_LENGTH / 32
|
2022-06-29 21:56:12 +02:00
|
|
|
val hugeData = ByteArray(sizeToTest)
|
|
|
|
SecureRandom().nextBytes(hugeData)
|
|
|
|
|
|
|
|
|
2023-06-29 19:26:27 +02:00
|
|
|
val server = run {
|
2022-04-04 14:46:22 +02:00
|
|
|
val configuration = serverConfig()
|
|
|
|
|
|
|
|
val server: Server<Connection> = Server(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
|
|
|
server.onMessage<ByteArray> {
|
2023-07-15 13:12:25 +02:00
|
|
|
logger.error { "received data, shutting down!" }
|
2022-06-11 23:54:40 +02:00
|
|
|
Assert.assertEquals(sizeToTest, it.size)
|
2022-06-29 21:56:12 +02:00
|
|
|
Assert.assertArrayEquals(hugeData, it)
|
2022-04-04 14:46:22 +02:00
|
|
|
stopEndPoints()
|
|
|
|
}
|
2023-06-29 19:26:27 +02:00
|
|
|
server
|
2022-04-04 14:46:22 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 19:26:27 +02:00
|
|
|
val client = run {
|
2022-04-04 14:46:22 +02:00
|
|
|
val config = clientConfig()
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config) {
|
|
|
|
Connection(it)
|
|
|
|
}
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
client.onConnect {
|
2023-06-29 19:26:27 +02:00
|
|
|
logger.error { "Sending huge data: ${Sys.getSizePretty(hugeData.size)} bytes" }
|
2022-08-19 23:39:30 +02:00
|
|
|
send(hugeData)
|
2023-06-29 19:26:27 +02:00
|
|
|
logger.error { "Done sending huge data: ${hugeData.size} bytes" }
|
2022-04-04 14:46:22 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 19:26:27 +02:00
|
|
|
client
|
2022-04-04 14:46:22 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 19:26:27 +02:00
|
|
|
server.bind(2000)
|
|
|
|
client.connect(LOCALHOST, 2000)
|
|
|
|
|
|
|
|
|
2023-07-16 14:57:04 +02:00
|
|
|
waitForThreads()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2023-09-13 15:57:54 +02:00
|
|
|
fun sendRmiStreamingObject() {
|
|
|
|
// if this number is too high, we will run out of memory
|
|
|
|
// ExpandableDirectByteBuffer.MAX_BUFFER_LENGTH = 1073741824
|
|
|
|
val sizeToTest = ExpandableDirectByteBuffer.MAX_BUFFER_LENGTH / 32
|
|
|
|
val hugeData = ByteArray(sizeToTest)
|
|
|
|
SecureRandom().nextBytes(hugeData)
|
|
|
|
|
2023-07-16 14:57:04 +02:00
|
|
|
|
|
|
|
val server = run {
|
|
|
|
val configuration = serverConfig()
|
2023-09-13 15:57:54 +02:00
|
|
|
configuration.serialization.rmi.register(TestStream::class.java, TestStreamCow::class.java)
|
2023-07-16 14:57:04 +02:00
|
|
|
|
|
|
|
val server: Server<Connection> = Server(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
2023-09-13 15:57:54 +02:00
|
|
|
server.onInit {
|
|
|
|
rmi.save(TestStreamCow(this@StreamingTest, hugeData), 765)
|
|
|
|
}
|
2023-07-16 14:57:04 +02:00
|
|
|
|
2023-09-13 15:57:54 +02:00
|
|
|
server
|
|
|
|
}
|
|
|
|
|
|
|
|
val client = run {
|
|
|
|
val config = clientConfig()
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config) {
|
|
|
|
Connection(it)
|
|
|
|
}
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
client.onConnect {
|
|
|
|
logger.error { "Sending huge data: ${Sys.getSizePretty(hugeData.size)} bytes" }
|
|
|
|
val remote = rmi.get<TestStream>(765)
|
|
|
|
dorkbox.network.rmi.RemoteObject.cast(remote).async = true
|
|
|
|
remote.send(hugeData)
|
|
|
|
logger.error { "Done sending huge data: ${hugeData.size} bytes" }
|
|
|
|
}
|
|
|
|
|
|
|
|
client
|
|
|
|
}
|
|
|
|
|
|
|
|
server.bind(2000)
|
|
|
|
client.connect(LOCALHOST, 2000)
|
|
|
|
|
|
|
|
|
|
|
|
waitForThreads()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun sendRmiFile() {
|
|
|
|
val file = File("LICENSE")
|
|
|
|
|
|
|
|
val server = run {
|
|
|
|
val configuration = serverConfig()
|
|
|
|
configuration.serialization.rmi.register(TestStream::class.java, TestStreamCow::class.java)
|
|
|
|
|
|
|
|
val server: Server<Connection> = Server(configuration)
|
|
|
|
addEndPoint(server)
|
|
|
|
|
|
|
|
server.onInit {
|
|
|
|
rmi.save(TestStreamCow(this@StreamingTest, null, file), 765)
|
2023-07-16 14:57:04 +02:00
|
|
|
}
|
2023-09-13 15:57:54 +02:00
|
|
|
|
2023-07-16 14:57:04 +02:00
|
|
|
server
|
|
|
|
}
|
|
|
|
|
|
|
|
val client = run {
|
|
|
|
val config = clientConfig()
|
|
|
|
|
|
|
|
val client: Client<Connection> = Client(config) {
|
|
|
|
Connection(it)
|
|
|
|
}
|
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
client.onConnect {
|
|
|
|
logger.error { "Sending file: $file" }
|
2023-09-13 15:57:54 +02:00
|
|
|
val remote = rmi.get<TestStream>(765)
|
|
|
|
remote.send(file)
|
2023-07-16 14:57:04 +02:00
|
|
|
logger.error { "Done sending file: $file" }
|
|
|
|
}
|
|
|
|
|
|
|
|
client
|
|
|
|
}
|
|
|
|
|
|
|
|
server.bind(2000)
|
|
|
|
client.connect(LOCALHOST, 2000)
|
|
|
|
|
|
|
|
|
2022-08-02 12:11:36 +02:00
|
|
|
waitForThreads()
|
2022-04-04 14:46:22 +02:00
|
|
|
}
|
2023-09-13 15:57:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestStream {
|
|
|
|
fun send(byteArray: ByteArray)
|
|
|
|
fun send(file: File)
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestStreamCow(val unitTest: BaseTest, val hugeData: ByteArray? = null, val file: File? = null) : TestStream {
|
|
|
|
override fun send(byteArray: ByteArray) {
|
|
|
|
// not used
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun send(file: File) {
|
|
|
|
// not used
|
|
|
|
}
|
|
|
|
|
|
|
|
fun send(connection: Connection, byteArray: ByteArray) {
|
|
|
|
connection.logger.error { "received data, shutting down!" }
|
|
|
|
Assert.assertEquals(hugeData!!.size, byteArray.size)
|
|
|
|
Assert.assertArrayEquals(hugeData, byteArray)
|
|
|
|
unitTest.stopEndPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun send(connection: Connection, file: File) {
|
|
|
|
connection.logger.error { "received data, shutting down!" }
|
|
|
|
connection.logger.error { "FILE: $file" }
|
|
|
|
Assert.assertArrayEquals(this.file!!.sha256(), file.sha256())
|
|
|
|
file.delete()
|
|
|
|
unitTest.stopEndPoints()
|
|
|
|
}
|
2022-04-04 14:46:22 +02:00
|
|
|
}
|