Skip to content

Commit 12e5a30

Browse files
garyrussellartembilan
authored andcommitted
GH-3062: Polishing
- avoid log noise when test server stopped
1 parent 8ad802c commit 12e5a30

File tree

1 file changed

+26
-8
lines changed
  • spring-integration-test-support/src/main/java/org/springframework/integration/test/mail

1 file changed

+26
-8
lines changed

spring-integration-test-support/src/main/java/org/springframework/integration/test/mail/TestMailServer.java

+26-8
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ else if (line.equals("QUIT")) {
148148
messages.add(sb.toString());
149149
}
150150
catch (IOException e) {
151-
LOGGER.error(IO_EXCEPTION, e);
151+
if (!this.stopped) {
152+
LOGGER.error(IO_EXCEPTION, e);
153+
}
152154
}
153155
}
154156

@@ -210,7 +212,9 @@ else if ("QUIT".equals(line)) {
210212
}
211213
}
212214
catch (IOException e) {
213-
LOGGER.error(IO_EXCEPTION, e);
215+
if (!this.stopped) {
216+
LOGGER.error(IO_EXCEPTION, e);
217+
}
214218
}
215219
}
216220

@@ -380,7 +384,9 @@ else if (line.contains("LOGOUT")) {
380384
}
381385
}
382386
catch (IOException e) {
383-
LOGGER.error(IO_EXCEPTION, e);
387+
if (!this.stopped) {
388+
LOGGER.error(IO_EXCEPTION, e);
389+
}
384390
}
385391
}
386392

@@ -412,7 +418,7 @@ public abstract static class MailServer implements Runnable {
412418

413419
protected final List<String> messages = new ArrayList<>(); // NOSONAR protected
414420

415-
private final List<Socket> clientSockets = new ArrayList<>();
421+
private final List<MailHandler> handlers = new ArrayList<>();
416422

417423
private volatile boolean listening;
418424

@@ -447,8 +453,9 @@ public void run() {
447453
try {
448454
while (!serverSocket.isClosed()) {
449455
Socket socket = this.serverSocket.accept();
450-
this.clientSockets.add(socket);
451-
exec.execute(mailHandler(socket));
456+
MailHandler mailHandler = mailHandler(socket);
457+
this.handlers.add(mailHandler);
458+
exec.execute(mailHandler);
452459
}
453460
}
454461
catch (@SuppressWarnings("unused") IOException e) {
@@ -460,8 +467,8 @@ public void run() {
460467

461468
public void stop() {
462469
try {
463-
for (Socket socket : this.clientSockets) {
464-
socket.close();
470+
for (MailHandler handler : this.handlers) {
471+
handler.stop();
465472
}
466473
this.serverSocket.close();
467474
}
@@ -491,6 +498,8 @@ public abstract class MailHandler implements Runnable {
491498

492499
protected BufferedReader reader; // NOSONAR protected
493500

501+
protected boolean stopped; // NOSONAR
502+
494503
MailHandler(Socket socket) {
495504
this.socket = socket;
496505
}
@@ -515,6 +524,15 @@ protected void write(String str) throws IOException {
515524

516525
abstract void doRun();
517526

527+
void stop() {
528+
this.stopped = true;
529+
try {
530+
this.socket.close();
531+
}
532+
catch (IOException e) {
533+
// NOSONAR
534+
}
535+
}
518536
}
519537

520538
}

0 commit comments

Comments
 (0)