From eb1258f44e2da8be32356fd8b439d2c107cdc392 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Tue, 23 Feb 2021 15:48:35 +0100 Subject: [PATCH] Fix display showing more projects than the ones actually active --- .../org/mvndaemon/mvnd/common/Message.java | 46 +++++++++++++++++++ .../mvnd/common/logging/TerminalOutput.java | 6 ++- .../org/mvndaemon/mvnd/daemon/Server.java | 46 +------------------ .../org/mvndaemon/mvnd/daemon/ServerTest.java | 42 +++++++++++++++++ 4 files changed, 93 insertions(+), 47 deletions(-) create mode 100644 daemon/src/test/java/org/mvndaemon/mvnd/daemon/ServerTest.java diff --git a/common/src/main/java/org/mvndaemon/mvnd/common/Message.java b/common/src/main/java/org/mvndaemon/mvnd/common/Message.java index e5524a9b..2d0c68c0 100644 --- a/common/src/main/java/org/mvndaemon/mvnd/common/Message.java +++ b/common/src/main/java/org/mvndaemon/mvnd/common/Message.java @@ -23,6 +23,7 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.io.UTFDataFormatException; import java.util.ArrayList; +import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -108,6 +109,51 @@ public abstract class Message { final long timestamp = System.nanoTime(); + public static Comparator getMessageComparator() { + return Comparator.comparingInt(Message::getClassOrder).thenComparingLong(Message::timestamp); + } + + public static int getClassOrder(Message m) { + switch (m.getType()) { + case KEEP_ALIVE: + case BUILD_REQUEST: + return 0; + case BUILD_STARTED: + return 1; + case PROMPT: + case PROMPT_RESPONSE: + case DISPLAY: + return 2; + case PROJECT_STARTED: + return 3; + case MOJO_STARTED: + return 4; + case TRANSFER_INITIATED: + case TRANSFER_STARTED: + return 40; + case TRANSFER_PROGRESSED: + return 41; + case TRANSFER_CORRUPTED: + case TRANSFER_SUCCEEDED: + case TRANSFER_FAILED: + return 42; + case PROJECT_LOG_MESSAGE: + return 50; + case BUILD_LOG_MESSAGE: + return 51; + case PROJECT_STOPPED: + return 95; + case BUILD_FINISHED: + return 96; + case BUILD_EXCEPTION: + return 97; + case STOP: + return 99; + default: + throw new IllegalStateException("Unexpected message type " + m.getType() + ": " + m); + } + } + public long timestamp() { return timestamp; } diff --git a/common/src/main/java/org/mvndaemon/mvnd/common/logging/TerminalOutput.java b/common/src/main/java/org/mvndaemon/mvnd/common/logging/TerminalOutput.java index 585023aa..1294aac6 100644 --- a/common/src/main/java/org/mvndaemon/mvnd/common/logging/TerminalOutput.java +++ b/common/src/main/java/org/mvndaemon/mvnd/common/logging/TerminalOutput.java @@ -355,8 +355,10 @@ public class TerminalOutput implements ClientOutput { } case Message.PROJECT_LOG_MESSAGE: { final ProjectEvent bm = (ProjectEvent) entry; - final Project prj = projects.computeIfAbsent(bm.getProjectId(), Project::new); - if (noBuffering || dumb) { + final Project prj = projects.get(bm.getProjectId()); + if (prj == null) { + log.accept(bm.getMessage()); + } else if (noBuffering || dumb) { String msg; if (maxThreads > 1) { msg = String.format("[%s] %s", bm.getProjectId(), bm.getMessage()); diff --git a/daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java b/daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java index 09dce2f3..d14f0a21 100644 --- a/daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java +++ b/daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java @@ -24,7 +24,6 @@ import java.nio.channels.SocketChannel; import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; -import java.util.Comparator; import java.util.List; import java.util.Locale; import java.util.Map; @@ -429,8 +428,7 @@ public class Server implements AutoCloseable, Runnable { private void handle(DaemonConnection connection, BuildRequest buildRequest) { updateState(Busy); - final BlockingQueue sendQueue = new PriorityBlockingQueue<>(64, - Comparator.comparingInt(this::getClassOrder).thenComparingLong(Message::timestamp)); + final BlockingQueue sendQueue = new PriorityBlockingQueue<>(64, Message.getMessageComparator()); final BlockingQueue recvQueue = new LinkedBlockingDeque<>(); final BuildEventListener buildEventListener = new ClientDispatcher(sendQueue); try (ProjectBuildLogAppender logAppender = new ProjectBuildLogAppender(buildEventListener)) { @@ -558,48 +556,6 @@ public class Server implements AutoCloseable, Runnable { } } - int getClassOrder(Message m) { - switch (m.getType()) { - case Message.BUILD_REQUEST: - return 0; - case Message.BUILD_STARTED: - return 1; - case Message.PROMPT: - case Message.PROMPT_RESPONSE: - case Message.DISPLAY: - return 2; - case Message.PROJECT_STARTED: - return 3; - case Message.MOJO_STARTED: - return 4; - case Message.TRANSFER_INITIATED: - case Message.TRANSFER_STARTED: - return 40; - case Message.TRANSFER_PROGRESSED: - return 41; - case Message.TRANSFER_CORRUPTED: - case Message.TRANSFER_SUCCEEDED: - case Message.TRANSFER_FAILED: - return 42; - case Message.PROJECT_LOG_MESSAGE: - return 50; - case Message.BUILD_LOG_MESSAGE: - return 51; - case Message.PROJECT_STOPPED: - return 95; - case Message.BUILD_FINISHED: - return 96; - case Message.BUILD_EXCEPTION: - return 97; - case Message.STOP: - return 99; - case Message.KEEP_ALIVE: - return 100; - default: - throw new IllegalStateException("Unexpected message type " + m.getType() + ": " + m); - } - } - private void updateState(DaemonState state) { if (getState() != state) { LOGGER.info("Updating state to: " + state); diff --git a/daemon/src/test/java/org/mvndaemon/mvnd/daemon/ServerTest.java b/daemon/src/test/java/org/mvndaemon/mvnd/daemon/ServerTest.java new file mode 100644 index 00000000..538293bd --- /dev/null +++ b/daemon/src/test/java/org/mvndaemon/mvnd/daemon/ServerTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2021 the original author or 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 org.mvndaemon.mvnd.daemon; + +import java.util.Arrays; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.PriorityBlockingQueue; + +import org.junit.jupiter.api.Test; +import org.mvndaemon.mvnd.common.Message; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ServerTest { + + @Test + void testMessageOrdering() { + BlockingQueue messages = new PriorityBlockingQueue<>(64, Message.getMessageComparator()); + messages.addAll(Arrays.asList( + Message.projectStopped("projectId"), + Message.projectStarted("projectId"), + Message.log("projectId", "message")) + ); + + assertEquals(Message.PROJECT_STARTED, messages.remove().getType()); + assertEquals(Message.PROJECT_LOG_MESSAGE, messages.remove().getType()); + assertEquals(Message.PROJECT_STOPPED, messages.remove().getType()); + } +}