ActionQueue.java

1
package com.renomad.minum.queue;
2
3
import com.renomad.minum.state.Context;
4
import com.renomad.minum.logging.ILogger;
5
import com.renomad.minum.utils.*;
6
7
import java.util.concurrent.*;
8
9
/**
10
 * This class provides the ability to pop items into
11
 * a queue thread-safely and know they'll happen later.
12
 * <p>
13
 * For example, this is helpful for minum.logging, or passing
14
 * functions to a minum.database.  It lets us run a bit faster,
15
 * since the I/O actions are happening on a separate
16
 * thread and the only time required is passing the
17
 * function of what we want to run later.
18
 * </p>
19
 * <h3>Example:</h3>
20
 * <p>
21
 *     This example shows where an {@link java.io.InputStream} representing the bytes
22
 *     of an image file are sent to the {@link ActionQueue} for processing.  The call
23
 *     to {@link ActionQueue#enqueue(String, ThrowingRunnable)} returns immediately,
24
 *     and processing continues on another thread.
25
 * </p>
26
 * <pre>
27
 * {@code
28
 *  ActionQueue photoResizingQueue;
29
 *  InputStream photoInputStream;
30
 *  photoResizingQueue = new ActionQueue("photo_resizing", context).initialize();
31
 *  photoResizingQueue.enqueue("resize an image", () -> resizeImage(photoInputStream));
32
 * }
33
 * </pre>
34
 */
35
public final class ActionQueue implements AbstractActionQueue {
36
    private final String name;
37
    private final ExecutorService queueExecutor;
38
    private final LinkedBlockingQueue<RunnableWithDescription> queue;
39
    private final ILogger logger;
40
    private boolean stop = false;
41
    private Thread queueThread;
42
    private boolean isStoppedStatus;
43
44
    /**
45
     * See the {@link ActionQueue} description for more detail. This
46
     * constructor will build your new action queue and handle registering
47
     * it with a list of other action queues in the {@link Context} object.
48
     * @param name give this object a unique, explanatory name.
49
     */
50
    public ActionQueue(String name, Context context) {
51
        this.name = name;
52
        this.queueExecutor = context.getExecutorService();
53
        this.queue = new LinkedBlockingQueue<>();
54 1 1. <init> : removed call to com/renomad/minum/queue/ActionQueueState::offerToQueue → KILLED
        context.getActionQueueState().offerToQueue(this);
55
        this.logger = context.getLogger();
56
    }
57
58
    // Regarding the InfiniteLoopStatement - indeed, we expect that the while loop
59
    // below is an infinite loop unless there's an exception thrown, that's what it is.
60
    @SuppressWarnings("InfiniteLoopStatement")
61
    @Override
62
    public ActionQueue initialize() {
63
        Runnable centralLoop = () -> {
64
            Thread.currentThread().setName(name);
65
            this.queueThread = Thread.currentThread();
66
            try {
67
                while (true) {
68 1 1. lambda$initialize$1 : removed call to com/renomad/minum/queue/ActionQueue::runAction → TIMED_OUT
                    runAction(logger, queue);
69
                }
70
            } catch (InterruptedException ex) {
71
                /*
72
                this is what we expect to happen.
73
                once this happens, we just continue on.
74
                this only gets called when we are trying to shut everything
75
                down cleanly
76
                 */
77
                logger.logDebug(() -> String.format("%s ActionQueue for %s is stopped.%n", TimeUtils.getTimestampIsoInstant(), name));
78
                Thread.currentThread().interrupt();
79
            }
80
        };
81
        queueExecutor.submit(centralLoop);
82 1 1. initialize : replaced return value with null for com/renomad/minum/queue/ActionQueue::initialize → KILLED
        return this;
83
    }
84
85
    static void runAction(ILogger logger, LinkedBlockingQueue<RunnableWithDescription> queue) throws InterruptedException {
86
        RunnableWithDescription action = queue.take();
87
        try {
88 1 1. runAction : removed call to com/renomad/minum/utils/RunnableWithDescription::run → KILLED
            action.run();
89
        } catch (Throwable e) {
90
            /*
91
             This needs to be a Throwable and not an Exception, for important reasons.
92
93
             Sometimes, the command being run will encounter Errors, rather than
94
             Exceptions. For example, OutOfMemoryError.  We must catch it, log the
95
             issue, and move on to avoid killing the thread needlessly
96
             */
97
            logger.logAsyncError(() -> StacktraceUtils.stackTraceToString(e));
98
        }
99
    }
100
101
    /**
102
     * Adds something to the queue to be processed.
103
     * <p>
104
     *     Here is an example use of .enqueue:
105
     * </p>
106
     * <pre>
107
     * {@code   actionQueue.enqueue("Write person file to disk at " + filePath, () -> {
108
     *             Files.writeString(filePath, pf.serialize());
109
     *         });}
110
     * </pre>
111
     */
112
    @Override
113
    public void enqueue(String description, ThrowingRunnable action) {
114 1 1. enqueue : negated conditional → KILLED
        if (! stop) {
115
            queue.add(new RunnableWithDescription(action, description));
116
        } else {
117
            throw new UtilsException(String.format("failed to enqueue %s - ActionQueue \"%s\" is stopped", description, this.name));
118
        }
119
    }
120
121
    /**
122
     * Stops the action queue
123
     * @param count how many loops to wait before we crash it closed
124
     * @param sleepTime how long to wait in milliseconds between loops
125
     */
126
    @Override
127
    public void stop(int count, int sleepTime) {
128
        String timestamp = TimeUtils.getTimestampIsoInstant();
129
        logger.logDebug(() ->  String.format("%s Stopping queue %s", timestamp, this));
130
        stop = true;
131 2 1. stop : negated conditional → TIMED_OUT
2. stop : changed conditional boundary → KILLED
        for (int i = 0; i < count; i++) {
132 1 1. stop : negated conditional → TIMED_OUT
            if (queue.isEmpty()) return;
133
            logger.logDebug(() ->  String.format("%s Queue not yet empty, has %d elements. waiting...%n",timestamp, queue.size()));
134
            MyThread.sleep(sleepTime);
135
        }
136
        isStoppedStatus = true;
137
        logger.logDebug(() -> String.format("%s Queue %s has %d elements left but we're done waiting.  Queue toString: %s", timestamp, this, queue.size(), queue));
138
    }
139
140
    /**
141
     * This will prevent any new actions being
142
     * queued (by setting the stop flag to true and thus
143
     * causing an exception to be thrown
144
     * when a call is made to [enqueue]) and will
145
     * block until the queue is empty.
146
     */
147
    @Override
148
    public void stop() {
149 1 1. stop : removed call to com/renomad/minum/queue/ActionQueue::stop → KILLED
        stop(5, 20);
150
    }
151
152
    @Override
153
    public String toString() {
154 1 1. toString : replaced return value with "" for com/renomad/minum/queue/ActionQueue::toString → KILLED
        return this.name;
155
    }
156
157
    Thread getQueueThread() {
158 1 1. getQueueThread : replaced return value with null for com/renomad/minum/queue/ActionQueue::getQueueThread → KILLED
        return queueThread;
159
    }
160
161
    @Override
162
    public LinkedBlockingQueue<RunnableWithDescription> getQueue() {
163 1 1. getQueue : replaced return value with null for com/renomad/minum/queue/ActionQueue::getQueue → KILLED
        return new LinkedBlockingQueue<>(queue);
164
    }
165
166
    @Override
167
    public boolean isStopped() {
168 2 1. isStopped : replaced boolean return with true for com/renomad/minum/queue/ActionQueue::isStopped → KILLED
2. isStopped : replaced boolean return with false for com/renomad/minum/queue/ActionQueue::isStopped → KILLED
        return isStoppedStatus;
169
    }
170
}

