-
-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always use ThingUid as the unique identifier for thing status metrics (…
…#3674) * Always use ThingUid as the unique identifier for thing status metrics At bind time, ThingId was being used, but then ThingUid was being used when changes occured, causing multiple meters to be created for the same Thing, with the status always being 0 for the meter created at bind time. This change uses the full ThingUid, for both binding and event processing, since ThingUid is the globally unique value. Fixes #3672 Signed-off-by: Scott Hraban <[email protected]>
- Loading branch information
1 parent
db97610
commit 505e512
Showing
2 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...itor/src/test/java/org/openhab/core/io/monitor/internal/metrics/ThingStateMetricTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.core.io.monitor.internal.metrics; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.openhab.core.thing.Thing; | ||
import org.openhab.core.thing.ThingRegistry; | ||
import org.openhab.core.thing.ThingStatus; | ||
import org.openhab.core.thing.ThingStatusDetail; | ||
import org.openhab.core.thing.ThingStatusInfo; | ||
import org.openhab.core.thing.ThingTypeUID; | ||
import org.openhab.core.thing.ThingUID; | ||
import org.openhab.core.thing.events.ThingEventFactory; | ||
import org.openhab.core.thing.internal.ThingImpl; | ||
import org.osgi.framework.BundleContext; | ||
|
||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
|
||
/** | ||
* Tests for ThingStateMetric class | ||
* | ||
* @author Scott Hraban - Initial contribution | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
@NonNullByDefault | ||
public class ThingStateMetricTest { | ||
|
||
@Test | ||
public void testThingUidAlwaysUsedToCreateMeter() { | ||
final String strThingTypeUid = "sonos:Amp"; | ||
|
||
final String strThingUid = strThingTypeUid + ":RINCON_347E5C0D150501400"; | ||
ThingUID thingUid = new ThingUID(strThingUid); | ||
Thing thing = new ThingImpl(new ThingTypeUID(strThingTypeUid), thingUid); | ||
|
||
final String strThingUid2 = strThingTypeUid + ":foo"; | ||
ThingUID thingUid2 = new ThingUID(strThingUid2); | ||
|
||
ThingRegistry thingRegistry = mock(ThingRegistry.class); | ||
|
||
SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry(); | ||
|
||
ThingStateMetric thingStateMetric = new ThingStateMetric(mock(BundleContext.class), thingRegistry, | ||
new HashSet<Tag>()); | ||
|
||
// Only one meter registered at bind time | ||
doReturn(Collections.singleton(thing)).when(thingRegistry).getAll(); | ||
thingStateMetric.bindTo(meterRegistry); | ||
|
||
List<Meter> meters = meterRegistry.getMeters(); | ||
assertEquals(1, meters.size()); | ||
assertEquals(strThingUid, meters.get(0).getId().getTag("thing")); | ||
|
||
// Still only one meter registered after receiving an event | ||
ThingStatusInfo thingStatusInfo = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null); | ||
thingStateMetric.receive(ThingEventFactory.createStatusInfoEvent(thingUid, thingStatusInfo)); | ||
|
||
meters = meterRegistry.getMeters(); | ||
assertEquals(1, meters.size()); | ||
assertEquals(strThingUid, meters.get(0).getId().getTag("thing")); | ||
|
||
// Now another one is added | ||
thingStateMetric.receive(ThingEventFactory.createStatusInfoEvent(thingUid2, thingStatusInfo)); | ||
|
||
meters = meterRegistry.getMeters(); | ||
assertEquals(2, meters.size()); | ||
} | ||
} |