[ISSUE #11072]Enhance unit tests related to trace events. (#11105)

This commit is contained in:
阿魁 2023-09-14 09:38:48 +08:00 committed by GitHub
parent a170489667
commit 5c8def0d25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 173 additions and 68 deletions

View File

@ -27,7 +27,7 @@ import java.util.List;
* @author zongtanghu * @author zongtanghu
*/ */
@SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule") @SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule")
public abstract class SmartSubscriber extends Subscriber { public abstract class SmartSubscriber extends Subscriber<Event> {
/** /**
* Returns which event type are smart subscriber interested in. * Returns which event type are smart subscriber interested in.

View File

@ -20,6 +20,7 @@ import com.alibaba.nacos.common.trace.DeregisterInstanceReason;
/** /**
* Naming deregister instance trace event. * Naming deregister instance trace event.
*
* @author yanda * @author yanda
*/ */
public class DeregisterInstanceTraceEvent extends NamingTraceEvent { public class DeregisterInstanceTraceEvent extends NamingTraceEvent {
@ -30,9 +31,9 @@ public class DeregisterInstanceTraceEvent extends NamingTraceEvent {
private final boolean rpc; private final boolean rpc;
private String instanceIp; private final String instanceIp;
private int instancePort; private final int instancePort;
public final DeregisterInstanceReason reason; public final DeregisterInstanceReason reason;

View File

@ -18,14 +18,15 @@ package com.alibaba.nacos.common.trace.event.naming;
/** /**
* Naming deregister service trace event. * Naming deregister service trace event.
*
* @author yanda * @author yanda
*/ */
public class DeregisterServiceTraceEvent extends NamingTraceEvent { public class DeregisterServiceTraceEvent extends NamingTraceEvent {
private static final long serialVersionUID = 7358195336881398548L; private static final long serialVersionUID = 7358195336881398548L;
public DeregisterServiceTraceEvent(long eventTime, String serviceNamespace, public DeregisterServiceTraceEvent(long eventTime, String serviceNamespace, String serviceGroup,
String serviceGroup, String serviceName) { String serviceName) {
super("DEREGISTER_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); super("DEREGISTER_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName);
} }
} }

View File

@ -20,21 +20,22 @@ import com.alibaba.nacos.common.trace.HealthCheckType;
/** /**
* Naming instance health state change trace event. * Naming instance health state change trace event.
*
* @author yanda * @author yanda
*/ */
public class HealthStateChangeTraceEvent extends NamingTraceEvent { public class HealthStateChangeTraceEvent extends NamingTraceEvent {
private static final long serialVersionUID = 6966396191118694597L; 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() { public String getInstanceIp() {
return instanceIp; return instanceIp;
@ -60,8 +61,8 @@ public class HealthStateChangeTraceEvent extends NamingTraceEvent {
return healthStateChangeReason; return healthStateChangeReason;
} }
public HealthStateChangeTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, public HealthStateChangeTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, String serviceName,
String serviceName, String instanceIp, int instancePort, boolean isHealthy, String healthStateChangeReason) { String instanceIp, int instancePort, boolean isHealthy, String healthStateChangeReason) {
super("HEALTH_STATE_CHANGE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); super("HEALTH_STATE_CHANGE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName);
this.instanceIp = instanceIp; this.instanceIp = instanceIp;
this.instancePort = instancePort; this.instancePort = instancePort;

View File

@ -24,11 +24,11 @@ import com.alibaba.nacos.common.trace.event.TraceEvent;
* @author yanda * @author yanda
*/ */
public class NamingTraceEvent extends TraceEvent { public class NamingTraceEvent extends TraceEvent {
private static final long serialVersionUID = 2923077640400851816L; private static final long serialVersionUID = 2923077640400851816L;
public NamingTraceEvent(String eventType, long eventTime, public NamingTraceEvent(String eventType, long eventTime, String serviceNamespace, String serviceGroup,
String serviceNamespace, String serviceGroup, String name) { String name) {
super(eventType, eventTime, serviceNamespace, serviceGroup, name); super(eventType, eventTime, serviceNamespace, serviceGroup, name);
} }

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.common.trace.event.naming;
/** /**
* Naming push service trace event. * Naming push service trace event.
*
* @author yanda * @author yanda
*/ */
public class PushServiceTraceEvent extends NamingTraceEvent { public class PushServiceTraceEvent extends NamingTraceEvent {
@ -55,8 +56,8 @@ public class PushServiceTraceEvent extends NamingTraceEvent {
} }
public PushServiceTraceEvent(long eventTime, long pushCostTimeForNetWork, long pushCostTimeForAll, public PushServiceTraceEvent(long eventTime, long pushCostTimeForNetWork, long pushCostTimeForAll,
long serviceLevelAgreementTime, String clientIp, String serviceNamespace, long serviceLevelAgreementTime, String clientIp, String serviceNamespace, String serviceGroup,
String serviceGroup, String serviceName, int instanceSize) { String serviceName, int instanceSize) {
super("PUSH_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); super("PUSH_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName);
this.clientIp = clientIp; this.clientIp = clientIp;
this.instanceSize = instanceSize; this.instanceSize = instanceSize;

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.common.trace.event.naming;
/** /**
* Naming register instance trace event. * Naming register instance trace event.
*
* @author yanda * @author yanda
*/ */
public class RegisterInstanceTraceEvent extends NamingTraceEvent { public class RegisterInstanceTraceEvent extends NamingTraceEvent {
@ -28,9 +29,9 @@ public class RegisterInstanceTraceEvent extends NamingTraceEvent {
private final boolean rpc; private final boolean rpc;
private String instanceIp; private final String instanceIp;
private int instancePort; private final int instancePort;
public String getClientIp() { public String getClientIp() {
return clientIp; return clientIp;

View File

@ -18,14 +18,14 @@ package com.alibaba.nacos.common.trace.event.naming;
/** /**
* Naming deregister service trace event. * Naming deregister service trace event.
*
* @author yanda * @author yanda
*/ */
public class RegisterServiceTraceEvent extends NamingTraceEvent { public class RegisterServiceTraceEvent extends NamingTraceEvent {
private static final long serialVersionUID = -8568231862586636388L; private static final long serialVersionUID = -8568231862586636388L;
public RegisterServiceTraceEvent(long eventTime, String serviceNamespace, public RegisterServiceTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, String serviceName) {
String serviceGroup, String serviceName) {
super("REGISTER_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); super("REGISTER_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName);
} }
} }

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.common.trace.event.naming;
/** /**
* Naming subscribe service trace event. * Naming subscribe service trace event.
*
* @author yanda * @author yanda
*/ */
public class SubscribeServiceTraceEvent extends NamingTraceEvent { public class SubscribeServiceTraceEvent extends NamingTraceEvent {
@ -30,8 +31,8 @@ public class SubscribeServiceTraceEvent extends NamingTraceEvent {
return clientIp; return clientIp;
} }
public SubscribeServiceTraceEvent(long eventTime, String clientIp, String serviceNamespace, public SubscribeServiceTraceEvent(long eventTime, String clientIp, String serviceNamespace, String serviceGroup,
String serviceGroup, String serviceName) { String serviceName) {
super("SUBSCRIBE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); super("SUBSCRIBE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName);
this.clientIp = clientIp; this.clientIp = clientIp;
} }

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.common.trace.event.naming;
/** /**
* Naming unsubscribe service trace event. * Naming unsubscribe service trace event.
*
* @author yanda * @author yanda
*/ */
public class UnsubscribeServiceTraceEvent extends NamingTraceEvent { public class UnsubscribeServiceTraceEvent extends NamingTraceEvent {
@ -30,8 +31,8 @@ public class UnsubscribeServiceTraceEvent extends NamingTraceEvent {
return clientIp; return clientIp;
} }
public UnsubscribeServiceTraceEvent(long eventTime, String clientIp, String serviceNamespace, public UnsubscribeServiceTraceEvent(long eventTime, String clientIp, String serviceNamespace, String serviceGroup,
String serviceGroup, String serviceName) { String serviceName) {
super("UNSUBSCRIBE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); super("UNSUBSCRIBE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName);
this.clientIp = clientIp; this.clientIp = clientIp;
} }

View File

@ -36,6 +36,10 @@ public class UpdateInstanceTraceEvent extends NamingTraceEvent {
private final int instancePort; private final int instancePort;
public Map<String, String> getMetadata() {
return metadata;
}
public String getClientIp() { public String getClientIp() {
return clientIp; return clientIp;
} }

View File

@ -30,6 +30,10 @@ public class UpdateServiceTraceEvent extends NamingTraceEvent {
private final Map<String, String> metadata; private final Map<String, String> metadata;
public Map<String, String> getMetadata() {
return metadata;
}
public UpdateServiceTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, String serviceName, public UpdateServiceTraceEvent(long eventTime, String serviceNamespace, String serviceGroup, String serviceName,
Map<String, String> metadata) { Map<String, String> metadata) {
super("UPDATE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName); super("UPDATE_SERVICE_TRACE_EVENT", eventTime, serviceNamespace, serviceGroup, serviceName);

View File

@ -20,8 +20,16 @@ import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.trace.DeregisterInstanceReason; import com.alibaba.nacos.common.trace.DeregisterInstanceReason;
import com.alibaba.nacos.common.trace.event.TraceEvent; 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.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.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.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.NacosTracePluginManager;
import com.alibaba.nacos.plugin.trace.spi.NacosTraceSubscriber; import com.alibaba.nacos.plugin.trace.spi.NacosTraceSubscriber;
import org.junit.After; import org.junit.After;
@ -29,12 +37,9 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; 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.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@SuppressWarnings("all")
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class NacosCombinedTraceSubscriberTest { public class NacosCombinedTraceSubscriberTest {
@Mock @Mock
private NacosTraceSubscriber mockSubscriber; private NacosTraceSubscriber mockServiceSubscriber;
@Mock @Mock
private NacosTraceSubscriber mockSubscriber2; private NacosTraceSubscriber mockInstanceSubscriber;
@Mock
private NacosTraceSubscriber mockOtherSubscriber;
private NacosCombinedTraceSubscriber combinedTraceSubscriber; private NacosCombinedTraceSubscriber combinedTraceSubscriber;
@SuppressWarnings("unchecked")
private Map<String, NacosTraceSubscriber> getTraceSubscribers() {
return (Map<String, NacosTraceSubscriber>) ReflectionTestUtils.getField(NacosTracePluginManager.getInstance(),
"traceSubscribers");
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
Map<String, NacosTraceSubscriber> traceSubscribers = (Map<String, NacosTraceSubscriber>) ReflectionTestUtils Map<String, NacosTraceSubscriber> traceSubscribers = getTraceSubscribers();
.getField(NacosTracePluginManager.getInstance(), "traceSubscribers"); traceSubscribers.put("instanceSubscriber", mockInstanceSubscriber);
traceSubscribers.put("nacos-combined", mockSubscriber); traceSubscribers.put("serviceSubscriber", mockServiceSubscriber);
traceSubscribers.put("nacos-combined2", mockSubscriber2); traceSubscribers.put("otherSubscriber", mockOtherSubscriber);
List<Class<? extends TraceEvent>> testEvents = new LinkedList<>(); // Initialization instance related.
testEvents.add(RegisterInstanceTraceEvent.class); List<Class<? extends TraceEvent>> instanceEvents = new LinkedList<>();
testEvents.add(DeregisterInstanceTraceEvent.class); instanceEvents.add(RegisterInstanceTraceEvent.class);
testEvents.add(TraceEvent.class); instanceEvents.add(DeregisterInstanceTraceEvent.class);
when(mockSubscriber.subscribeTypes()).thenReturn(testEvents); instanceEvents.add(UpdateInstanceTraceEvent.class);
when(mockSubscriber2.subscribeTypes()).thenReturn(Collections.singletonList(RegisterInstanceTraceEvent.class)); // Initialization service related.
List<Class<? extends TraceEvent>> 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<Class<? extends TraceEvent>> 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); combinedTraceSubscriber = new NacosCombinedTraceSubscriber(NamingTraceEvent.class);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
Map<String, NacosTraceSubscriber> traceSubscribers = (Map<String, NacosTraceSubscriber>) ReflectionTestUtils Map<String, NacosTraceSubscriber> traceSubscribers = getTraceSubscribers();
.getField(NacosTracePluginManager.getInstance(), "traceSubscribers"); traceSubscribers.remove("serviceSubscriber");
traceSubscribers.remove("nacos-combined"); traceSubscribers.remove("instanceSubscriber");
traceSubscribers.remove("nacos-combined2"); traceSubscribers.remove("otherSubscriber");
combinedTraceSubscriber.shutdown(); combinedTraceSubscriber.shutdown();
} }
@Test @Test
public void testSubscribeTypes() { public void testSubscribeTypes() {
List<Class<? extends Event>> actual = combinedTraceSubscriber.subscribeTypes(); List<Class<? extends Event>> actual = combinedTraceSubscriber.subscribeTypes();
assertEquals(2, actual.size()); assertEquals(10, actual.size());
assertTrue(actual.contains(RegisterInstanceTraceEvent.class)); assertTrue(actual.contains(RegisterInstanceTraceEvent.class));
assertTrue(actual.contains(DeregisterInstanceTraceEvent.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 @Test
public void testOnEvent() { public void testOnEvent() {
RegisterInstanceTraceEvent event = new RegisterInstanceTraceEvent(1L, "", true, "", "", "", "", 1); // Test RegisterInstanceTraceEvent.
doThrow(new RuntimeException("test")).when(mockSubscriber2).onEvent(event); RegisterInstanceTraceEvent registerInstanceTraceEvent = new RegisterInstanceTraceEvent(1L, "", true, "", "", "",
combinedTraceSubscriber.onEvent(event); "", 1);
verify(mockSubscriber).onEvent(event); doThrow(new RuntimeException("test")).when(mockInstanceSubscriber).onEvent(registerInstanceTraceEvent);
verify(mockSubscriber2).onEvent(event); combinedTraceSubscriber.onEvent(registerInstanceTraceEvent);
DeregisterInstanceTraceEvent event1 = new DeregisterInstanceTraceEvent(1L, "", true, 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); DeregisterInstanceReason.REQUEST, "", "", "", "", 1);
combinedTraceSubscriber.onEvent(event1); combinedTraceSubscriber.onEvent(deregisterInstanceTraceEvent);
verify(mockSubscriber).onEvent(event1); verify(mockInstanceSubscriber, times(1)).onEvent(deregisterInstanceTraceEvent);
verify(mockSubscriber2, never()).onEvent(event1); verify(mockServiceSubscriber, never()).onEvent(deregisterInstanceTraceEvent);
TraceEvent event2 = new TraceEvent("", 1L, "", "", ""); verify(mockOtherSubscriber, never()).onEvent(deregisterInstanceTraceEvent);
combinedTraceSubscriber.onEvent(event2); // Test UpdateInstanceTraceEvent.
verify(mockSubscriber, never()).onEvent(event2); UpdateInstanceTraceEvent updateInstanceTraceEvent = new UpdateInstanceTraceEvent(1L, "", "", "", "", "", 123,
verify(mockSubscriber2, never()).onEvent(event2); 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 @Test
public void testOnEventWithExecutor() { public void testOnEventWithExecutor() {
Executor executor = mock(Executor.class); Executor executor = mock(Executor.class);
doAnswer(new Answer() { doAnswer(invocationOnMock -> {
@Override invocationOnMock.getArgument(0, Runnable.class).run();
public Object answer(InvocationOnMock invocationOnMock) throws Throwable { return null;
invocationOnMock.getArgument(0, Runnable.class).run();
return null;
}
}).when(executor).execute(any(Runnable.class)); }).when(executor).execute(any(Runnable.class));
when(mockSubscriber.executor()).thenReturn(executor); when(mockInstanceSubscriber.executor()).thenReturn(executor);
RegisterInstanceTraceEvent event = new RegisterInstanceTraceEvent(1L, "", true, "", "", "", "", 1); RegisterInstanceTraceEvent event = new RegisterInstanceTraceEvent(1L, "", true, "", "", "", "", 1);
combinedTraceSubscriber.onEvent(event); combinedTraceSubscriber.onEvent(event);
verify(mockSubscriber).onEvent(event); verify(mockInstanceSubscriber).onEvent(event);
verify(mockSubscriber2).onEvent(event);
} }
} }