Mutations

54

1.1
Location : <init>
Killed by : com.renomad.minum.utils.ActionQueueKillerTests.test_KillAllQueues(com.renomad.minum.utils.ActionQueueKillerTests)
removed call to com/renomad/minum/queue/ActionQueueState::offerToQueue → KILLED

68

1.1
Location : lambda$initialize$1
Killed by : none
removed call to com/renomad/minum/queue/ActionQueue::runAction → TIMED_OUT

82

1.1
Location : initialize
Killed by : com.renomad.minum.database.DbTests.testIndex_NegativeCase_IndexNameEmptyString(com.renomad.minum.database.DbTests)
replaced return value with null for com/renomad/minum/queue/ActionQueue::initialize → KILLED

88

1.1
Location : runAction
Killed by : com.renomad.minum.database.DbTests.test_Deserialization_EdgeCases_2(com.renomad.minum.database.DbTests)
removed call to com/renomad/minum/utils/RunnableWithDescription::run → KILLED

114

1.1
Location : enqueue
Killed by : com.renomad.minum.database.DbTests.testIndex_NegativeCase_IndexNameEmptyString(com.renomad.minum.database.DbTests)
negated conditional → KILLED

131

1.1
Location : stop
Killed by : none
negated conditional → TIMED_OUT

2.2
Location : stop
Killed by : com.renomad.minum.utils.ActionQueueTests
changed conditional boundary → KILLED

132

1.1
Location : stop
Killed by : none
negated conditional → TIMED_OUT

149

1.1
Location : stop
Killed by : com.renomad.minum.utils.ActionQueueKillerTests.test_KillAllQueues_WithDebug(com.renomad.minum.utils.ActionQueueKillerTests)
removed call to com/renomad/minum/queue/ActionQueue::stop → KILLED

154

1.1
Location : toString
Killed by : com.renomad.minum.utils.ActionQueueKillerTests.test_KillAllQueues(com.renomad.minum.utils.ActionQueueKillerTests)
replaced return value with "" for com/renomad/minum/queue/ActionQueue::toString → KILLED

158

1.1
Location : getQueueThread
Killed by : com.renomad.minum.utils.ActionQueueKillerTests.test_KillAllQueues_NeedingInterruption(com.renomad.minum.utils.ActionQueueKillerTests)
replaced return value with null for com/renomad/minum/queue/ActionQueue::getQueueThread → KILLED

163

1.1
Location : getQueue
Killed by : com.renomad.minum.utils.ActionQueueTests
replaced return value with null for com/renomad/minum/queue/ActionQueue::getQueue → KILLED

168

1.1
Location : isStopped
Killed by : com.renomad.minum.utils.ActionQueueTests
replaced boolean return with true for com/renomad/minum/queue/ActionQueue::isStopped → KILLED

2.2
Location : isStopped
Killed by : com.renomad.minum.utils.ActionQueueTests
replaced boolean return with false for com/renomad/minum/queue/ActionQueue::isStopped → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0