From f18d44232f46556940fa91afe9797c974111b60f Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Fri, 26 Apr 2024 16:44:21 +0200 Subject: [PATCH] fix(tests): fixing issue where jvmTest ran all tests but ended up stuck indefinitely (#69) --- .../commonTest/kotlin/io/zenoh/ConfigTest.kt | 75 -------- .../commonTest/kotlin/io/zenoh/DeleteTest.kt | 15 +- .../src/commonTest/kotlin/io/zenoh/GetTest.kt | 182 +++++------------- .../kotlin/io/zenoh/PublisherTest.kt | 54 +++--- .../src/commonTest/kotlin/io/zenoh/PutTest.kt | 6 +- .../kotlin/io/zenoh/QueryableTest.kt | 94 +++++---- .../kotlin/io/zenoh/SubscriberTest.kt | 72 +++---- .../kotlin/io/zenoh/UserAttachmentTest.kt | 110 +++++------ 8 files changed, 207 insertions(+), 401 deletions(-) delete mode 100644 zenoh-java/src/commonTest/kotlin/io/zenoh/ConfigTest.kt diff --git a/zenoh-java/src/commonTest/kotlin/io/zenoh/ConfigTest.kt b/zenoh-java/src/commonTest/kotlin/io/zenoh/ConfigTest.kt deleted file mode 100644 index adaaf9f2..00000000 --- a/zenoh-java/src/commonTest/kotlin/io/zenoh/ConfigTest.kt +++ /dev/null @@ -1,75 +0,0 @@ -// -// Copyright (c) 2023 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -package io.zenoh - -import io.zenoh.keyexpr.intoKeyExpr -import io.zenoh.sample.Sample -import io.zenoh.value.Value -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNotNull - -class ConfigTest { - companion object { - val TEST_KEY_EXP = "example/testing/keyexpr".intoKeyExpr() - } - - @Test - fun configLoadsJsonTest() { - // There are a bunch of different possible configurations. - // For this test we do the following: - // - Modifying the default endpoints. - // - Disabling multicast scouting to avoid unintended connections. - val clientCfg = "{\n" + - " \"mode\": \"peer\",\n" + - " \"connect\": {\n" + - " \"endpoints\": [\"tcp/localhost:7450\"]\n" + - " },\n" + - " \"scouting\": {\n" + - " \"multicast\": {\n" + - " \"enabled\": false\n" + - " }\n" + - " }\n" + - "}\n" - - val serverCfg = "{\n" + - " \"mode\": \"peer\",\n" + - " \"listen\": {\n" + - " \"endpoints\": [\"tcp/localhost:7450\"]\n" + - " },\n" + - " \"scouting\": {\n" + - " \"multicast\": {\n" + - " \"enabled\": false\n" + - " }\n" + - " }\n" + - "}\n" - - val sessionClient = Session.open(Config.from(clientCfg)) - val sessionServer = Session.open(Config.from(serverCfg)) - var receivedSample: Sample? = null - val subscriber = sessionClient.declareSubscriber(TEST_KEY_EXP).with { sample -> receivedSample = sample }.res() - - val value = Value("encrypted_message") - sessionServer.put(TEST_KEY_EXP, value).res() - Thread.sleep(1000) - - subscriber.close() - sessionClient.close() - sessionServer.close() - - assertNotNull(receivedSample) - assertEquals(receivedSample!!.value, value) - } -} diff --git a/zenoh-java/src/commonTest/kotlin/io/zenoh/DeleteTest.kt b/zenoh-java/src/commonTest/kotlin/io/zenoh/DeleteTest.kt index 22d3ae91..083e210a 100644 --- a/zenoh-java/src/commonTest/kotlin/io/zenoh/DeleteTest.kt +++ b/zenoh-java/src/commonTest/kotlin/io/zenoh/DeleteTest.kt @@ -23,19 +23,16 @@ import kotlin.test.assertNotNull class DeleteTest { - companion object { - val TEST_KEY_EXP = "example/testing/keyexpr".intoKeyExpr() - } - @Test - fun subscriber_receivesDelete() { + fun delete_isProperlyReceivedBySubscriber() { val session = Session.open() var receivedSample: Sample? = null - val subscriber = session.declareSubscriber(TEST_KEY_EXP).with { sample -> receivedSample = sample }.res() - session.delete(TEST_KEY_EXP).res() - subscriber.undeclare() + val keyExpr = "example/testing/keyexpr".intoKeyExpr() + val subscriber = session.declareSubscriber(keyExpr).with { sample -> receivedSample = sample }.res() + session.delete(keyExpr).res() + subscriber.close() + keyExpr.close() session.close() - assertNotNull(receivedSample) assertEquals(receivedSample!!.kind, SampleKind.DELETE) } diff --git a/zenoh-java/src/commonTest/kotlin/io/zenoh/GetTest.kt b/zenoh-java/src/commonTest/kotlin/io/zenoh/GetTest.kt index 79b94001..57747556 100644 --- a/zenoh-java/src/commonTest/kotlin/io/zenoh/GetTest.kt +++ b/zenoh-java/src/commonTest/kotlin/io/zenoh/GetTest.kt @@ -20,117 +20,63 @@ import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.SampleKind import io.zenoh.query.Reply import io.zenoh.queryable.Queryable -import io.zenoh.sample.Sample import io.zenoh.selector.Selector import io.zenoh.value.Value import org.apache.commons.net.ntp.TimeStamp import java.time.Duration import java.util.* -import kotlin.test.assertEquals -import kotlin.test.assertTrue -import kotlin.test.Test +import kotlin.test.* class GetTest { companion object { - const val TEST_KEY_EXP = "example/testing/keyexpr" - const val TEST_KEY_EXP_WILD = "example/testing/*" - const val TEST_PAYLOAD = "Hello" + val value = Value("Test") + val timestamp = TimeStamp.getCurrentTime() + val kind = SampleKind.PUT } - @Test - fun get_runsWithCallback() { - val sessionA = Session.open() + private lateinit var session: Session + private lateinit var keyExpr: KeyExpr + private lateinit var queryable: Queryable - val value = Value(TEST_PAYLOAD) - val timeStamp = TimeStamp.getCurrentTime() - val kind = SampleKind.PUT - val keyExpr = TEST_KEY_EXP.intoKeyExpr() - val queryable = sessionA.declareQueryable(keyExpr).with { query -> - query.reply(keyExpr) + @BeforeTest + fun setUp() { + session = Session.open() + keyExpr = "example/testing/keyexpr".intoKeyExpr() + queryable = session.declareQueryable(keyExpr).with { query -> + query.reply(query.keyExpr) .success(value) - .withTimeStamp(timeStamp) + .withTimeStamp(timestamp) .withKind(kind) .res() }.res() - - val sessionB = Session.open() - - sessionB.get(keyExpr).with { reply: Reply -> - assertTrue(reply is Reply.Success) - assertEquals(value, reply.sample.value) - assertEquals(kind, reply.sample.kind) - assertEquals(keyExpr, reply.sample.keyExpr) - assertEquals(timeStamp, reply.sample.timestamp) - }.timeout(Duration.ofMillis(1000)).res() - - Thread.sleep(1000) - - queryable.undeclare() - sessionA.close() - sessionB.close() } - @Test - fun getWithSelectorParamsTest() { - val session = Session.open() - - var receivedParams = "" - val keyExpr = TEST_KEY_EXP.intoKeyExpr() - val queryable = session.declareQueryable(keyExpr).with { it.use { query -> - receivedParams = query.parameters - }}.res() - - val params = "arg1=val1,arg2=val2" - val selector = Selector(keyExpr, params) - session.get(selector).res() - + @AfterTest + fun tearDown() { + keyExpr.close() queryable.close() session.close() + } - assertEquals(params, receivedParams) + @Test + fun get_runsWithCallback() { + var reply: Reply? = null + session.get(keyExpr).with { reply = it }.timeout(Duration.ofMillis(1000)).res() + + assertTrue(reply is Reply.Success) + val sample = (reply as Reply.Success).sample + assertEquals(value, sample.value) + assertEquals(kind, sample.kind) + assertEquals(keyExpr, sample.keyExpr) + assertEquals(timestamp, sample.timestamp) } @Test fun get_runsWithHandler() { - val sessionA = Session.open() - val repliedSamples: ArrayList = ArrayList() - val queryablesAmount = 3 - val declaredQueryables: ArrayList> = ArrayList() - - val value = Value(TEST_PAYLOAD) - val timestamp = TimeStamp.getCurrentTime() - val kind = SampleKind.PUT - - for (i in 1..queryablesAmount) { - val keyExpr = KeyExpr.tryFrom(TEST_KEY_EXP + i.toString()) - val queryable = sessionA.declareQueryable(keyExpr).with { it.use { query -> - query.reply(keyExpr) - .success(value) - .withTimeStamp(timestamp) - .withKind(kind) - .res() - } - } - .res() - - declaredQueryables.add(queryable) - repliedSamples.add(Sample(keyExpr, value, kind, timestamp)) - } - - val sessionB = Session.open() - val receiver: ArrayList = - sessionB.get(TEST_KEY_EXP_WILD.intoKeyExpr()) - .with(GetHandler()) - .timeout(Duration.ofMillis(1000)) - .res()!! + val receiver: ArrayList = session.get(keyExpr).with(TestHandler()) + .timeout(Duration.ofMillis(1000)).res()!! - Thread.sleep(1000) - declaredQueryables.forEach { queryable -> queryable.undeclare() } - sessionA.close() - sessionB.close() - - assertEquals(queryablesAmount, receiver.size) for (reply in receiver) { reply as Reply.Success val receivedSample = reply.sample @@ -141,66 +87,26 @@ class GetTest { } @Test - fun get_runsWithBlockingQueue() { - val sessionA = Session.open() - - val queryablesAmount = 3 - val declaredQueryables: ArrayList> = ArrayList() - - val value = Value(TEST_PAYLOAD) - val timestamp = TimeStamp.getCurrentTime() - val kind = SampleKind.PUT - - for (i in 1..queryablesAmount) { - val keyExpr = (TEST_KEY_EXP + i.toString()).intoKeyExpr() - val queryable = sessionA.declareQueryable(keyExpr).with { it.use { query -> - query.reply(keyExpr) - .success(value) - .withTimeStamp(timestamp) - .withKind(kind) - .res() - } - } - .res() - - declaredQueryables.add(queryable) - } - - val receivedReplies = ArrayList(0) - - val sessionB = Session.open() - val receiver = sessionB.get(TEST_KEY_EXP_WILD.intoKeyExpr()).res()!! - - Thread.sleep(1000) - - val iterator = receiver.iterator() - while (iterator.hasNext()) { - try { - val reply = iterator.next().get() - receivedReplies.add(reply) - } catch (e: Exception) { - break + fun getWithSelectorParamsTest() { + var receivedParams = String() + val queryable = session.declareQueryable(keyExpr).with { + it.use { query -> + receivedParams = query.parameters } - } - - sessionB.close() + }.res() - declaredQueryables.forEach { queryable -> queryable.undeclare() } - sessionA.close() + val params = "arg1=val1,arg2=val2" + val selector = Selector(keyExpr, params) + session.get(selector).with {}.timeout(Duration.ofMillis(1000)).res() + Thread.sleep(1000) - assertEquals(queryablesAmount, receivedReplies.size) - for (reply in receivedReplies) { - assert(reply is Reply.Success) - val receivedSample = (reply as Reply.Success).sample - assertEquals(value, receivedSample.value) - assertEquals(SampleKind.PUT, receivedSample.kind) - assertEquals(timestamp, receivedSample.timestamp) - } + queryable.close() + assertEquals(params, receivedParams) } } /** A dummy handler for get operations. */ -private class GetHandler : Handler> { +private class TestHandler : Handler> { val performedReplies: ArrayList = ArrayList() diff --git a/zenoh-java/src/commonTest/kotlin/io/zenoh/PublisherTest.kt b/zenoh-java/src/commonTest/kotlin/io/zenoh/PublisherTest.kt index 37b64f83..b89b07c1 100644 --- a/zenoh-java/src/commonTest/kotlin/io/zenoh/PublisherTest.kt +++ b/zenoh-java/src/commonTest/kotlin/io/zenoh/PublisherTest.kt @@ -14,30 +14,46 @@ package io.zenoh +import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.KnownEncoding import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.Encoding import io.zenoh.prelude.SampleKind +import io.zenoh.publication.Publisher import io.zenoh.sample.Sample +import io.zenoh.subscriber.Subscriber import io.zenoh.value.Value -import kotlin.test.Test -import kotlin.test.assertEquals +import kotlin.test.* class PublisherTest { - companion object { - val TEST_KEY_EXP = "example/testing/keyexpr".intoKeyExpr() + lateinit var session: Session + lateinit var receivedSamples: ArrayList + lateinit var publisher: Publisher + lateinit var subscriber: Subscriber + lateinit var keyExpr: KeyExpr + + @BeforeTest + fun setUp() { + session = Session.open() + keyExpr = "example/testing/keyexpr".intoKeyExpr() + publisher = session.declarePublisher(keyExpr).res() + subscriber = session.declareSubscriber(keyExpr).with { sample -> + receivedSamples.add(sample) + }.res() + receivedSamples = ArrayList() + } + + @AfterTest + fun tearDown() { + publisher.close() + subscriber.close() + session.close() + keyExpr.close() } @Test fun putTest() { - val session = Session.open() - - val receivedSamples = ArrayList() - val publisher = session.declarePublisher(TEST_KEY_EXP).res() - val subscriber = session.declareSubscriber(TEST_KEY_EXP).with { sample -> - receivedSamples.add(sample) - }.res() val testValues = arrayListOf( Value("Test 1".encodeToByteArray(), Encoding(KnownEncoding.TEXT_PLAIN)), @@ -46,9 +62,6 @@ class PublisherTest { ) testValues.forEach() { value -> publisher.put(value).res() } - subscriber.undeclare() - publisher.undeclare() - session.close() assertEquals(receivedSamples.size, testValues.size) for ((index, sample) in receivedSamples.withIndex()) { @@ -58,20 +71,7 @@ class PublisherTest { @Test fun deleteTest() { - val session = Session.open() - - val receivedSamples = ArrayList() - val subscriber = session.declareSubscriber(TEST_KEY_EXP).with { sample -> - receivedSamples.add(sample) - }.res() - - val publisher = session.declarePublisher(TEST_KEY_EXP).res() publisher.delete().res() - - publisher.undeclare() - subscriber.undeclare() - session.close() - assertEquals(1, receivedSamples.size) assertEquals(SampleKind.DELETE, receivedSamples[0].kind) } diff --git a/zenoh-java/src/commonTest/kotlin/io/zenoh/PutTest.kt b/zenoh-java/src/commonTest/kotlin/io/zenoh/PutTest.kt index 91b63f7a..ffebc4b4 100644 --- a/zenoh-java/src/commonTest/kotlin/io/zenoh/PutTest.kt +++ b/zenoh-java/src/commonTest/kotlin/io/zenoh/PutTest.kt @@ -31,16 +31,18 @@ class PutTest { } @Test - fun subscriber_receivesPutValue() { + fun putTest() { val session = Session.open() var receivedSample: Sample? = null val keyExpr = TEST_KEY_EXP.intoKeyExpr() val subscriber = session.declareSubscriber(keyExpr).with { sample -> receivedSample = sample }.res() val value = Value(TEST_PAYLOAD.toByteArray(), Encoding(KnownEncoding.TEXT_PLAIN)) session.put(keyExpr, value).res() - subscriber.undeclare() + subscriber.close() session.close() + keyExpr.close() assertNotNull(receivedSample) assertEquals(value, receivedSample!!.value) } } + diff --git a/zenoh-java/src/commonTest/kotlin/io/zenoh/QueryableTest.kt b/zenoh-java/src/commonTest/kotlin/io/zenoh/QueryableTest.kt index 0cfb4f93..d21a63b9 100644 --- a/zenoh-java/src/commonTest/kotlin/io/zenoh/QueryableTest.kt +++ b/zenoh-java/src/commonTest/kotlin/io/zenoh/QueryableTest.kt @@ -15,6 +15,7 @@ package io.zenoh import io.zenoh.handlers.Handler +import io.zenoh.keyexpr.KeyExpr import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.SampleKind import io.zenoh.query.Reply @@ -26,11 +27,7 @@ import java.time.Duration import java.time.Instant import java.util.* import java.util.concurrent.BlockingQueue -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNotNull -import kotlin.test.assertNull -import kotlin.test.assertTrue +import kotlin.test.* class QueryableTest { @@ -39,105 +36,100 @@ class QueryableTest { const val TEST_PAYLOAD = "Hello queryable" } + private lateinit var session: Session + private lateinit var testKeyExpr: KeyExpr + + @BeforeTest + fun setUp() { + session = Session.open() + testKeyExpr = "example/testing/keyexpr".intoKeyExpr() + } + + @AfterTest + fun tearDown() { + session.close() + testKeyExpr.close() + } + /** Test validating both Queryable and get operations. */ @Test fun queryable_runsWithCallback() { - val sessionA = Session.open() - val sample = Sample( - TEST_KEY_EXP, Value(TEST_PAYLOAD), SampleKind.PUT, TimeStamp(Date.from(Instant.now())) + testKeyExpr, Value(TEST_PAYLOAD), SampleKind.PUT, TimeStamp(Date.from(Instant.now())) ) - val queryable = sessionA.declareQueryable(TEST_KEY_EXP).with { query -> - query.reply(TEST_KEY_EXP).success(sample.value).withTimeStamp(sample.timestamp!!).res() + val queryable = session.declareQueryable(testKeyExpr).with { query -> + query.reply(testKeyExpr).success(sample.value).withTimeStamp(sample.timestamp!!).res() }.res() - val sessionB = Session.open() - - sessionB.get(TEST_KEY_EXP).with { reply: Reply -> - assertTrue(reply is Reply.Success) - assertEquals(reply.sample, sample) - }.timeout(Duration.ofMillis(1000)).res() + var reply: Reply? = null + val delay = Duration.ofMillis(1000) + session.get(testKeyExpr).with { reply = it }.timeout(delay).res() Thread.sleep(1000) - queryable.undeclare() - sessionA.close() - sessionB.close() + assertTrue(reply is Reply.Success) + assertEquals((reply as Reply.Success).sample, sample) + + queryable.close() } @Test fun queryable_runsWithHandler() { - val sessionA = Session.open() val handler = QueryHandler() - val queryable = sessionA.declareQueryable(TEST_KEY_EXP).with(handler).res() + val queryable = session.declareQueryable(testKeyExpr).with(handler).res() - val sessionB = Session.open() val receivedReplies = ArrayList() - sessionB.get(TEST_KEY_EXP).with { reply: Reply -> + session.get(testKeyExpr).with { reply: Reply -> receivedReplies.add(reply) - }.timeout(Duration.ofMillis(1000)).res() - - Thread.sleep(1000) + }.res() - queryable.undeclare() - sessionA.close() - sessionB.close() + Thread.sleep(500) - for (receivedReply in receivedReplies) { - assertTrue(receivedReply is Reply.Success) - } - assertEquals(handler.performedReplies, receivedReplies.map { reply -> (reply as Reply.Success).sample }) + queryable.close() + assertTrue(receivedReplies.all { it is Reply.Success }) + assertEquals(handler.performedReplies.size, receivedReplies.size) } @Test fun queryableBuilder_queueHandlerIsTheDefaultHandler() { - val session = Session.open() val queryable = session.declareQueryable(TEST_KEY_EXP).res() assertTrue(queryable.receiver is BlockingQueue>) + queryable.close() } @Test fun queryTest() { - val session = Session.open() var receivedQuery: Query? = null - val queryable = session.declareQueryable(TEST_KEY_EXP).with { query -> receivedQuery = query }.res() - - session.get(TEST_KEY_EXP).res() + val queryable = session.declareQueryable(testKeyExpr).with { query -> receivedQuery = query }.res() - queryable.undeclare() - session.close() + session.get(testKeyExpr).res() Thread.sleep(1000) + queryable.close() assertNotNull(receivedQuery) assertNull(receivedQuery!!.value) } @Test fun queryWithValueTest() { - val session = Session.open() var receivedQuery: Query? = null - val queryable = session.declareQueryable(TEST_KEY_EXP).with { query -> receivedQuery = query }.res() + val queryable = session.declareQueryable(testKeyExpr).with { query -> receivedQuery = query }.res() - session.get(TEST_KEY_EXP).withValue("Test value").res() + session.get(testKeyExpr).withValue("Test value").res() Thread.sleep(1000) - - queryable.undeclare() - session.close() - + queryable.close() assertNotNull(receivedQuery) assertEquals(Value("Test value"), receivedQuery!!.value) - } @Test fun onCloseTest() { - val session = Session.open() var onCloseWasCalled = false - val queryable = session.declareQueryable(TEST_KEY_EXP).onClose { onCloseWasCalled = true }.res() + val queryable = session.declareQueryable(testKeyExpr).onClose { onCloseWasCalled = true }.res() queryable.undeclare() + assertTrue(onCloseWasCalled) - session.close() } } diff --git a/zenoh-java/src/commonTest/kotlin/io/zenoh/SubscriberTest.kt b/zenoh-java/src/commonTest/kotlin/io/zenoh/SubscriberTest.kt index 32bbf393..be25d116 100644 --- a/zenoh-java/src/commonTest/kotlin/io/zenoh/SubscriberTest.kt +++ b/zenoh-java/src/commonTest/kotlin/io/zenoh/SubscriberTest.kt @@ -15,6 +15,7 @@ package io.zenoh import io.zenoh.handlers.Handler +import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.KnownEncoding import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.Encoding @@ -24,15 +25,11 @@ import java.util.* import java.util.concurrent.BlockingQueue import kotlin.collections.ArrayDeque import kotlin.collections.ArrayList -import kotlin.test.assertEquals -import kotlin.test.assertTrue -import kotlin.test.Test +import kotlin.test.* class SubscriberTest { companion object { - val TEST_KEY_EXP = "example/testing/keyexpr".intoKeyExpr() - val testValues = arrayListOf( Value("Test 1".encodeToByteArray(), Encoding(KnownEncoding.TEXT_PLAIN)), Value("Test 2".encodeToByteArray(), Encoding(KnownEncoding.TEXT_JSON)), @@ -40,66 +37,69 @@ class SubscriberTest { ) } - @Test - fun subscriber_runsWithCallback() { - val session = Session.open() - val receivedSamples = ArrayList() - val subscriber = session.declareSubscriber(TEST_KEY_EXP).with { sample -> receivedSamples.add(sample) }.res() + private lateinit var session: Session + private lateinit var testKeyExpr: KeyExpr - publishTestValues(session) + @BeforeTest + fun setUp() { + session = Session.open() + testKeyExpr = "example/testing/keyexpr".intoKeyExpr() + } - subscriber.undeclare() + @AfterTest + fun tearDown() { session.close() + testKeyExpr.close() + } + @Test + fun subscriber_runsWithCallback() { + val receivedSamples = ArrayList() + val subscriber = + session.declareSubscriber(testKeyExpr).with { sample -> receivedSamples.add(sample) }.res() + + testValues.forEach { value -> session.put(testKeyExpr, value).res() } assertEquals(receivedSamples.size, testValues.size) - for ((index, sample) in receivedSamples.withIndex()) { - assertEquals(sample.value, testValues[index]) + receivedSamples.zip(testValues).forEach { (sample, value) -> + assertEquals(sample.value, value) } + + subscriber.close() } @Test fun subscriber_runsWithHandler() { val handler = QueueHandler() - val session = Session.open() - val subscriber = session.declareSubscriber(TEST_KEY_EXP).with(handler).res() - publishTestValues(session) - subscriber.undeclare() - session.close() + val subscriber = session.declareSubscriber(testKeyExpr).with(handler).res() + + testValues.forEach { value -> session.put(testKeyExpr, value).res() } + assertEquals(handler.queue.size, testValues.size) - val queue = subscriber.receiver!! - assertEquals(queue.size, testValues.size) - for ((index, sample) in queue.withIndex()) { - assertEquals(sample.value, testValues[index]) + handler.queue.zip(testValues).forEach { (sample, value) -> + assertEquals(sample.value, value) } + + subscriber.close() } @Test fun subscriberBuilder_queueHandlerIsTheDefaultHandler() { - val session = Session.open() - val subscriber = session.declareSubscriber(TEST_KEY_EXP).res() + val subscriber = session.declareSubscriber(testKeyExpr).res() + subscriber.close() assertTrue(subscriber.receiver is BlockingQueue>) } @Test fun onCloseTest() { - val session = Session.open() var onCloseWasCalled = false - val subscriber = session.declareSubscriber(TEST_KEY_EXP).onClose { onCloseWasCalled = true }.res() + val subscriber = session.declareSubscriber(testKeyExpr).onClose { onCloseWasCalled = true }.res() subscriber.undeclare() assertTrue(onCloseWasCalled) - session.close() - } - - private fun publishTestValues(session: Session): ArrayList { - val publisher = session.declarePublisher(TEST_KEY_EXP).res() - testValues.forEach { value -> publisher.put(value).res() } - publisher.undeclare() - return testValues } } -private class QueueHandler : Handler> { +private class QueueHandler : Handler> { val queue: ArrayDeque = ArrayDeque() override fun handle(t: T) { diff --git a/zenoh-java/src/commonTest/kotlin/io/zenoh/UserAttachmentTest.kt b/zenoh-java/src/commonTest/kotlin/io/zenoh/UserAttachmentTest.kt index 9b0b89cd..ca6f050d 100644 --- a/zenoh-java/src/commonTest/kotlin/io/zenoh/UserAttachmentTest.kt +++ b/zenoh-java/src/commonTest/kotlin/io/zenoh/UserAttachmentTest.kt @@ -18,10 +18,10 @@ import io.zenoh.jni.decodeAttachment import io.zenoh.jni.encodeAttachment import io.zenoh.jni.toByteArray import io.zenoh.jni.toInt +import io.zenoh.keyexpr.KeyExpr import io.zenoh.keyexpr.intoKeyExpr import io.zenoh.prelude.Encoding import io.zenoh.prelude.KnownEncoding -import io.zenoh.prelude.SampleKind import io.zenoh.query.Reply import io.zenoh.sample.Attachment import io.zenoh.sample.Sample @@ -29,11 +29,15 @@ import io.zenoh.value.Value import java.time.Duration import kotlin.test.* + class UserAttachmentTest { + private lateinit var session: Session + private lateinit var keyExpr: KeyExpr + companion object { val value = Value("test", Encoding(KnownEncoding.TEXT_PLAIN)) - val keyExpr = "example/testing/attachment".intoKeyExpr() + const val keyExprString = "example/testing/attachment" val attachmentPairs = arrayListOf( "key1" to "value1", "key2" to "value2", "key3" to "value3", "repeatedKey" to "value1", "repeatedKey" to "value2" ) @@ -41,6 +45,18 @@ class UserAttachmentTest { Attachment(attachmentPairs.map { it.first.encodeToByteArray() to it.second.encodeToByteArray() }) } + @BeforeTest + fun setup() { + session = Session.open() + keyExpr = keyExprString.intoKeyExpr() + } + + @AfterTest + fun tearDown() { + session.close() + keyExpr.close() + } + private fun assertAttachmentOk(attachment: Attachment?) { assertNotNull(attachment) val receivedPairs = attachment.values @@ -54,25 +70,19 @@ class UserAttachmentTest { @Test fun putWithAttachmentTest() { var receivedSample: Sample? = null - val session = Session.open() - val subscriber = session.declareSubscriber(keyExpr).with { sample -> receivedSample = sample }.res() session.put(keyExpr, value).withAttachment(attachment).res() - Thread.sleep(500) - subscriber.close() - session.close() - assertNotNull(receivedSample) - assertEquals(value, receivedSample!!.value) - assertAttachmentOk(receivedSample!!.attachment) + assertNotNull(receivedSample) { + assertEquals(value, it.value) + assertAttachmentOk(it.attachment) + } } @Test fun publisherPutWithAttachmentTest() { - val session = Session.open() - var receivedSample: Sample? = null val publisher = session.declarePublisher(keyExpr).res() val subscriber = session.declareSubscriber(keyExpr).with { sample -> @@ -80,119 +90,95 @@ class UserAttachmentTest { }.res() publisher.put("test").withAttachment(attachment).res() + publisher.close() subscriber.close() - session.close() assertAttachmentOk(receivedSample!!.attachment!!) } @Test fun publisherPutWithoutAttachmentTest() { - val session = Session.open() - var receivedSample: Sample? = null val publisher = session.declarePublisher(keyExpr).res() - val subscriber = session.declareSubscriber(keyExpr).with { sample -> - receivedSample = sample - }.res() - publisher.put("test").res() + val subscriber = session.declareSubscriber(keyExpr).with { sample -> receivedSample = sample }.res() - Thread.sleep(1000) + publisher.put("test").res() - subscriber.close() publisher.close() - session.close() + subscriber.close() - assertNotNull(receivedSample) - assertNull(receivedSample!!.attachment) + assertNotNull(receivedSample) { + assertNull(it.attachment) + } } @Test fun publisherDeleteWithAttachmentTest() { - val session = Session.open() - var receivedSample: Sample? = null val publisher = session.declarePublisher(keyExpr).res() - val subscriber = session.declareSubscriber(keyExpr).with { sample -> - receivedSample = sample - }.res() + val subscriber = session.declareSubscriber(keyExpr).with { sample -> receivedSample = sample }.res() publisher.delete().withAttachment(attachment).res() - Thread.sleep(500) - subscriber.close() publisher.close() - session.close() + subscriber.close() - assertAttachmentOk(receivedSample!!.attachment!!) + assertAttachmentOk(receivedSample?.attachment) } @Test fun publisherDeleteWithoutAttachmentTest() { - val session = Session.open() - var receivedSample: Sample? = null val publisher = session.declarePublisher(keyExpr).res() - val subscriber = session.declareSubscriber(keyExpr).with { sample -> - receivedSample = sample - }.res() + val subscriber = session.declareSubscriber(keyExpr).with { sample -> receivedSample = sample }.res() publisher.delete().res() - subscriber.close() publisher.close() - session.close() + subscriber.close() - assertNotNull(receivedSample) - assertNull(receivedSample!!.attachment) + assertNotNull(receivedSample) { + assertNull(it.attachment) + } } @Test fun queryWithAttachmentTest() { - val session = Session.open() - var receivedAttachment: Attachment? = null - val queryable = session.declareQueryable(keyExpr).with { query -> receivedAttachment = query.attachment query.reply(keyExpr).success("test").res() }.res() session.get(keyExpr).with {}.withAttachment(attachment).timeout(Duration.ofMillis(1000)).res() - Thread.sleep(1000) queryable.close() - session.close() + assertAttachmentOk(receivedAttachment) } @Test fun queryReplyWithAttachmentTest() { - val session = Session.open() - var receivedAttachment: Attachment? = null - val queryable = session.declareQueryable(keyExpr).with { query -> query.reply(keyExpr).success("test").withAttachment(attachment).res() }.res() session.get(keyExpr).with { reply -> - (reply as Reply.Success) - receivedAttachment = reply.sample.attachment + if (reply is Reply.Success) { + receivedAttachment = reply.sample.attachment + } }.timeout(Duration.ofMillis(1000)).res() - Thread.sleep(1000) - queryable.close() - session.close() + assertAttachmentOk(receivedAttachment) } @Test fun queryReplyWithoutAttachmentTest() { var reply: Reply? = null - val session = Session.open() val queryable = session.declareQueryable(keyExpr).with { query -> query.reply(keyExpr).success("test").res() }.res() @@ -201,21 +187,19 @@ class UserAttachmentTest { reply = it }.timeout(Duration.ofMillis(1000)).res() - Thread.sleep(1000) - queryable.close() - session.close() - assertNotNull(reply) - assertTrue(reply is Reply.Success) - assertNull((reply as Reply.Success).sample.attachment) + assertNotNull(reply) { + assertTrue(it is Reply.Success) + assertNull(it.sample.attachment) + } } @Test fun encodeAndDecodeNumbersTest() { val numbers: List = arrayListOf(0, 1, -1, 12345, -12345, 123567, 123456789, -123456789) - for (number in numbers) { + numbers.forEach { number -> val bytes = number.toByteArray() val decodedNumber: Int = bytes.toInt() assertEquals(number, decodedNumber)