Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
56334b48
Commit
56334b48
authored
Oct 27, 2015
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve testing of shutdown handler registration
parent
18081544
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
16 deletions
+45
-16
LoggingApplicationListener.java
...ingframework/boot/logging/LoggingApplicationListener.java
+5
-1
LoggingApplicationListenerTests.java
...amework/boot/logging/LoggingApplicationListenerTests.java
+40
-15
No files found.
spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java
View file @
56334b48
...
@@ -337,11 +337,15 @@ public class LoggingApplicationListener implements GenericApplicationListener {
...
@@ -337,11 +337,15 @@ public class LoggingApplicationListener implements GenericApplicationListener {
Runnable
shutdownHandler
=
loggingSystem
.
getShutdownHandler
();
Runnable
shutdownHandler
=
loggingSystem
.
getShutdownHandler
();
if
(
shutdownHandler
!=
null
if
(
shutdownHandler
!=
null
&&
shutdownHookRegistered
.
compareAndSet
(
false
,
true
))
{
&&
shutdownHookRegistered
.
compareAndSet
(
false
,
true
))
{
Runtime
.
getRuntime
().
add
ShutdownHook
(
new
Thread
(
shutdownHandler
));
register
ShutdownHook
(
new
Thread
(
shutdownHandler
));
}
}
}
}
}
}
void
registerShutdownHook
(
Thread
shutdownHook
)
{
Runtime
.
getRuntime
().
addShutdownHook
(
shutdownHook
);
}
public
void
setOrder
(
int
order
)
{
public
void
setOrder
(
int
order
)
{
this
.
order
=
order
;
this
.
order
=
order
;
}
}
...
...
spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java
View file @
56334b48
...
@@ -18,6 +18,8 @@ package org.springframework.boot.logging;
...
@@ -18,6 +18,8 @@ package org.springframework.boot.logging;
import
java.io.File
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.concurrent.CountDownLatch
;
import
java.util.concurrent.TimeUnit
;
import
java.util.logging.Handler
;
import
java.util.logging.Handler
;
import
java.util.logging.LogManager
;
import
java.util.logging.LogManager
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
...
@@ -44,6 +46,7 @@ import org.springframework.context.support.GenericApplicationContext;
...
@@ -44,6 +46,7 @@ import org.springframework.context.support.GenericApplicationContext;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
not
;
import
static
org
.
hamcrest
.
Matchers
.
not
;
import
static
org
.
hamcrest
.
Matchers
.
nullValue
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
@@ -346,26 +349,29 @@ public class LoggingApplicationListenerTests {
...
@@ -346,26 +349,29 @@ public class LoggingApplicationListenerTests {
@Test
@Test
public
void
shutdownHookIsNotRegisteredByDefault
()
throws
Exception
{
public
void
shutdownHookIsNotRegisteredByDefault
()
throws
Exception
{
TestLoggingApplicationListener
listener
=
new
TestLoggingApplicationListener
();
System
.
setProperty
(
LoggingSystem
.
class
.
getName
(),
System
.
setProperty
(
LoggingSystem
.
class
.
getName
(),
Null
ShutdownHandlerLoggingSystem
.
class
.
getName
());
Test
ShutdownHandlerLoggingSystem
.
class
.
getName
());
this
.
initializ
er
.
onApplicationEvent
(
listen
er
.
onApplicationEvent
(
new
ApplicationStartedEvent
(
new
SpringApplication
(),
NO_ARGS
));
new
ApplicationStartedEvent
(
new
SpringApplication
(),
NO_ARGS
));
this
.
initializer
.
initialize
(
this
.
context
.
getEnvironment
(),
listener
.
initialize
(
this
.
context
.
getEnvironment
(),
this
.
context
.
getClassLoader
());
this
.
context
.
getClassLoader
());
assertThat
(
listener
.
shutdownHook
,
is
(
nullValue
()));
assertThat
(
NullShutdownHandlerLoggingSystem
.
shutdownHandlerRequested
,
is
(
false
));
}
}
@Test
@Test
public
void
shutdownHookCanBeRegistered
()
throws
Exception
{
public
void
shutdownHookCanBeRegistered
()
throws
Exception
{
TestLoggingApplicationListener
listener
=
new
TestLoggingApplicationListener
();
System
.
setProperty
(
LoggingSystem
.
class
.
getName
(),
System
.
setProperty
(
LoggingSystem
.
class
.
getName
(),
Null
ShutdownHandlerLoggingSystem
.
class
.
getName
());
Test
ShutdownHandlerLoggingSystem
.
class
.
getName
());
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"logging.register_shutdown_hook:true"
);
"logging.register_shutdown_hook:true"
);
this
.
initializ
er
.
onApplicationEvent
(
listen
er
.
onApplicationEvent
(
new
ApplicationStartedEvent
(
new
SpringApplication
(),
NO_ARGS
));
new
ApplicationStartedEvent
(
new
SpringApplication
(),
NO_ARGS
));
this
.
initializer
.
initialize
(
this
.
context
.
getEnvironment
(),
listener
.
initialize
(
this
.
context
.
getEnvironment
(),
this
.
context
.
getClassLoader
());
this
.
context
.
getClassLoader
());
assertThat
(
listener
.
shutdownHook
,
is
(
not
(
nullValue
())));
assertThat
(
NullShutdownHandlerLoggingSystem
.
shutdownHandlerRequested
,
is
(
true
));
listener
.
shutdownHook
.
start
();
assertThat
(
TestShutdownHandlerLoggingSystem
.
shutdownLatch
.
await
(
30
,
TimeUnit
.
SECONDS
),
is
(
true
));
}
}
private
boolean
bridgeHandlerInstalled
()
{
private
boolean
bridgeHandlerInstalled
()
{
...
@@ -379,12 +385,13 @@ public class LoggingApplicationListenerTests {
...
@@ -379,12 +385,13 @@ public class LoggingApplicationListenerTests {
return
false
;
return
false
;
}
}
public
static
class
Null
ShutdownHandlerLoggingSystem
extends
AbstractLoggingSystem
{
public
static
class
Test
ShutdownHandlerLoggingSystem
extends
AbstractLoggingSystem
{
static
boolean
shutdownHandlerRequested
=
false
;
private
static
CountDownLatch
shutdownLatch
;
public
Null
ShutdownHandlerLoggingSystem
(
ClassLoader
classLoader
)
{
public
Test
ShutdownHandlerLoggingSystem
(
ClassLoader
classLoader
)
{
super
(
classLoader
);
super
(
classLoader
);
TestShutdownHandlerLoggingSystem
.
shutdownLatch
=
new
CountDownLatch
(
1
);
}
}
@Override
@Override
...
@@ -410,8 +417,26 @@ public class LoggingApplicationListenerTests {
...
@@ -410,8 +417,26 @@ public class LoggingApplicationListenerTests {
@Override
@Override
public
Runnable
getShutdownHandler
()
{
public
Runnable
getShutdownHandler
()
{
shutdownHandlerRequested
=
true
;
return
new
Runnable
()
{
return
null
;
@Override
public
void
run
()
{
TestShutdownHandlerLoggingSystem
.
shutdownLatch
.
countDown
();
}
};
}
}
public
static
class
TestLoggingApplicationListener
extends
LoggingApplicationListener
{
private
Thread
shutdownHook
;
@Override
void
registerShutdownHook
(
Thread
shutdownHook
)
{
this
.
shutdownHook
=
shutdownHook
;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment