Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/display/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-core</artifactId>
<version>4.0.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-junit5</artifactId>
<version>4.0.18</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;

import javafx.stage.Window;
Expand All @@ -31,6 +32,7 @@
import org.csstudio.display.builder.representation.javafx.JFXRepresentation;
import org.csstudio.display.builder.runtime.ActionUtil;
import org.csstudio.display.builder.runtime.RuntimeUtil;
import org.phoebus.framework.jobs.Job;
import org.phoebus.framework.jobs.JobManager;
import org.phoebus.framework.jobs.JobMonitor;
import org.phoebus.framework.macros.Macros;
Expand Down Expand Up @@ -94,6 +96,18 @@

private DisplayModel active_model;

/** Job that loads a display, cancelled by a newer load or closing the display. UI thread only */
private Job load_job;

Check warning on line 100 in app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DisplayRuntimeInstance.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "load_job" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHgFSgTH6_68wC13Fdl&open=AaHgFSgTH6_68wC13Fdl&pullRequest=4017

/** Model whose runtime a load job is starting.
* A starting runtime cannot be stopped, so when the display is disposed meanwhile,
* the load job stops the runtime and disposes the model once the start is done.
*/
private final AtomicReference<DisplayModel> starting_model = new AtomicReference<>();

Check warning on line 106 in app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DisplayRuntimeInstance.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "starting_model" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHgFSgTH6_68wC13Fdm&open=AaHgFSgTH6_68wC13Fdm&pullRequest=4017

/** Lets one load job at a time use {@link #starting_model}. Not used on the UI thread */
private final Object start_lock = new Object();

Check warning on line 109 in app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DisplayRuntimeInstance.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "start_lock" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHgFSgTH6_68wC13Fdn&open=AaHgFSgTH6_68wC13Fdn&pullRequest=4017

/** Toolbar button for zoom */
private ZoomAction zoom_action;

Expand Down Expand Up @@ -329,12 +343,15 @@
return display_info.orElse(null);
}

/** Load display file, represent it, start runtime
/** Load display file, represent it, start runtime.
* A load that is still in progress is cancelled.
* @param info Display file to load and represent
*/
public void loadDisplayFile(final DisplayInfo info)

