Search in sources :

Example 11 with BootEvent

use of org.apache.servicecomb.core.BootListener.BootEvent in project java-chassis by ServiceComb.

the class TestProducerBootListener method onBootEvent_close.

@Test
public void onBootEvent_close(@Mocked SCBEngine scbEngine, @Mocked MicroserviceMeta microserviceMeta, @Mocked OperationMeta op1, @Mocked OperationMeta op2, @Mocked GroupExecutor closeable) {
    AtomicInteger count = new AtomicInteger();
    ExecutorService executorService = new MockUp<ExecutorService>() {

        @Mock
        void shutdown() {
            count.incrementAndGet();
        }
    }.getMockInstance();
    new MockUp<Closeable>(closeable) {

        @Mock
        void close() {
            count.incrementAndGet();
        }
    };
    new Expectations() {

        {
            scbEngine.getProducerMicroserviceMeta();
            result = microserviceMeta;
            microserviceMeta.getOperations();
            result = Arrays.asList(op1, op2);
            op1.getExecutor();
            result = executorService;
            op2.getExecutor();
            result = closeable;
        }
    };
    BootEvent event = new BootEvent();
    event.setScbEngine(scbEngine);
    event.setEventType(EventType.AFTER_CLOSE);
    producerBootListener.onBootEvent(event);
    Assert.assertEquals(2, count.get());
}
Also used : Expectations(mockit.Expectations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) BootEvent(org.apache.servicecomb.core.BootListener.BootEvent) MockUp(mockit.MockUp) Mock(mockit.Mock) Test(org.junit.Test)

Example 12 with BootEvent

use of org.apache.servicecomb.core.BootListener.BootEvent in project java-chassis by ServiceComb.

the class TestProducerBootListener method onBootEvent_close_unknown.

@Test
public void onBootEvent_close_unknown(@Mocked SCBEngine scbEngine, @Mocked MicroserviceMeta microserviceMeta, @Mocked OperationMeta op1) {
    Executor executor = new UnCloseableExecutor();
    new Expectations() {

        {
            scbEngine.getProducerMicroserviceMeta();
            result = microserviceMeta;
            microserviceMeta.getOperations();
            result = Arrays.asList(op1);
            op1.getExecutor();
            result = executor;
        }
    };
    try (LogCollector logCollector = new LogCollector()) {
        BootEvent event = new BootEvent();
        event.setScbEngine(scbEngine);
        event.setEventType(EventType.AFTER_CLOSE);
        producerBootListener.onBootEvent(event);
        Assert.assertEquals("Executor org.apache.servicecomb.core.provider.producer.TestProducerBootListener$UnCloseableExecutor " + "do not support close or shutdown, it may block service shutdown.", logCollector.getLastEvents().getMessage());
    }
}
Also used : Expectations(mockit.Expectations) LogCollector(org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector) GroupExecutor(org.apache.servicecomb.core.executor.GroupExecutor) Executor(java.util.concurrent.Executor) BootEvent(org.apache.servicecomb.core.BootListener.BootEvent) Test(org.junit.Test)

Example 13 with BootEvent

use of org.apache.servicecomb.core.BootListener.BootEvent in project java-chassis by ServiceComb.

the class SCBEngine method triggerEvent.

protected void triggerEvent(EventType eventType) {
    BootEvent event = new BootEvent();
    event.setScbEngine(this);
    event.setEventType(eventType);
    for (BootListener listener : bootListeners) {
        listener.onBootEvent(event);
    }
}
Also used : BootEvent(org.apache.servicecomb.core.BootListener.BootEvent)

Example 14 with BootEvent

use of org.apache.servicecomb.core.BootListener.BootEvent in project java-chassis by ServiceComb.

the class TestHealthBootListener method onBeforeProducerProvider_health_endpoint_enabled_by_default.

