InterruptibleCompletableFuture.java

1
/*
2
 * Copyright 2014-2026 Grzegorz Piwowarek, https://4comprehension.com/
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.pivovarit.collectors;
17
18
import java.util.concurrent.CompletableFuture;
19
import java.util.concurrent.FutureTask;
20
21
/**
22
 * A {@link CompletableFuture} whose cancellation propagates to the {@link FutureTask} that completes
23
 * it, so that {@code cancel(true)} interrupts the thread running the backing task.
24
 */
25
final class InterruptibleCompletableFuture<T> extends CompletableFuture<T> {
26
27
    private volatile FutureTask<?> backingTask;
28
29
    void completedBy(FutureTask<?> task) {
30
        backingTask = task;
31
    }
32
33
    @Override
34
    public boolean cancel(boolean mayInterruptIfRunning) {
35
        var cancelled = super.cancel(mayInterruptIfRunning);
36 1 1. cancel : negated conditional → TIMED_OUT
        if (backingTask != null) {
37
            backingTask.cancel(mayInterruptIfRunning);
38
        }
39 2 1. cancel : replaced boolean return with true for com/pivovarit/collectors/InterruptibleCompletableFuture::cancel → SURVIVED
2. cancel : replaced boolean return with false for com/pivovarit/collectors/InterruptibleCompletableFuture::cancel → KILLED
        return cancelled;
40
    }
41
}

Mutations

36

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

39

1.1
Location : cancel
Killed by : none
replaced boolean return with true for com/pivovarit/collectors/InterruptibleCompletableFuture::cancel → SURVIVED
Covering tests

2.2
Location : cancel
Killed by : com.pivovarit.collectors.test.CancellationTest.[engine:junit-jupiter]/[class:com.pivovarit.collectors.test.CancellationTest]/[test-factory:shouldInterruptRunningTasksOnCancellation()]/[dynamic-test:#2]
replaced boolean return with false for com/pivovarit/collectors/InterruptibleCompletableFuture::cancel → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support