Check failure on line 350 in app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DisplayRuntimeInstance.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHgFSgTH6_68wC13Fdo&open=AaHgFSgTH6_68wC13Fdo&pullRequest=4017
{
DisplayInfo old_info = display_info.orElse(null);
// If still loading another display, skip it
cancelLoad();
// If already executing another display, shut it down
disposeModel();

Expand All @@ -357,14 +374,15 @@
// Note the path & macros, then
display_info = Optional.of(info);
// load model off UI thread
JobManager.schedule("Load Display", monitor ->
load_job = JobManager.schedule("Load Display", monitor ->
{
try
{
final DisplayModel model = loadModel(monitor, info);

final Future<Void> represented = representation.submit(() -> representModel(model));
represented.get();
final Future<Boolean> represented = representation.submit(() -> representModelUnlessCanceled(monitor, model));
if (! Boolean.TRUE.equals(represented.get()))
return;

if (Boolean.TRUE.equals(auto_size_stage))
{
Expand All @@ -378,7 +396,8 @@
}

// Start runtime for the model
RuntimeUtil.startRuntime(model);
if (! startRuntimeUnlessCanceled(monitor, model))
return;

logger.log(Level.FINE, "Waiting for representation of model " + info.getPath());

Expand All @@ -402,6 +421,11 @@
}
catch (Exception ex)
{
if (monitor.isCanceled())
{ // Replaced by a newer load or closed: Nothing to report to the user
logger.log(Level.FINE, ex, () -> "Cannot load model from " + info.getPath() + ", which was replaced or closed meanwhile");
return;
}
logger.log(Level.SEVERE, "Cannot load model from " + info.getPath(), ex);

final String exception_message;
Expand Down Expand Up @@ -479,6 +503,58 @@
return null;
}

/** Represent a loaded model, unless its load was cancelled. Called on the UI thread, where loads are cancelled
* @param monitor Monitor of the job that loaded the model
* @param model Model to represent
* @return <code>true</code> if represented
* @throws Exception on error
*/
private boolean representModelUnlessCanceled(final JobMonitor monitor, final DisplayModel model) throws Exception
{
if (monitor.isCanceled())
{
model.dispose();
return false;
}
representModel(model);
return true;
}

/** Start the runtime of a represented model, unless its load was cancelled.
* If the display is disposed while the runtime starts, stop it and dispose the model, see {@link #disposeModel()}
* @param monitor Monitor of the job that loaded the model
* @param model Model to start
* @return <code>true</code> if the runtime was started and the load is still the current one
*/
private boolean startRuntimeUnlessCanceled(final JobMonitor monitor, final DisplayModel model)
{
synchronized (start_lock)
{
starting_model.set(model);
if (monitor.isCanceled())
{ // Disposed before the start. Unless the UI thread did it all, dispose the model
if (! starting_model.compareAndSet(model, null))
model.dispose();
return false;
}
RuntimeUtil.startRuntime(model);
if (starting_model.compareAndSet(model, null))
return ! monitor.isCanceled();
// Disposed while starting, left to this job
RuntimeUtil.stopRuntime(model);
model.dispose();
return false;
}
}

/** Cancel the load of a display, if one is in progress */
private void cancelLoad()
{
if (load_job != null && ! load_job.getMonitor().isDone())
load_job.cancel();
load_job = null;
}

/** Take note of the currently displayed model
*
* <p>Called by the {@link DockItemRepresentation},
Expand Down Expand Up @@ -524,14 +600,19 @@
active_model = model;
}

/** Stop runtime, dispose representation for current model */
/** Stop runtime, dispose representation for current model. Callers first cancel a load, see {@link #cancelLoad()} */
private void disposeModel()
{
final DisplayModel model = active_model;
active_model = null;

// Close handler disposes runtime and representation for model
if (model != null)
if (model == null)
return;
if (starting_model.compareAndSet(model, null))
// The load job is starting the runtime and will then stop it and dispose the model
representation.disposeRepresentation(model);
else
// Close handler disposes runtime and representation for model
ActionUtil.handleClose(model);
}

Expand Down Expand Up @@ -569,6 +650,8 @@
/** DockItem closed */
public void onClosed()
{
// If still loading a display, skip it
cancelLoad();
// Stop runtime, dispose widgets for the model
disposeModel();
// Stop representation, so no more widgets can be created in this dock item
Expand Down Expand Up @@ -614,4 +697,4 @@
{
return target.startsWith("window") || target.startsWith(TAG_STANDALONE);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.csstudio.display.builder.runtime.app;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.phoebus.pv.PV;
import org.phoebus.pv.PVFactory;

/** Creates PVs "blocking://..." for tests
*
* <p>After {@link #block()}, creating the next PV waits until {@link #release()},
* like a PV type that connects while it is created.
* This keeps a display runtime in its start for as long as a test needs.
*/
@SuppressWarnings("nls")
public class BlockingPVFactory implements PVFactory
{
private static volatile CountDownLatch creating = new CountDownLatch(0);
private static volatile CountDownLatch released = new CountDownLatch(0);

/** Make the next PV creation wait until {@link #release()} */
static void block()
{
creating = new CountDownLatch(1);
released = new CountDownLatch(1);
}

/** Wait until a PV creation is waiting
* @throws InterruptedException when interrupted
*/
static void awaitCreation() throws InterruptedException
{
if (! creating.await(10, TimeUnit.SECONDS))
throw new IllegalStateException("No PV was created");
}

/** Let a waiting PV creation continue */
static void release()
{
released.countDown();
}

@Override
public String getType()
{
return "blocking";
}

@Override
public PV createPV(final String name, final String base_name) throws Exception
{
creating.countDown();
if (! released.await(10, TimeUnit.SECONDS))
throw new IllegalStateException("PV creation was not released");
return new PV(name)
{
// Never connects, which is enough for these tests
};
}
}
Loading
Loading