2020-08-19 15:29:35 +02:00
|
|
|
/*
|
2023-06-16 11:18:03 +02:00
|
|
|
* Copyright 2023 dorkbox, llc
|
2020-08-19 15:29:35 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2023-06-16 11:18:03 +02:00
|
|
|
*/
|
|
|
|
/*
|
2020-08-19 15:29:35 +02:00
|
|
|
* Copyright (c) 2008, Nathan Sweet
|
2020-07-03 01:45:18 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided with the distribution.
|
|
|
|
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2020-08-12 23:38:56 +02:00
|
|
|
package dorkboxTest.network
|
2020-07-03 01:45:18 +02:00
|
|
|
|
2020-08-12 23:38:56 +02:00
|
|
|
import dorkbox.network.Client
|
|
|
|
import dorkbox.network.Server
|
|
|
|
import dorkbox.network.connection.Connection
|
2020-08-18 23:17:49 +02:00
|
|
|
import dorkbox.network.connection.ConnectionParams
|
2020-07-03 01:45:18 +02:00
|
|
|
import dorkbox.util.exceptions.InitializationException
|
|
|
|
import dorkbox.util.exceptions.SecurityException
|
2020-08-27 13:46:14 +02:00
|
|
|
import kotlinx.atomicfu.atomic
|
2020-07-03 01:45:18 +02:00
|
|
|
import org.junit.Assert
|
|
|
|
import org.junit.Test
|
|
|
|
import java.io.IOException
|
|
|
|
|
|
|
|
class ListenerTest : BaseTest() {
|
2020-08-27 13:46:14 +02:00
|
|
|
private val origString = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
2020-07-03 01:45:18 +02:00
|
|
|
private val limit = 20
|
2020-08-27 13:46:14 +02:00
|
|
|
|
|
|
|
private val count = atomic(0)
|
|
|
|
var checkFail1 = atomic(false)
|
|
|
|
var checkFail2 = atomic(false)
|
|
|
|
|
|
|
|
var overrideCheck = atomic(false)
|
|
|
|
var serverOnMessage = atomic(false)
|
2022-06-06 11:41:46 +02:00
|
|
|
|
|
|
|
var serverConnectionOnMessage = atomic(0)
|
|
|
|
var serverDisconnectMessage = atomic(0)
|
2020-08-27 13:46:14 +02:00
|
|
|
var serverConnect = atomic(false)
|
|
|
|
var serverDisconnect = atomic(false)
|
|
|
|
var clientConnect = atomic(false)
|
|
|
|
var clientDisconnect = atomic(false)
|
2020-07-03 01:45:18 +02:00
|
|
|
|
|
|
|
// quick and dirty test to also test connection sub-classing
|
2020-08-18 23:17:49 +02:00
|
|
|
internal open inner class TestConnectionA(connectionParameters: ConnectionParams<TestConnectionA>) : Connection(connectionParameters) {
|
2020-07-03 01:45:18 +02:00
|
|
|
open fun check() {
|
2023-06-18 19:41:22 +02:00
|
|
|
overrideCheck.lazySet(true)
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Throws(SecurityException::class, InitializationException::class, IOException::class, InterruptedException::class)
|
|
|
|
fun listener() {
|
2023-09-17 02:36:45 +02:00
|
|
|
val server = object : Server<TestConnectionA>(serverConfig()) {
|
|
|
|
override fun newConnection(connectionParameters: ConnectionParams<TestConnectionA>): TestConnectionA {
|
|
|
|
return TestConnectionA(connectionParameters)
|
|
|
|
}
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
2023-09-17 02:36:45 +02:00
|
|
|
|
2020-07-03 01:45:18 +02:00
|
|
|
addEndPoint(server)
|
2023-09-07 01:01:55 +02:00
|
|
|
|
|
|
|
// has session/stream count errors!
|
2020-07-03 01:45:18 +02:00
|
|
|
// standard listener
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onMessage<String> { message ->
|
2022-08-02 12:11:36 +02:00
|
|
|
logger.error ("server string message")
|
2020-07-03 01:45:18 +02:00
|
|
|
// should be called
|
2021-04-30 22:39:36 +02:00
|
|
|
check()
|
|
|
|
send(message)
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// generic listener
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onMessage<Any> {
|
2020-07-03 01:45:18 +02:00
|
|
|
// should be called!
|
2023-06-18 19:41:22 +02:00
|
|
|
serverOnMessage.lazySet(true)
|
2022-08-02 12:11:36 +02:00
|
|
|
logger.error ("server any message")
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// standard connect check
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onConnect {
|
2022-08-02 12:11:36 +02:00
|
|
|
logger.error ("server connect")
|
2023-06-18 19:41:22 +02:00
|
|
|
serverConnect.lazySet(true)
|
2020-08-27 13:46:14 +02:00
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
onMessage<Any> {
|
2022-08-02 12:11:36 +02:00
|
|
|
logger.error ("server connection any message")
|
2022-06-06 11:41:46 +02:00
|
|
|
serverConnectionOnMessage.getAndIncrement()
|
2020-08-18 23:17:49 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
onDisconnect {
|
2022-08-02 12:11:36 +02:00
|
|
|
logger.error ("server connection disconnect")
|
2022-06-06 11:41:46 +02:00
|
|
|
serverDisconnectMessage.getAndIncrement()
|
2020-08-18 23:17:49 +02:00
|
|
|
}
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// standard listener disconnect check
|
2021-04-30 22:39:36 +02:00
|
|
|
server.onDisconnect {
|
2022-08-02 12:11:36 +02:00
|
|
|
logger.error ("server disconnect")
|
2023-06-18 19:41:22 +02:00
|
|
|
serverDisconnect.lazySet(true)
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----
|
2023-09-17 02:36:45 +02:00
|
|
|
val client = object : Client<TestConnectionA>(clientConfig()) {
|
|
|
|
override fun newConnection(connectionParameters: ConnectionParams<TestConnectionA>): TestConnectionA {
|
|
|
|
return TestConnectionA(connectionParameters)
|
|
|
|
}
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
2023-09-17 02:36:45 +02:00
|
|
|
|
2020-07-03 01:45:18 +02:00
|
|
|
addEndPoint(client)
|
|
|
|
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onConnect {
|
2023-09-13 16:57:32 +02:00
|
|
|
logger.error("client connect 1")
|
2021-04-30 22:39:36 +02:00
|
|
|
send(origString) // 20 a's
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
2022-08-02 12:11:36 +02:00
|
|
|
// standard connect check
|
|
|
|
client.onConnect {
|
2023-09-13 16:57:32 +02:00
|
|
|
logger.error("client connect 2")
|
2023-06-18 19:41:22 +02:00
|
|
|
clientConnect.lazySet(true)
|
2022-08-02 12:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-30 22:39:36 +02:00
|
|
|
client.onMessage<String> { message ->
|
2023-09-13 16:57:32 +02:00
|
|
|
logger.error("client string message")
|
2020-07-03 01:45:18 +02:00
|
|
|
if (origString != message) {
|
2023-06-18 19:41:22 +02:00
|
|
|
checkFail2.lazySet(true)
|
2020-07-03 01:45:18 +02:00
|
|
|
System.err.println("original string not equal to the string received")
|
|
|
|
stopEndPoints()
|
|
|
|
return@onMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count.getAndIncrement() < limit) {
|
2021-04-30 22:39:36 +02:00
|
|
|
send(message)
|
2020-07-03 01:45:18 +02:00
|
|
|
} else {
|
|
|
|
stopEndPoints()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// standard listener disconnect check
|
|
|
|
client.onDisconnect {
|
2022-08-02 12:11:36 +02:00
|
|
|
logger.error ("client disconnect")
|
2023-06-18 19:41:22 +02:00
|
|
|
clientDisconnect.lazySet(true)
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-29 19:26:27 +02:00
|
|
|
server.bind(2000)
|
2023-06-26 19:28:55 +02:00
|
|
|
client.connect(LOCALHOST, 2000)
|
2020-07-03 01:45:18 +02:00
|
|
|
|
2022-08-02 12:11:36 +02:00
|
|
|
waitForThreads()
|
2020-07-03 01:45:18 +02:00
|
|
|
|
2022-06-06 11:41:46 +02:00
|
|
|
// +1 BECAUSE we are `getAndIncrement` for each check earlier
|
|
|
|
val limitCheck = limit+1
|
|
|
|
|
|
|
|
Assert.assertEquals(limitCheck, count.value)
|
|
|
|
|
2020-08-27 13:46:14 +02:00
|
|
|
Assert.assertTrue(overrideCheck.value)
|
|
|
|
Assert.assertTrue(serverOnMessage.value)
|
2022-07-27 00:20:34 +02:00
|
|
|
Assert.assertEquals(limitCheck, serverConnectionOnMessage.value)
|
|
|
|
Assert.assertEquals(1, serverDisconnectMessage.value)
|
2020-08-27 13:46:14 +02:00
|
|
|
Assert.assertTrue(serverConnect.value)
|
|
|
|
Assert.assertTrue(serverDisconnect.value)
|
|
|
|
Assert.assertTrue(clientConnect.value)
|
|
|
|
Assert.assertTrue(clientDisconnect.value)
|
|
|
|
|
|
|
|
Assert.assertFalse(checkFail1.value)
|
|
|
|
Assert.assertFalse(checkFail2.value)
|
2020-07-03 01:45:18 +02:00
|
|
|
}
|
|
|
|
}
|