• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FIWARE / contract-management / #116

04 Nov 2026 06:22AM UTC coverage: 7.885% (+3.4%) from 4.488%
#116

Pull #31

wistefan
Address the review comments of PR #31

* OID4VP flows triggered by a participant-chosen sink use their own non-redirecting client
* a reached subscription cap is refused with 503 instead of 409, which a subscriber reads as an existing subscription
* document the broadcast semantics of subscriptions in the security notes
Pull Request #31: subscriptions, webhook handshake and delivery

1035 of 1054 new or added lines in 36 files covered. (98.2%)

2927 of 37120 relevant lines covered (7.89%)

0.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.18
/src/main/java/org/fiware/iam/dataspace/SubscriptionChannel.java
1
package org.fiware.iam.dataspace;
2

3
import io.micronaut.core.annotation.Nullable;
4
import lombok.extern.slf4j.Slf4j;
5
import org.fiware.iam.events.model.DataSpaceEventVO;
6
import reactor.core.Disposable;
7
import reactor.core.publisher.Mono;
8
import reactor.core.publisher.Sinks;
9

10
import java.time.Duration;
11
import java.util.concurrent.ArrayBlockingQueue;
12
import java.util.function.BiFunction;
13

14
/**
15
 * Bounded queue of one subscription, delivered one event after another, so a slow or failing sink only holds back its own
16
 * events. Also paces the requests to that sink. Lives as long as the subscription id, so a renewal keeps queue and pacing.
17
 */
18
@Slf4j
1 ✔
19
final class SubscriptionChannel {
20

21
    private static final long NANOS_PER_MINUTE = Duration.ofMinutes(1).toNanos();
1 ✔
22
    // concatMap must not prefetch, or events would wait beyond the bounded queue
23
    private static final int NO_PREFETCH = 0;
24

25
    /**
26
     * Result of queueing an event. OVERFLOW_STARTED is the first refusal after an accepted event, so a full queue is reported once.
27
     */
28
    enum Offer { QUEUED, OVERFLOW_STARTED, OVERFLOW, CLOSED }
1 ✔
29

30
    private final Sinks.Many<DataSpaceEventVO> queue;
31
    private final Disposable worker;
32
    // guarded by this
33
    private long nextSlot = System.nanoTime();
1 ✔
34
    private boolean overflowing;
35

36
    /**
37
     * The delivery must complete empty on failure; an error would end the channel.
38
     */
39
    SubscriptionChannel(int queueSize, BiFunction<SubscriptionChannel, DataSpaceEventVO, Mono<Void>> delivery) {
1 ✔
40
        queue = Sinks.many().unicast().onBackpressureBuffer(new ArrayBlockingQueue<>(queueSize));
1 ✔
41
        worker = queue.asFlux()
1 ✔
42
                .concatMap(event -> delivery.apply(this, event)
1 ✔
43
                        .onErrorResume(e -> {
1 ✔
NEW
44
                            log.error("Unexpected failure delivering event {}.", event.getId(), e);
×
NEW
45
                            return Mono.empty();
×
46
                        }), NO_PREFETCH)
47
                .subscribe();
1 ✔
48
    }
1 ✔
49

50
    synchronized Offer offer(DataSpaceEventVO event) {
51
        Sinks.EmitResult result = queue.tryEmitNext(event);
1 ✔
52
        if (result.isSuccess()) {
1 ✔
53
            overflowing = false;
1 ✔
54
            return Offer.QUEUED;
1 ✔
55
        }
56
        if (result != Sinks.EmitResult.FAIL_OVERFLOW) {
1 ✔
NEW
57
            return Offer.CLOSED;
×
58
        }
59
        boolean started = !overflowing;
1 ✔
60
        overflowing = true;
1 ✔
61
        return started ? Offer.OVERFLOW_STARTED : Offer.OVERFLOW;
1 ✔
62
    }
63

64
    /**
65
     * Completes when the next request to the sink may be sent: at most allowedRate requests per minute, unlimited if null.
66
     */
67
    Mono<Void> nextSlot(@Nullable Integer allowedRate) {
68
        return Mono.defer(() -> {
1 ✔
69
            Duration wait = reserveSlot(allowedRate);
1 ✔
70
            return wait.isZero() ? Mono.<Void>empty() : Mono.delay(wait).then();
1 ✔
71
        });
72
    }
73

74
    /**
75
     * Accepts no further events. Queued and in-flight ones still run through the delivery, which ends and counts them.
76
     */
77
    // same monitor as offer: the sink refuses concurrent emissions, and a lost completion would leak the worker
78
    synchronized void complete() {
79
        queue.tryEmitComplete();
1 ✔
80
    }
1 ✔
81

82
    /**
83
     * Stops delivery at once on shutdown; queued events are dropped uncounted.
84
     */
85
    void dispose() {
86
        worker.dispose();
1 ✔
87
    }
1 ✔
88

89
    /**
90
     * Longest wait for one slot at the given rate; zero if unlimited.
91
     */
92
    static Duration interval(@Nullable Integer allowedRate) {
93
        return Duration.ofNanos(allowedRate == null || allowedRate <= 0 ? 0 : NANOS_PER_MINUTE / allowedRate);
1 ✔
94
    }
95

96
    private synchronized Duration reserveSlot(@Nullable Integer allowedRate) {
97
        long now = System.nanoTime();
1 ✔
98
        long slot = nextSlot - now > 0 ? nextSlot : now;
1 ✔
99
        nextSlot = slot + interval(allowedRate).toNanos();
1 ✔
100
        return Duration.ofNanos(slot - now);
1 ✔
101
    }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc