removed embedded broker (#8649)

Also-by: Wouter Born <github@maindrain.net>
Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
Kai Kreuzer
2020-10-04 13:43:44 +02:00
committed by GitHub
parent f24f4971d6
commit 112f46c95e
28 changed files with 44 additions and 1533 deletions

View File

@@ -34,11 +34,6 @@
<artifactId>org.openhab.binding.mqtt.homeassistant</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.io.mqttembeddedbroker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.github.j-n-k</groupId>
<artifactId>moquette-broker</artifactId>

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.mqtt;
/**
* MQTT embedded broker constants
*
* @author David Graeff - Initial contribution
*/
public class Constants {
/**
* The broker connection client ID. You can request the embedded broker connection via the MqttService:
*
* <pre>
* MqttBrokerConnection c = mqttService.getBrokerConnection(Constants.CLIENTID);
* </pre>
*/
public static final String CLIENTID = "embedded-mqtt-broker";
/**
* The broker persistent identifier used for identifying configurations.
*/
public static final String PID = "org.openhab.core.mqttembeddedbroker";
/**
* The configuration key used for configuring the embedded broker port.
*/
public static final String PORT = "port";
}

View File

@@ -27,7 +27,6 @@ import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
import org.openhab.core.io.transport.mqtt.MqttConnectionState;
import org.openhab.core.io.transport.mqtt.MqttService;
import org.openhab.core.io.transport.mqtt.MqttServiceObserver;
import org.openhab.io.mqttembeddedbroker.Constants;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

View File

@@ -34,11 +34,6 @@
<artifactId>org.openhab.binding.mqtt.homie</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.io.mqttembeddedbroker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.github.j-n-k</groupId>
<artifactId>moquette-broker</artifactId>

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.mqtt;
/**
* MQTT embedded broker constants
*
* @author David Graeff - Initial contribution
*/
public class Constants {
/**
* The broker connection client ID. You can request the embedded broker connection via the MqttService:
*
* <pre>
* MqttBrokerConnection c = mqttService.getBrokerConnection(Constants.CLIENTID);
* </pre>
*/
public static final String CLIENTID = "embedded-mqtt-broker";
/**
* The broker persistent identifier used for identifying configurations.
*/
public static final String PID = "org.openhab.core.mqttembeddedbroker";
/**
* The configuration key used for configuring the embedded broker port.
*/
public static final String PORT = "port";
}

View File

@@ -27,7 +27,6 @@ import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
import org.openhab.core.io.transport.mqtt.MqttConnectionState;
import org.openhab.core.io.transport.mqtt.MqttService;
import org.openhab.core.io.transport.mqtt.MqttServiceObserver;
import org.openhab.io.mqttembeddedbroker.Constants;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

View File

@@ -1,13 +0,0 @@
This content is produced and maintained by the openHAB project.
* Project home: https://www.openhab.org
== Declared Project Licenses
This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.
== Source Code
https://github.com/openhab/openhab-addons

View File

@@ -1,78 +0,0 @@
-include: ../itest-common.bndrun
Bundle-SymbolicName: ${project.artifactId}
Fragment-Host: org.openhab.io.mqttembeddedbroker
-runrequires: \
bnd.identity;id='org.openhab.io.mqttembeddedbroker.tests'
# We would like to use the "volatile" storage only
-runblacklist: \
bnd.identity;id='org.openhab.core.storage.json'
-runvm: \
-Dio.netty.noUnsafe=true,\
-Dmqttembeddedbroker.port=${mqttembeddedbroker.port}
#
# done
#
-runbundles: \
ch.qos.logback.core;version='[1.2.3,1.2.4)',\
com.google.gson;version='[2.8.2,2.8.3)',\
com.h2database.mvstore;version='[1.4.199,1.4.200)',\
io.netty.buffer;version='[4.1.42,4.1.43)',\
io.netty.codec;version='[4.1.42,4.1.43)',\
io.netty.codec-mqtt;version='[4.1.42,4.1.43)',\
io.netty.common;version='[4.1.42,4.1.43)',\
io.netty.handler;version='[4.1.42,4.1.43)',\
io.netty.resolver;version='[4.1.42,4.1.43)',\
io.netty.transport;version='[4.1.42,4.1.43)',\
javax.measure.unit-api;version='[1.0.0,1.0.1)',\
org.apache.felix.configadmin;version='[1.9.8,1.9.9)',\
org.apache.felix.http.servlet-api;version='[1.1.2,1.1.3)',\
org.apache.felix.scr;version='[2.1.10,2.1.11)',\
org.eclipse.equinox.event;version='[1.4.300,1.4.301)',\
org.objenesis;version='[2.6.0,2.6.1)',\
org.osgi.service.event;version='[1.4.0,1.4.1)',\
slf4j.api;version='[1.7.25,1.7.26)',\
tec.uom.lib.uom-lib-common;version='[1.0.3,1.0.4)',\
tec.uom.se;version='[1.0.10,1.0.11)',\
ch.qos.logback.classic;version='[1.2.3,1.2.4)',\
biz.aQute.tester.junit-platform;version='[5.1.2,5.1.3)',\
com.google.dagger;version='[2.20.0,2.20.1)',\
com.hivemq.client.mqtt;version='[1.1.2,1.1.3)',\
io.netty.codec-http;version='[4.1.34,4.1.35)',\
io.netty.transport-native-epoll;version='[4.1.34,4.1.35)',\
io.netty.transport-native-unix-common;version='[4.1.34,4.1.35)',\
io.reactivex.rxjava2.rxjava;version='[2.2.5,2.2.6)',\
junit-jupiter-api;version='[5.6.2,5.6.3)',\
junit-jupiter-engine;version='[5.6.2,5.6.3)',\
junit-platform-commons;version='[1.6.2,1.6.3)',\
junit-platform-engine;version='[1.6.2,1.6.3)',\
junit-platform-launcher;version='[1.6.2,1.6.3)',\
net.bytebuddy.byte-buddy;version='[1.10.13,1.10.14)',\
net.bytebuddy.byte-buddy-agent;version='[1.10.13,1.10.14)',\
org.apache.commons.codec;version='[1.10.0,1.10.1)',\
org.eclipse.jetty.http;version='[9.4.20,9.4.21)',\
org.eclipse.jetty.io;version='[9.4.20,9.4.21)',\
org.eclipse.jetty.security;version='[9.4.20,9.4.21)',\
org.eclipse.jetty.server;version='[9.4.20,9.4.21)',\
org.eclipse.jetty.servlet;version='[9.4.20,9.4.21)',\
org.eclipse.jetty.util;version='[9.4.20,9.4.21)',\
org.glassfish.hk2.external.javax.inject;version='[2.4.0,2.4.1)',\
org.hamcrest;version='[2.2.0,2.2.1)',\
org.jctools.core;version='[2.1.2,2.1.3)',\
org.mockito.mockito-core;version='[3.4.6,3.4.7)',\
org.openhab.core;version='[3.0.0,3.0.1)',\
org.openhab.core.config.core;version='[3.0.0,3.0.1)',\
org.openhab.core.io.transport.mqtt;version='[3.0.0,3.0.1)',\
org.openhab.core.test;version='[3.0.0,3.0.1)',\
org.openhab.io.mqttembeddedbroker;version='[3.0.0,3.0.1)',\
org.openhab.io.mqttembeddedbroker.tests;version='[3.0.0,3.0.1)',\
org.opentest4j;version='[1.2.0,1.2.1)',\
org.reactivestreams.reactive-streams;version='[1.0.2,1.0.3)',\
com.sun.xml.bind.jaxb-osgi;version='[2.3.3,2.3.4)',\
jakarta.xml.bind-api;version='[2.3.3,2.3.4)',\
org.glassfish.hk2.osgi-resource-locator;version='[1.0.1,1.0.2)',\
org.apache.servicemix.specs.activation-api-1.2.1;version='[1.2.1,1.2.2)'

View File

@@ -1,92 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.openhab.addons.itests</groupId>
<artifactId>org.openhab.addons.reactor.itests</artifactId>
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>org.openhab.io.mqttembeddedbroker.tests</artifactId>
<name>openHAB Add-ons :: Integration Tests :: MQTT Embeddedbroker</name>
<properties>
<netty.version>4.1.42.Final</netty.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.io.mqttembeddedbroker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2-mvstore</artifactId>
<version>1.4.199</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-mqtt</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-resolver</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>${netty.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>mqttembeddedbroker.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,122 +0,0 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.io.mqttembeddedbroker;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
import org.openhab.core.io.transport.mqtt.MqttConnectionState;
import org.openhab.core.io.transport.mqtt.MqttService;
import org.openhab.core.io.transport.mqtt.MqttServiceObserver;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
/**
* A full implementation test, that starts the embedded MQTT broker and publishes a homeassistant MQTT discovery device
* tree.
*
* @author David Graeff - Initial contribution
* @author Wouter Born - Support running MQTT itests in parallel by reconfiguring embedded broker port
*/
@NonNullByDefault
public class EmbeddedBrokerTools {
private static final int BROKER_PORT = Integer.getInteger("mqttembeddedbroker.port", 1883);
private final ConfigurationAdmin configurationAdmin;
private final MqttService mqttService;
public @Nullable MqttBrokerConnection embeddedConnection;
public EmbeddedBrokerTools(ConfigurationAdmin configurationAdmin, MqttService mqttService) {
this.configurationAdmin = configurationAdmin;
this.mqttService = mqttService;
}
/**
* Request the embedded broker connection from the {@link MqttService} and wait for a connection to be established.
*
* @throws InterruptedException
* @throws IOException
*/
public MqttBrokerConnection waitForConnection() throws InterruptedException, IOException {
reconfigurePort();
embeddedConnection = mqttService.getBrokerConnection(Constants.CLIENTID);
if (embeddedConnection == null) {
Semaphore semaphore = new Semaphore(1);
semaphore.acquire();
MqttServiceObserver observer = new MqttServiceObserver() {
@Override
public void brokerAdded(String brokerID, MqttBrokerConnection broker) {
if (brokerID.equals(Constants.CLIENTID)) {
embeddedConnection = broker;
semaphore.release();
}
}
@Override
public void brokerRemoved(String brokerID, MqttBrokerConnection broker) {
}
};
mqttService.addBrokersListener(observer);
assertTrue(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Wait for embedded connection client failed");
}
MqttBrokerConnection embeddedConnection = this.embeddedConnection;
if (embeddedConnection == null) {
throw new IllegalStateException();
}
Semaphore semaphore = new Semaphore(1);
semaphore.acquire();
MqttConnectionObserver mqttConnectionObserver = (state, error) -> {
if (state == MqttConnectionState.CONNECTED) {
semaphore.release();
}
};
embeddedConnection.addConnectionObserver(mqttConnectionObserver);
if (embeddedConnection.connectionState() == MqttConnectionState.CONNECTED) {
semaphore.release();
}
assertTrue(semaphore.tryAcquire(5, TimeUnit.SECONDS), "Connection " + embeddedConnection.getClientId()
+ " failed. State: " + embeddedConnection.connectionState());
return embeddedConnection;
}
public void reconfigurePort() throws IOException {
Configuration configuration = configurationAdmin.getConfiguration(Constants.PID, null);
Dictionary<String, Object> properties = configuration.getProperties();
if (properties == null) {
properties = new Hashtable<>();
}
Integer currentPort = (Integer) properties.get(Constants.PORT);
if (currentPort == null || currentPort.intValue() != BROKER_PORT) {
properties.put(Constants.PORT, BROKER_PORT);
configuration.update(properties);
// Remove the connection to make sure the test waits for the new connection to become available
mqttService.removeBrokerConnection(Constants.CLIENTID);
}
}
}

View File

@@ -1,166 +0,0 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.io.mqttembeddedbroker;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.MockitoAnnotations.openMocks;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
import org.openhab.core.io.transport.mqtt.MqttConnectionState;
import org.openhab.core.io.transport.mqtt.MqttService;
import org.openhab.core.test.java.JavaOSGiTest;
import org.osgi.service.cm.ConfigurationAdmin;
/**
* Moquette test
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class MoquetteTest extends JavaOSGiTest {
private static final String TEST_TOPIC = "testtopic";
private @NonNullByDefault({}) AutoCloseable mocksCloseable;
private @NonNullByDefault({}) ConfigurationAdmin configurationAdmin;
private @NonNullByDefault({}) MqttService mqttService;
private @NonNullByDefault({}) MqttBrokerConnection embeddedConnection;
private @NonNullByDefault({}) MqttBrokerConnection clientConnection;
/**
* Create an observer that fails the test as soon as the broker client connection changes its connection state
* to something else then CONNECTED.
*/
private MqttConnectionObserver failIfChange = (state, error) -> assertThat(state,
is(MqttConnectionState.CONNECTED));
@BeforeEach
public void beforeEach() throws Exception {
registerVolatileStorageService();
mocksCloseable = openMocks(this);
configurationAdmin = getService(ConfigurationAdmin.class);
mqttService = getService(MqttService.class);
// Wait for the EmbeddedBrokerService internal connection to be connected
embeddedConnection = new EmbeddedBrokerTools(configurationAdmin, mqttService).waitForConnection();
embeddedConnection.setQos(1);
clientConnection = new MqttBrokerConnection(embeddedConnection.getHost(), embeddedConnection.getPort(),
embeddedConnection.isSecure(), "client");
clientConnection.setQos(1);
clientConnection.start().get(500, TimeUnit.MILLISECONDS);
assertThat(clientConnection.connectionState(), is(MqttConnectionState.CONNECTED));
// If the connection state changes in between -> fail
clientConnection.addConnectionObserver(failIfChange);
}
@AfterEach
public void afterEach() throws Exception {
if (clientConnection != null) {
clientConnection.removeConnectionObserver(failIfChange);
clientConnection.stop().get(500, TimeUnit.MILLISECONDS);
}
mocksCloseable.close();
}
private CompletableFuture<Boolean> publish(String topic, String message) {
return embeddedConnection.publish(topic, message.getBytes(StandardCharsets.UTF_8), 0, true);
}
@Test
public void singleTopic() throws InterruptedException, ExecutionException, TimeoutException {
List<CompletableFuture<Boolean>> futures = new ArrayList<>();
futures.add(publish(TEST_TOPIC, "testPayload"));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(1000, TimeUnit.MILLISECONDS);
CountDownLatch c = new CountDownLatch(1);
futures.clear();
futures.add(clientConnection.subscribe(TEST_TOPIC, (topic, payload) -> c.countDown()));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(1000, TimeUnit.MILLISECONDS);
assertTrue(c.await(1000, TimeUnit.MILLISECONDS));
}
@Test
public void multipleTopicsWithSingleSubscription()
throws InterruptedException, ExecutionException, TimeoutException {
List<CompletableFuture<Boolean>> futures = new ArrayList<>();
futures.add(publish(TEST_TOPIC + "/1", "testPayload1"));
futures.add(publish(TEST_TOPIC + "/2", "testPayload2"));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(1000, TimeUnit.MILLISECONDS);
CountDownLatch c = new CountDownLatch(2);
futures.clear();
futures.add(clientConnection.subscribe(TEST_TOPIC + "/1", (topic, payload) -> c.countDown()));
futures.add(clientConnection.subscribe(TEST_TOPIC + "/2", (topic, payload) -> c.countDown()));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(1000, TimeUnit.MILLISECONDS);
assertTrue(c.await(1000, TimeUnit.MILLISECONDS));
}
@Test
public void multipleTopicsWithHashWildcardSubscription()
throws InterruptedException, ExecutionException, TimeoutException {
List<CompletableFuture<Boolean>> futures = new ArrayList<>();
futures.add(publish(TEST_TOPIC + "/1", "testPayload1"));
futures.add(publish(TEST_TOPIC + "/2", "testPayload2"));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(1000, TimeUnit.MILLISECONDS);
CountDownLatch c = new CountDownLatch(2);
futures.clear();
futures.add(clientConnection.subscribe(TEST_TOPIC + "/#", (topic, payload) -> c.countDown()));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(1000, TimeUnit.MILLISECONDS);
assertTrue(c.await(1000, TimeUnit.MILLISECONDS));
}
@Test
public void multipleTopicsWithPlusWildcardSubscription()
throws InterruptedException, ExecutionException, TimeoutException {
List<CompletableFuture<Boolean>> futures = new ArrayList<>();
futures.add(publish(TEST_TOPIC + "/1", "testPayload1"));
futures.add(publish(TEST_TOPIC + "/2", "testPayload2"));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(1000, TimeUnit.MILLISECONDS);
CountDownLatch c = new CountDownLatch(2);
futures.clear();
futures.add(clientConnection.subscribe(TEST_TOPIC + "/+", (topic, payload) -> c.countDown()));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(1000, TimeUnit.MILLISECONDS);
assertTrue(c.await(1000, TimeUnit.MILLISECONDS));
}
}

View File

@@ -23,14 +23,15 @@
<module>org.openhab.binding.hue.tests</module>
<module>org.openhab.binding.max.tests</module>
<module>org.openhab.binding.modbus.tests</module>
<module>org.openhab.binding.mqtt.homeassistant.tests</module>
<module>org.openhab.binding.mqtt.homie.tests</module>
<!-- MQTT tests need to be refactored to not use the embedded broker bundle anymore
<module>org.openhab.binding.mqtt.homeassistant.tests</module>
<module>org.openhab.binding.mqtt.homie.tests</module>
-->
<module>org.openhab.binding.nest.tests</module>
<module>org.openhab.binding.ntp.tests</module>
<module>org.openhab.binding.systeminfo.tests</module>
<module>org.openhab.binding.tradfri.tests</module>
<module>org.openhab.binding.wemo.tests</module>
<module>org.openhab.io.mqttembeddedbroker.tests</module>
<module>org.openhab.persistence.mapdb.tests</module>
</modules>