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
import dorkbox.network.Client
import dorkbox.network.Server
import dorkbox.network.connection.Connection
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
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-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
val sizeToTest = ExpandableDirectByteBuffer . MAX _BUFFER _LENGTH / 16
2022-06-29 21:56:12 +02:00
val hugeData = ByteArray ( sizeToTest )
SecureRandom ( ) . nextBytes ( hugeData )
2022-04-04 14:46:22 +02:00
run {
val configuration = serverConfig ( )
val server : Server < Connection > = Server ( configuration )
addEndPoint ( server )
2023-06-26 19:28:55 +02:00
server . bind ( 2000 )
2022-04-04 14:46:22 +02:00
server . onMessage < ByteArray > {
println ( " 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 ( )
}
}
run {
val config = clientConfig ( )
val client : Client < Connection > = Client ( config ) {
Connection ( it )
}
addEndPoint ( client )
client . onConnect {
2022-08-19 23:31:07 +02:00
logger . error { " Sending huge data: ${hugeData.size} bytes " }
2022-08-19 23:39:30 +02:00
send ( hugeData )
2022-04-04 14:46:22 +02:00
}
2023-06-26 19:28:55 +02:00
client . connect ( LOCALHOST , 2000 )
2022-04-04 14:46:22 +02:00
}
2022-08-02 12:11:36 +02:00
waitForThreads ( )
2022-04-04 14:46:22 +02:00
}
}