/* * 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.bytes.sha256 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.io.File import java.security.SecureRandom class StreamingTest : BaseTest() { @Test fun sendStreamingObject() { // 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 = Server(configuration) addEndPoint(server) server.onMessage { logger.error { "received data, shutting down!" } Assert.assertEquals(sizeToTest, it.size) Assert.assertArrayEquals(hugeData, it) stopEndPoints() } server } val client = run { val config = clientConfig() val client: Client = 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() } @Test fun sendFile() { val file = File("LICENSE") val server = run { val configuration = serverConfig() val server: Server = Server(configuration) addEndPoint(server) server.onMessage { logger.error { "received data, shutting down!" } logger.error { "FILE: $it" } Assert.assertArrayEquals(file.sha256(), it.sha256()) it.delete() stopEndPoints() } server } val client = run { val config = clientConfig() val client: Client = Client(config) { Connection(it) } addEndPoint(client) client.onConnect { logger.error { "Sending file: $file" } send(file) logger.error { "Done sending file: $file" } } client } server.bind(2000) client.connect(LOCALHOST, 2000) waitForThreads() } }