Network/test/dorkboxTest/network/StreamingTest.kt

79 lines
2.4 KiB
Kotlin
Raw Normal View History

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.
*/
package dorkboxTest.network
import dorkbox.network.Client
import dorkbox.network.Server
import dorkbox.network.connection.Connection
import dorkbox.util.Sys
import org.agrona.ExpandableDirectByteBuffer
import org.junit.Assert
import org.junit.Test
import java.security.SecureRandom
class StreamingTest : BaseTest() {
@Test
fun sendStreamingObject() {
2023-06-25 17:25:29 +02:00
// TODO: streaming data is NOT saved to temp files, it is in memory. every 16 megs should be flushed to disk (this is arbitrary and should be a config setting). 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)
val server = run {
val configuration = serverConfig()
val server: Server<Connection> = Server(configuration)
addEndPoint(server)
server.onMessage<ByteArray> {
println("received data, shutting down!")
Assert.assertEquals(sizeToTest, it.size)
Assert.assertArrayEquals(hugeData, it)
stopEndPoints()
}
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" }
send(hugeData)
logger.error { "Done sending huge data: ${hugeData.size} bytes" }
}
client
}
server.bind(2000)
client.connect(LOCALHOST, 2000)
waitForThreads()
}
}