@Test
public void onBeforeProducerProvider_health_endpoint_enabled_by_default() {
    final HealthBootListener listener = new HealthBootListener();
    final List<ProducerMeta> producerMetas = new ArrayList<>();
    final BootEvent event = new BootEvent();
    final ProducerMeta producerMeta = new ProducerMeta();
    final SCBEngine scbEngine = new SCBEngine() {

        public final ProducerProviderManager producerProviderManager = new ProducerProviderManager(this) {

            @Override
            public void addProducerMeta(String schemaId, Object instance) {
                producerMeta.setSchemaId(schemaId);
                producerMeta.setInstance(instance);
                producerMetas.add(producerMeta);
            }
        };

        @Override
        public ProducerProviderManager getProducerProviderManager() {
            return producerProviderManager;
        }
    };
    event.setScbEngine(scbEngine);
    listener.onBeforeProducerProvider(event);
    Assert.assertThat(producerMetas, Matchers.contains(producerMeta));
    Assert.assertThat(producerMeta.getSchemaId(), Matchers.equalTo("healthEndpoint"));
    Assert.assertThat(producerMeta.getInstance(), Matchers.instanceOf(HealthCheckerRestPublisher.class));
}
Also used : ProducerMeta(org.apache.servicecomb.core.provider.producer.ProducerMeta) SCBEngine(org.apache.servicecomb.core.SCBEngine) ArrayList(java.util.ArrayList) BootEvent(org.apache.servicecomb.core.BootListener.BootEvent) HealthCheckerRestPublisher(org.apache.servicecomb.metrics.core.publish.HealthCheckerRestPublisher) ProducerProviderManager(org.apache.servicecomb.core.provider.producer.ProducerProviderManager) Test(org.junit.Test)

Example 15 with BootEvent

use of org.apache.servicecomb.core.BootListener.BootEvent in project java-chassis by ServiceComb.

the class TestHealthBootListener method onBeforeProducerProvider_health_endpoint_disabled.

@Test
public void onBeforeProducerProvider_health_endpoint_disabled() {
    ArchaiusUtils.setProperty("servicecomb.health.endpoint.enabled", false);
    final HealthBootListener listener = new HealthBootListener();
    final List<ProducerMeta> producerMetas = new ArrayList<>();
    final BootEvent event = new BootEvent();
    final SCBEngine scbEngine = new SCBEngine() {

        public final ProducerProviderManager producerProviderManager = new ProducerProviderManager(this) {

            @Override
            public void addProducerMeta(String schemaId, Object instance) {
                producerMetas.add(new ProducerMeta(schemaId, instance));
            }
        };

        @Override
        public ProducerProviderManager getProducerProviderManager() {
            return producerProviderManager;
        }
    };
    event.setScbEngine(scbEngine);
    listener.onBeforeProducerProvider(event);
    Assert.assertThat(producerMetas, Matchers.empty());
}
Also used : ProducerMeta(org.apache.servicecomb.core.provider.producer.ProducerMeta) SCBEngine(org.apache.servicecomb.core.SCBEngine) ArrayList(java.util.ArrayList) BootEvent(org.apache.servicecomb.core.BootListener.BootEvent) ProducerProviderManager(org.apache.servicecomb.core.provider.producer.ProducerProviderManager) Test(org.junit.Test)

Aggregations

BootEvent (org.apache.servicecomb.core.BootListener.BootEvent)16 Test (org.junit.Test)13 SCBEngine (org.apache.servicecomb.core.SCBEngine)6 ProducerProviderManager (org.apache.servicecomb.core.provider.producer.ProducerProviderManager)5 ArrayList (java.util.ArrayList)4 ProducerMeta (org.apache.servicecomb.core.provider.producer.ProducerMeta)4 Expectations (mockit.Expectations)3 AuthHandlerBoot (org.apache.servicecomb.AuthHandlerBoot)3 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)2 LogCollector (org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector)2 Executor (java.util.concurrent.Executor)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Mock (mockit.Mock)1 MockUp (mockit.MockUp)1 EventType (org.apache.servicecomb.core.BootListener.EventType)1 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)1 GroupExecutor (org.apache.servicecomb.core.executor.GroupExecutor)1 Holder (org.apache.servicecomb.foundation.common.Holder)1 HealthCheckerRestPublisher (org.apache.servicecomb.metrics.core.publish.HealthCheckerRestPublisher)1