Skip to content

Commit

Permalink
Publish service ready / stopped in consistent order (#10325)
Browse files Browse the repository at this point in the history
* Publish service ready / stopped in consistent order

* Update test-suite/src/test/groovy/io/micronaut/EventListenerSpec.groovy

Co-authored-by: Sergio del Amo <[email protected]>

* Update test-suite/src/test/groovy/io/micronaut/EventListenerSpec.groovy

Co-authored-by: Sergio del Amo <[email protected]>

---------

Co-authored-by: Sergio del Amo <[email protected]>
  • Loading branch information
dstepanov and sdelamo authored Jan 4, 2024
1 parent 0295ee0 commit f35b55b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.Order;
import io.micronaut.core.order.Ordered;
import io.micronaut.discovery.ServiceInstance;
import io.micronaut.discovery.event.ServiceReadyEvent;
import io.micronaut.discovery.event.ServiceStoppedEvent;
Expand All @@ -32,6 +34,7 @@
@Singleton
@Internal
@Requires(classes = ServiceInstance.class)
@Order(Ordered.LOWEST_PRECEDENCE)
final class NettyServiceDiscovery {
private final ApplicationEventPublisher<ServiceReadyEvent> serviceReadyEventApplicationEventPublisher;
private final ApplicationEventPublisher<ServiceStoppedEvent> serviceStoppedEventApplicationEventPublisher;
Expand Down
93 changes: 93 additions & 0 deletions test-suite/src/test/groovy/io/micronaut/EventListenerSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2017-2019 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.context.event.ApplicationEvent
import io.micronaut.context.event.ApplicationEventListener
import io.micronaut.context.event.ShutdownEvent
import io.micronaut.context.event.StartupEvent
import io.micronaut.core.order.OrderUtil
import io.micronaut.discovery.event.ServiceReadyEvent
import io.micronaut.discovery.event.ServiceStoppedEvent
import io.micronaut.inject.qualifiers.Qualifiers
import io.micronaut.runtime.event.annotation.EventListener
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.runtime.server.event.ServerShutdownEvent
import io.micronaut.runtime.server.event.ServerStartupEvent
import jakarta.inject.Singleton
import spock.lang.Specification
import spock.util.concurrent.PollingConditions

class EventListenerSpec extends Specification {

void "test all events listener is invoked"() {
given:
PollingConditions conditions = new PollingConditions(timeout: 10)
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer,
['spec.name': getClass().getSimpleName(),
'micronaut.application.name': 'events-test']
)

when:
TestAllEventsListener t = embeddedServer.getApplicationContext().getBean(TestAllEventsListener)
def serverStartupListeners = embeddedServer.getApplicationContext().getBeansOfType(ApplicationEventListener.class, Qualifiers.byTypeArguments(ServerStartupEvent))
.stream()
.sorted(OrderUtil.COMPARATOR)
.toArray(ApplicationEventListener[]::new)
def serverShutdownListeners = embeddedServer.getApplicationContext().getBeansOfType(ApplicationEventListener.class, Qualifiers.byTypeArguments(ServerShutdownEvent))
.stream()
.sorted(OrderUtil.COMPARATOR)
.toArray(ApplicationEventListener[]::new)
then:
serverStartupListeners.last().getClass().name.contains "NettyServiceDiscovery"
serverShutdownListeners.last().getClass().name.contains "NettyServiceDiscovery"

conditions.eventually {
t.events.size() == 3
}

embeddedServer.close()

conditions.eventually {
t.events.size() == 6
}

t.events[0] == StartupEvent
t.events[1] == ServerStartupEvent
t.events[2] == ServiceReadyEvent
t.events[3] == ServerShutdownEvent
t.events[4] == ServiceStoppedEvent
t.events[5] == ShutdownEvent

cleanup:
embeddedServer.close()
}

@Requires(property = "spec.name", value = "EventListenerSpec")
@Singleton
static class TestAllEventsListener {

List<Class> events = new ArrayList<>()

@EventListener
void onStartup(ApplicationEvent event) {
events.add(event.getClass())
}

}
}

0 comments on commit f35b55b

Please sign in to comment.