From 5c8def0d25bfc15715b243e8eb2b846db58e17f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E9=AD=81?= <670569467@qq.com> Date: Thu, 14 Sep 2023 09:38:48 +0800 Subject: [PATCH] [ISSUE #11072]Enhance unit tests related to trace events. (#11105) --- .../notify/listener/SmartSubscriber.java | 2 +- .../naming/DeregisterInstanceTraceEvent.java | 5 +- .../naming/DeregisterServiceTraceEvent.java | 5 +- .../naming/HealthStateChangeTraceEvent.java | 15 +- .../trace/event/naming/NamingTraceEvent.java | 6 +- .../event/naming/PushServiceTraceEvent.java | 5 +- .../naming/RegisterInstanceTraceEvent.java | 5 +- .../naming/RegisterServiceTraceEvent.java | 4 +- .../naming/SubscribeServiceTraceEvent.java | 5 +- .../naming/UnsubscribeServiceTraceEvent.java | 5 +- .../naming/UpdateInstanceTraceEvent.java | 4 + .../event/naming/UpdateServiceTraceEvent.java | 4 + .../NacosCombinedTraceSubscriberTest.java | 176 +++++++++++++----- 13 files changed, 173 insertions(+), 68 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/notify/listener/SmartSubscriber.java b/common/src/main/java/com/alibaba/nacos/common/notify/listener/SmartSubscriber.java index 8ce72b3a5..3d79f2a00 100644 --- a/common/src/main/java/com/alibaba/nacos/common/notify/listener/SmartSubscriber.java +++ b/common/src/main/java/com/alibaba/nacos/common/notify/listener/SmartSubscriber.java @@ -27,7 +27,7 @@ import java.util.List; * @author zongtanghu */ @SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule") -public abstract class SmartSubscriber extends Subscriber { +public abstract class SmartSubscriber extends Subscriber { /** * Returns which event type are smart subscriber interested in. diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/DeregisterInstanceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/DeregisterInstanceTraceEvent.java index 96676ee7e..2a25e0dca 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/DeregisterInstanceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/DeregisterInstanceTraceEvent.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.common.trace.DeregisterInstanceReason; /** * Naming deregister instance trace event. + * * @author yanda */ public class DeregisterInstanceTraceEvent extends NamingTraceEvent { @@ -30,9 +31,9 @@ public class DeregisterInstanceTraceEvent extends NamingTraceEvent { private final boolean rpc; - private String instanceIp; + private final String instanceIp; - private int instancePort; + private final int instancePort; public final DeregisterInstanceReason reason; diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/DeregisterServiceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/DeregisterServiceTraceEvent.java index 617c97d7a..2ad345778 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/DeregisterServiceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/DeregisterServiceTraceEvent.java @@ -18,14 +18,15 @@ package com.alibaba.nacos.common.trace.event.naming; /** * Naming deregister service trace event. + * * @author yanda */ public class DeregisterServiceTraceEvent extends NamingTraceEvent { private static final long serialVersionUID = 7358195336881398548L; - public DeregisterServiceTraceEvent(long eventTime, String serviceNamespace, - String serviceGroup, String serviceName) { + public DeregisterServiceTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, + String serviceName) { super("DEREGISTER_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); } } diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/HealthStateChangeTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/HealthStateChangeTraceEvent.java index bb90e97bf..8bac9b9dd 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/HealthStateChangeTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/HealthStateChangeTraceEvent.java @@ -20,21 +20,22 @@ import com.alibaba.nacos.common.trace.HealthCheckType; /** * Naming instance health state change trace event. + * * @author yanda */ public class HealthStateChangeTraceEvent extends NamingTraceEvent { private static final long serialVersionUID = 6966396191118694597L; - private String instanceIp; + private final String instanceIp; - private int instancePort; + private final int instancePort; - private boolean isHealthy; + private final boolean isHealthy; - private HealthCheckType healthCheckType; + private final HealthCheckType healthCheckType; - private String healthStateChangeReason; + private final String healthStateChangeReason; public String getInstanceIp() { return instanceIp; @@ -60,8 +61,8 @@ public class HealthStateChangeTraceEvent extends NamingTraceEvent { return healthStateChangeReason; } - public HealthStateChangeTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, - String serviceName, String instanceIp, int instancePort, boolean isHealthy, String healthStateChangeReason) { + public HealthStateChangeTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, String serviceName, + String instanceIp, int instancePort, boolean isHealthy, String healthStateChangeReason) { super("HEALTH_STATE_CHANGE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); this.instanceIp = instanceIp; this.instancePort = instancePort; diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/NamingTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/NamingTraceEvent.java index 7aa1d8877..93276e4aa 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/NamingTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/NamingTraceEvent.java @@ -24,11 +24,11 @@ import com.alibaba.nacos.common.trace.event.TraceEvent; * @author yanda */ public class NamingTraceEvent extends TraceEvent { - + private static final long serialVersionUID = 2923077640400851816L; - public NamingTraceEvent(String eventType, long eventTime, - String serviceNamespace, String serviceGroup, String name) { + public NamingTraceEvent(String eventType, long eventTime, String serviceNamespace, String serviceGroup, + String name) { super(eventType, eventTime, serviceNamespace, serviceGroup, name); } diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/PushServiceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/PushServiceTraceEvent.java index aca60c172..8d692d465 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/PushServiceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/PushServiceTraceEvent.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.common.trace.event.naming; /** * Naming push service trace event. + * * @author yanda */ public class PushServiceTraceEvent extends NamingTraceEvent { @@ -55,8 +56,8 @@ public class PushServiceTraceEvent extends NamingTraceEvent { } public PushServiceTraceEvent(long eventTime, long pushCostTimeForNetWork, long pushCostTimeForAll, - long serviceLevelAgreementTime, String clientIp, String serviceNamespace, - String serviceGroup, String serviceName, int instanceSize) { + long serviceLevelAgreementTime, String clientIp, String serviceNamespace, String serviceGroup, + String serviceName, int instanceSize) { super("PUSH_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); this.clientIp = clientIp; this.instanceSize = instanceSize; diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/RegisterInstanceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/RegisterInstanceTraceEvent.java index e579e7644..8374d765d 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/RegisterInstanceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/RegisterInstanceTraceEvent.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.common.trace.event.naming; /** * Naming register instance trace event. + * * @author yanda */ public class RegisterInstanceTraceEvent extends NamingTraceEvent { @@ -28,9 +29,9 @@ public class RegisterInstanceTraceEvent extends NamingTraceEvent { private final boolean rpc; - private String instanceIp; + private final String instanceIp; - private int instancePort; + private final int instancePort; public String getClientIp() { return clientIp; diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/RegisterServiceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/RegisterServiceTraceEvent.java index c123f6516..8823bae63 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/RegisterServiceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/RegisterServiceTraceEvent.java @@ -18,14 +18,14 @@ package com.alibaba.nacos.common.trace.event.naming; /** * Naming deregister service trace event. + * * @author yanda */ public class RegisterServiceTraceEvent extends NamingTraceEvent { private static final long serialVersionUID = -8568231862586636388L; - public RegisterServiceTraceEvent(long eventTime, String serviceNamespace, - String serviceGroup, String serviceName) { + public RegisterServiceTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, String serviceName) { super("REGISTER_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); } } diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/SubscribeServiceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/SubscribeServiceTraceEvent.java index a8c987cb8..b3a2f5692 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/SubscribeServiceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/SubscribeServiceTraceEvent.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.common.trace.event.naming; /** * Naming subscribe service trace event. + * * @author yanda */ public class SubscribeServiceTraceEvent extends NamingTraceEvent { @@ -30,8 +31,8 @@ public class SubscribeServiceTraceEvent extends NamingTraceEvent { return clientIp; } - public SubscribeServiceTraceEvent(long eventTime, String clientIp, String serviceNamespace, - String serviceGroup, String serviceName) { + public SubscribeServiceTraceEvent(long eventTime, String clientIp, String serviceNamespace, String serviceGroup, + String serviceName) { super("SUBSCRIBE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); this.clientIp = clientIp; } diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UnsubscribeServiceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UnsubscribeServiceTraceEvent.java index 0fc195dfd..7f7f664c4 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UnsubscribeServiceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UnsubscribeServiceTraceEvent.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.common.trace.event.naming; /** * Naming unsubscribe service trace event. + * * @author yanda */ public class UnsubscribeServiceTraceEvent extends NamingTraceEvent { @@ -30,8 +31,8 @@ public class UnsubscribeServiceTraceEvent extends NamingTraceEvent { return clientIp; } - public UnsubscribeServiceTraceEvent(long eventTime, String clientIp, String serviceNamespace, - String serviceGroup, String serviceName) { + public UnsubscribeServiceTraceEvent(long eventTime, String clientIp, String serviceNamespace, String serviceGroup, + String serviceName) { super("UNSUBSCRIBE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); this.clientIp = clientIp; } diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UpdateInstanceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UpdateInstanceTraceEvent.java index c9a08a097..14bf57c30 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UpdateInstanceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UpdateInstanceTraceEvent.java @@ -36,6 +36,10 @@ public class UpdateInstanceTraceEvent extends NamingTraceEvent { private final int instancePort; + public Map getMetadata() { + return metadata; + } + public String getClientIp() { return clientIp; } diff --git a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UpdateServiceTraceEvent.java b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UpdateServiceTraceEvent.java index 91858bac0..2d83c5024 100644 --- a/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UpdateServiceTraceEvent.java +++ b/common/src/main/java/com/alibaba/nacos/common/trace/event/naming/UpdateServiceTraceEvent.java @@ -30,6 +30,10 @@ public class UpdateServiceTraceEvent extends NamingTraceEvent { private final Map metadata; + public Map getMetadata() { + return metadata; + } + public UpdateServiceTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, String serviceName, Map metadata) { super("UPDATE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); diff --git a/core/src/test/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriberTest.java b/core/src/test/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriberTest.java index 77007df85..80ec05fd6 100644 --- a/core/src/test/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriberTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/trace/NacosCombinedTraceSubscriberTest.java @@ -20,8 +20,16 @@ import com.alibaba.nacos.common.notify.Event; import com.alibaba.nacos.common.trace.DeregisterInstanceReason; import com.alibaba.nacos.common.trace.event.TraceEvent; import com.alibaba.nacos.common.trace.event.naming.DeregisterInstanceTraceEvent; +import com.alibaba.nacos.common.trace.event.naming.DeregisterServiceTraceEvent; +import com.alibaba.nacos.common.trace.event.naming.HealthStateChangeTraceEvent; import com.alibaba.nacos.common.trace.event.naming.NamingTraceEvent; +import com.alibaba.nacos.common.trace.event.naming.PushServiceTraceEvent; import com.alibaba.nacos.common.trace.event.naming.RegisterInstanceTraceEvent; +import com.alibaba.nacos.common.trace.event.naming.RegisterServiceTraceEvent; +import com.alibaba.nacos.common.trace.event.naming.SubscribeServiceTraceEvent; +import com.alibaba.nacos.common.trace.event.naming.UnsubscribeServiceTraceEvent; +import com.alibaba.nacos.common.trace.event.naming.UpdateInstanceTraceEvent; +import com.alibaba.nacos.common.trace.event.naming.UpdateServiceTraceEvent; import com.alibaba.nacos.plugin.trace.NacosTracePluginManager; import com.alibaba.nacos.plugin.trace.spi.NacosTraceSubscriber; import org.junit.After; @@ -29,12 +37,9 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.invocation.InvocationOnMock; import org.mockito.junit.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; import org.springframework.test.util.ReflectionTestUtils; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -47,85 +52,170 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@SuppressWarnings("all") @RunWith(MockitoJUnitRunner.class) public class NacosCombinedTraceSubscriberTest { @Mock - private NacosTraceSubscriber mockSubscriber; + private NacosTraceSubscriber mockServiceSubscriber; @Mock - private NacosTraceSubscriber mockSubscriber2; + private NacosTraceSubscriber mockInstanceSubscriber; + + @Mock + private NacosTraceSubscriber mockOtherSubscriber; private NacosCombinedTraceSubscriber combinedTraceSubscriber; + @SuppressWarnings("unchecked") + private Map getTraceSubscribers() { + return (Map) ReflectionTestUtils.getField(NacosTracePluginManager.getInstance(), + "traceSubscribers"); + } + @Before public void setUp() throws Exception { - Map traceSubscribers = (Map) ReflectionTestUtils - .getField(NacosTracePluginManager.getInstance(), "traceSubscribers"); - traceSubscribers.put("nacos-combined", mockSubscriber); - traceSubscribers.put("nacos-combined2", mockSubscriber2); - List> testEvents = new LinkedList<>(); - testEvents.add(RegisterInstanceTraceEvent.class); - testEvents.add(DeregisterInstanceTraceEvent.class); - testEvents.add(TraceEvent.class); - when(mockSubscriber.subscribeTypes()).thenReturn(testEvents); - when(mockSubscriber2.subscribeTypes()).thenReturn(Collections.singletonList(RegisterInstanceTraceEvent.class)); + Map traceSubscribers = getTraceSubscribers(); + traceSubscribers.put("instanceSubscriber", mockInstanceSubscriber); + traceSubscribers.put("serviceSubscriber", mockServiceSubscriber); + traceSubscribers.put("otherSubscriber", mockOtherSubscriber); + // Initialization instance related. + List> instanceEvents = new LinkedList<>(); + instanceEvents.add(RegisterInstanceTraceEvent.class); + instanceEvents.add(DeregisterInstanceTraceEvent.class); + instanceEvents.add(UpdateInstanceTraceEvent.class); + // Initialization service related. + List> serviceEvents = new LinkedList<>(); + serviceEvents.add(PushServiceTraceEvent.class); + serviceEvents.add(RegisterServiceTraceEvent.class); + serviceEvents.add(DeregisterServiceTraceEvent.class); + serviceEvents.add(SubscribeServiceTraceEvent.class); + serviceEvents.add(UnsubscribeServiceTraceEvent.class); + serviceEvents.add(UpdateServiceTraceEvent.class); + // Initialization other related. + List> otherEvents = new LinkedList<>(); + otherEvents.add(HealthStateChangeTraceEvent.class); + otherEvents.add(TraceEvent.class); + when(mockServiceSubscriber.subscribeTypes()).thenReturn(serviceEvents); + when(mockInstanceSubscriber.subscribeTypes()).thenReturn(instanceEvents); + when(mockOtherSubscriber.subscribeTypes()).thenReturn(otherEvents); combinedTraceSubscriber = new NacosCombinedTraceSubscriber(NamingTraceEvent.class); } @After public void tearDown() throws Exception { - Map traceSubscribers = (Map) ReflectionTestUtils - .getField(NacosTracePluginManager.getInstance(), "traceSubscribers"); - traceSubscribers.remove("nacos-combined"); - traceSubscribers.remove("nacos-combined2"); + Map traceSubscribers = getTraceSubscribers(); + traceSubscribers.remove("serviceSubscriber"); + traceSubscribers.remove("instanceSubscriber"); + traceSubscribers.remove("otherSubscriber"); combinedTraceSubscriber.shutdown(); } @Test public void testSubscribeTypes() { List> actual = combinedTraceSubscriber.subscribeTypes(); - assertEquals(2, actual.size()); + assertEquals(10, actual.size()); assertTrue(actual.contains(RegisterInstanceTraceEvent.class)); assertTrue(actual.contains(DeregisterInstanceTraceEvent.class)); + assertTrue(actual.contains(UpdateInstanceTraceEvent.class)); + assertTrue(actual.contains(RegisterServiceTraceEvent.class)); + assertTrue(actual.contains(DeregisterServiceTraceEvent.class)); + assertTrue(actual.contains(SubscribeServiceTraceEvent.class)); + assertTrue(actual.contains(UnsubscribeServiceTraceEvent.class)); + assertTrue(actual.contains(UpdateServiceTraceEvent.class)); + assertTrue(actual.contains(PushServiceTraceEvent.class)); + assertTrue(actual.contains(HealthStateChangeTraceEvent.class)); } @Test public void testOnEvent() { - RegisterInstanceTraceEvent event = new RegisterInstanceTraceEvent(1L, "", true, "", "", "", "", 1); - doThrow(new RuntimeException("test")).when(mockSubscriber2).onEvent(event); - combinedTraceSubscriber.onEvent(event); - verify(mockSubscriber).onEvent(event); - verify(mockSubscriber2).onEvent(event); - DeregisterInstanceTraceEvent event1 = new DeregisterInstanceTraceEvent(1L, "", true, + // Test RegisterInstanceTraceEvent. + RegisterInstanceTraceEvent registerInstanceTraceEvent = new RegisterInstanceTraceEvent(1L, "", true, "", "", "", + "", 1); + doThrow(new RuntimeException("test")).when(mockInstanceSubscriber).onEvent(registerInstanceTraceEvent); + combinedTraceSubscriber.onEvent(registerInstanceTraceEvent); + verify(mockInstanceSubscriber, times(1)).onEvent(registerInstanceTraceEvent); + verify(mockServiceSubscriber, never()).onEvent(registerInstanceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(registerInstanceTraceEvent); + // Test DeregisterInstanceTraceEvent. + DeregisterInstanceTraceEvent deregisterInstanceTraceEvent = new DeregisterInstanceTraceEvent(1L, "", true, DeregisterInstanceReason.REQUEST, "", "", "", "", 1); - combinedTraceSubscriber.onEvent(event1); - verify(mockSubscriber).onEvent(event1); - verify(mockSubscriber2, never()).onEvent(event1); - TraceEvent event2 = new TraceEvent("", 1L, "", "", ""); - combinedTraceSubscriber.onEvent(event2); - verify(mockSubscriber, never()).onEvent(event2); - verify(mockSubscriber2, never()).onEvent(event2); + combinedTraceSubscriber.onEvent(deregisterInstanceTraceEvent); + verify(mockInstanceSubscriber, times(1)).onEvent(deregisterInstanceTraceEvent); + verify(mockServiceSubscriber, never()).onEvent(deregisterInstanceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(deregisterInstanceTraceEvent); + // Test UpdateInstanceTraceEvent. + UpdateInstanceTraceEvent updateInstanceTraceEvent = new UpdateInstanceTraceEvent(1L, "", "", "", "", "", 123, + null); + combinedTraceSubscriber.onEvent(updateInstanceTraceEvent); + verify(mockInstanceSubscriber, times(1)).onEvent(updateInstanceTraceEvent); + verify(mockServiceSubscriber, never()).onEvent(updateInstanceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(updateInstanceTraceEvent); + // Test RegisterServiceTraceEvent. + RegisterServiceTraceEvent registerServiceTraceEvent = new RegisterServiceTraceEvent(1L, "", "", ""); + combinedTraceSubscriber.onEvent(registerServiceTraceEvent); + verify(mockInstanceSubscriber, never()).onEvent(registerServiceTraceEvent); + verify(mockServiceSubscriber, times(1)).onEvent(registerServiceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(registerServiceTraceEvent); + // Test DeregisterServiceTraceEvent. + DeregisterServiceTraceEvent deregisterServiceTraceEvent = new DeregisterServiceTraceEvent(1L, "", "", ""); + combinedTraceSubscriber.onEvent(deregisterServiceTraceEvent); + verify(mockInstanceSubscriber, never()).onEvent(deregisterServiceTraceEvent); + verify(mockServiceSubscriber, times(1)).onEvent(deregisterServiceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(deregisterServiceTraceEvent); + // Test SubscribeServiceTraceEvent. + SubscribeServiceTraceEvent subscribeServiceTraceEvent = new SubscribeServiceTraceEvent(1L, "", "", "", ""); + combinedTraceSubscriber.onEvent(subscribeServiceTraceEvent); + verify(mockInstanceSubscriber, never()).onEvent(subscribeServiceTraceEvent); + verify(mockServiceSubscriber, times(1)).onEvent(subscribeServiceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(subscribeServiceTraceEvent); + // Test UnsubscribeServiceTraceEvent. + UnsubscribeServiceTraceEvent unsubscribeServiceTraceEvent = new UnsubscribeServiceTraceEvent(1L, "", "", "", + ""); + combinedTraceSubscriber.onEvent(unsubscribeServiceTraceEvent); + verify(mockInstanceSubscriber, never()).onEvent(unsubscribeServiceTraceEvent); + verify(mockServiceSubscriber, times(1)).onEvent(unsubscribeServiceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(unsubscribeServiceTraceEvent); + // Test UpdateServiceTraceEvent. + UpdateServiceTraceEvent updateServiceTraceEvent = new UpdateServiceTraceEvent(1L, "", "", "", null); + combinedTraceSubscriber.onEvent(updateServiceTraceEvent); + verify(mockInstanceSubscriber, never()).onEvent(updateServiceTraceEvent); + verify(mockServiceSubscriber, times(1)).onEvent(updateServiceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(updateServiceTraceEvent); + // Test PushServiceTraceEvent. + PushServiceTraceEvent pushServiceTraceEvent = new PushServiceTraceEvent(1L, 1L, 1L, 1L, "", "", "", "", 1); + combinedTraceSubscriber.onEvent(pushServiceTraceEvent); + verify(mockInstanceSubscriber, never()).onEvent(pushServiceTraceEvent); + verify(mockServiceSubscriber, times(1)).onEvent(pushServiceTraceEvent); + verify(mockOtherSubscriber, never()).onEvent(pushServiceTraceEvent); + // Test HealthStateChangeTraceEvent. + HealthStateChangeTraceEvent healthStateChangeTraceEvent = new HealthStateChangeTraceEvent(1L, "", "", "", "", + 8867, true, ""); + combinedTraceSubscriber.onEvent(healthStateChangeTraceEvent); + verify(mockInstanceSubscriber, never()).onEvent(healthStateChangeTraceEvent); + verify(mockServiceSubscriber, never()).onEvent(healthStateChangeTraceEvent); + verify(mockOtherSubscriber, times(1)).onEvent(healthStateChangeTraceEvent); + // Test TraceEvent. + TraceEvent traceEvent = new TraceEvent("", 1L, "", "", ""); + combinedTraceSubscriber.onEvent(traceEvent); + verify(mockInstanceSubscriber, never()).onEvent(traceEvent); + verify(mockServiceSubscriber, never()).onEvent(traceEvent); + verify(mockOtherSubscriber, never()).onEvent(traceEvent); } @Test public void testOnEventWithExecutor() { Executor executor = mock(Executor.class); - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) throws Throwable { - invocationOnMock.getArgument(0, Runnable.class).run(); - return null; - } + doAnswer(invocationOnMock -> { + invocationOnMock.getArgument(0, Runnable.class).run(); + return null; }).when(executor).execute(any(Runnable.class)); - when(mockSubscriber.executor()).thenReturn(executor); + when(mockInstanceSubscriber.executor()).thenReturn(executor); RegisterInstanceTraceEvent event = new RegisterInstanceTraceEvent(1L, "", true, "", "", "", "", 1); combinedTraceSubscriber.onEvent(event); - verify(mockSubscriber).onEvent(event); - verify(mockSubscriber2).onEvent(event); + verify(mockInstanceSubscriber).onEvent(event); } }