From 27c2717d3be679bbc95c0ee5214b6d7bcf927fdf Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Thu, 15 Feb 2024 09:26:03 +0000 Subject: [PATCH] Can attempt message send without subscribers - Change state assert not to throw if we're in state where message would never get consumed. Now just returning false. - Fixes #1002 --- .../shell/component/view/event/DefaultEventLoop.java | 10 ++++++---- .../component/view/event/DefaultEventLoopTests.java | 11 ++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/spring-shell-core/src/main/java/org/springframework/shell/component/view/event/DefaultEventLoop.java b/spring-shell-core/src/main/java/org/springframework/shell/component/view/event/DefaultEventLoop.java index 10c628c9..0d5459a1 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/component/view/event/DefaultEventLoop.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/component/view/event/DefaultEventLoop.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2023-2024 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. @@ -45,7 +45,6 @@ import org.springframework.shell.component.view.control.View; import org.springframework.shell.component.view.control.ViewEvent; import org.springframework.shell.component.view.event.processor.AnimationEventLoopProcessor; import org.springframework.shell.component.view.event.processor.TaskEventLoopProcessor; -import org.springframework.util.Assert; /** * Default implementation of an {@link EventLoop}. @@ -202,8 +201,11 @@ public class DefaultEventLoop implements EventLoop { // } private boolean doSend(Message message, long timeout) { - Assert.state(this.active && this.many.currentSubscriberCount() > 0, - () -> "The [" + this + "] doesn't have subscribers to accept messages"); + if (!this.active || this.many.currentSubscriberCount() == 0) { + return false; + } + // Assert.state(this.active && this.many.currentSubscriberCount() > 0, + // () -> "The [" + this + "] doesn't have subscribers to accept messages"); long remainingTime = 0; if (timeout > 0) { remainingTime = timeout; diff --git a/spring-shell-core/src/test/java/org/springframework/shell/component/view/event/DefaultEventLoopTests.java b/spring-shell-core/src/test/java/org/springframework/shell/component/view/event/DefaultEventLoopTests.java index 23cbf025..1564b111 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/component/view/event/DefaultEventLoopTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/component/view/event/DefaultEventLoopTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2023-2024 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. @@ -120,6 +120,15 @@ class DefaultEventLoopTests { verifier1.verify(Duration.ofSeconds(1)); } + + @Test + void dispatchNoSubscribersDoesNotError() { + initDefault(); + Message message = MessageBuilder.withPayload("TEST").build(); + + loop.dispatch(message); + } + @Test void subsribtionCompletesWhenLoopDestroyed() { initDefault();