-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish service ready / stopped in consistent order (#10325)
* 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
Showing
2 changed files
with
96 additions
and
0 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
93 changes: 93 additions & 0 deletions
93
test-suite/src/test/groovy/io/micronaut/EventListenerSpec.groovy
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,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()) | ||
} | ||
|
||
} | ||
} |