CrossJvmPortFinderLock.java

1
package fr.sii.ogham.testing.util.port;
2
3
import java.io.File;
4
import java.io.IOException;
5
import java.nio.channels.FileChannel;
6
import java.nio.channels.FileLock;
7
import java.nio.file.Files;
8
import java.nio.file.Path;
9
import java.nio.file.Paths;
10
import java.nio.file.StandardOpenOption;
11
import java.util.SortedSet;
12
import java.util.function.Supplier;
13
14
public class CrossJvmPortFinderLock implements PortFinder {
15
    private final PortFinder delegate;
16
    private final Path lockFile;
17
18
    public CrossJvmPortFinderLock(PortFinder delegate) {
19
        this.delegate = delegate;
20
        lockFile = Paths.get(System.getProperty("java.io.tmpdir"), "ogham-port-finder.lock");
21
    }
22
23
    @Override
24
    public int findAvailablePort(int minPort, int maxPort) {
25 2 1. lambda$findAvailablePort$0 : replaced Integer return value with 0 for fr/sii/ogham/testing/util/port/CrossJvmPortFinderLock::lambda$findAvailablePort$0 → NO_COVERAGE
2. findAvailablePort : replaced int return with 0 for fr/sii/ogham/testing/util/port/CrossJvmPortFinderLock::findAvailablePort → NO_COVERAGE
        return doWithLock(() -> delegate.findAvailablePort(minPort, maxPort));
26
    }
27
28
    @Override
29
    public SortedSet<Integer> findAvailablePorts(int numRequested, int minPort, int maxPort) {
30 2 1. lambda$findAvailablePorts$1 : replaced return value with null for fr/sii/ogham/testing/util/port/CrossJvmPortFinderLock::lambda$findAvailablePorts$1 → NO_COVERAGE
2. findAvailablePorts : replaced return value with null for fr/sii/ogham/testing/util/port/CrossJvmPortFinderLock::findAvailablePorts → NO_COVERAGE
        return doWithLock(() -> delegate.findAvailablePorts(numRequested, minPort, maxPort));
31
    }
32
33
    private <T> T doWithLock(Supplier<T> func) {
34
        try (FileChannel channel = FileChannel.open(lockFile, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
35
             FileLock lock = channel.lock()) {
36
            return func.get();
37
        } catch (IOException e) {
38
            throw new RuntimeException(e);
39
        }
40
    }
41
}

Mutations

25

1.1
Location : lambda$findAvailablePort$0
Killed by :
replaced Integer return value with 0 for fr/sii/ogham/testing/util/port/CrossJvmPortFinderLock::lambda$findAvailablePort$0 → NO_COVERAGE

2.2
Location : findAvailablePort
Killed by :
replaced int return with 0 for fr/sii/ogham/testing/util/port/CrossJvmPortFinderLock::findAvailablePort → NO_COVERAGE

30

1.1
Location : lambda$findAvailablePorts$1
Killed by :
replaced return value with null for fr/sii/ogham/testing/util/port/CrossJvmPortFinderLock::lambda$findAvailablePorts$1 → NO_COVERAGE

2.2
Location : findAvailablePorts
Killed by :
replaced return value with null for fr/sii/ogham/testing/util/port/CrossJvmPortFinderLock::findAvailablePorts → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1