/** * Returns a test instance of {@link LeaseFrame}. * * @return a test instance of {@link LeaseFrame} */ public static LeaseFrame createTestLeaseFrame() { return createLeaseFrame(DEFAULT, Duration.ofMillis(1), 1, null); }
/** * Creates the {@code LEASE} frame. * * @param byteBufAllocator the {@code ByteBufAllocator} to use * @param timeToLive the validity of lease from time of reception * @param numberOfRequests the number of requests that may be sent until the next lease * @param metadata the metadata * @return the {@code LEASE} frame * @throws IllegalArgumentException if {@code timeToLive} is not a positive duration * @throws IllegalArgumentException if {@code numberOfRequests} is not positive * @throws NullPointerException if {@code byteBufAllocator} or {@code timeToLive} is {@code null} * @throws IllegalArgumentException if {@code timeToLive} is not a positive duration or {@code * numberOfRequests} is not positive */ public static LeaseFrame createLeaseFrame( ByteBufAllocator byteBufAllocator, Duration timeToLive, int numberOfRequests, @Nullable ByteBuf metadata) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(timeToLive, "timeToLive must not be null"); NumberUtils.requirePositive(timeToLive.toMillis(), "timeToLive must be a positive duration"); NumberUtils.requirePositive(numberOfRequests, "numberOfRequests must be positive"); ByteBuf byteBuf = createFrameTypeAndFlags(byteBufAllocator, LEASE) .writeInt(toIntExact(timeToLive.toMillis())) .writeInt(numberOfRequests); byteBuf = appendMetadata(byteBuf, metadata); return RECYCLER.get().setByteBuf(byteBuf); }
/** * Returns the time to live. * * @return the time to live */ public Duration getTimeToLive() { return Duration.ofMillis(getByteBuf().getInt(OFFSET_TIME_TO_LIVE)); }
@Override public String toString() { return "LeaseFrame{" + "timeToLive=" + getTimeToLive() + ", numberOfRequests=" + getNumberOfRequests() + ", metadata=" + mapMetadata(ByteBufUtil::hexDump) + +'}'; } }
/** * Creates the {@code LEASE} frame. * * @param byteBuf the {@link ByteBuf} representing the frame * @return the {@code LEASE} frame. * @throws NullPointerException if {@code byteBuf} is {@code null} */ public static LeaseFrame createLeaseFrame(ByteBuf byteBuf) { Objects.requireNonNull(byteBuf, "byteBuf must not be null"); return RECYCLER.get().setByteBuf(byteBuf.retain()); }
@Override public @Nullable ByteBuf getUnsafeMetadata() { return getMetadata(OFFSET_METADATA); }
@Override public String toString() { return "LeaseFrame{" + "timeToLive=" + getTimeToLive() + ", numberOfRequests=" + getNumberOfRequests() + ", metadata=" + mapMetadata(ByteBufUtil::hexDump) + +'}'; } }
/** * Creates the {@code LEASE} frame. * * @param byteBuf the {@link ByteBuf} representing the frame * @return the {@code LEASE} frame. * @throws NullPointerException if {@code byteBuf} is {@code null} */ public static LeaseFrame createLeaseFrame(ByteBuf byteBuf) { Objects.requireNonNull(byteBuf, "byteBuf must not be null"); return RECYCLER.get().setByteBuf(byteBuf.retain()); }
@Override public @Nullable ByteBuf getUnsafeMetadata() { return getMetadata(OFFSET_METADATA); }
public static LeaseFrame createTestLeaseFrame() { return createLeaseFrame(DEFAULT, Duration.ofMillis(1), 1, null); }
/** * Creates the {@code LEASE} frame. * * @param byteBufAllocator the {@code ByteBufAllocator} to use * @param timeToLive the validity of lease from time of reception * @param numberOfRequests the number of requests that may be sent until the next lease * @param metadata the metadata * @return the {@code LEASE} frame * @throws IllegalArgumentException if {@code timeToLive} is not a positive duration * @throws IllegalArgumentException if {@code numberOfRequests} is not positive * @throws NullPointerException if {@code byteBufAllocator} or {@code timeToLive} is {@code null} * @throws IllegalArgumentException if {@code timeToLive} is not a positive duration or {@code * numberOfRequests} is not positive */ public static LeaseFrame createLeaseFrame( ByteBufAllocator byteBufAllocator, Duration timeToLive, int numberOfRequests, @Nullable ByteBuf metadata) { Objects.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); Objects.requireNonNull(timeToLive, "timeToLive must not be null"); NumberUtils.requirePositive(timeToLive.toMillis(), "timeToLive must be a positive duration"); NumberUtils.requirePositive(numberOfRequests, "numberOfRequests must be positive"); ByteBuf byteBuf = createFrameTypeAndFlags(byteBufAllocator, LEASE) .writeInt(toIntExact(timeToLive.toMillis())) .writeInt(numberOfRequests); byteBuf = appendMetadata(byteBuf, metadata); return RECYCLER.get().setByteBuf(byteBuf); }
/** * Returns the number of requests * * @return the number of requests */ public int getNumberOfRequests() { return getByteBuf().getInt(OFFSET_NUMBER_OF_REQUESTS); }
@DisplayName("createLeaseFrame throws IllegalArgumentException with invalid numberOfRequests") @Test void createLeaseFrameFrameInvalidNumberOfRequests() { assertThatIllegalArgumentException() .isThrownBy(() -> createLeaseFrame(DEFAULT, Duration.ofMillis(1), 0, null)) .withMessage("numberOfRequests must be positive"); }
/** * Returns the number of requests * * @return the number of requests */ public int getNumberOfRequests() { return getByteBuf().getInt(OFFSET_NUMBER_OF_REQUESTS); }
@DisplayName("createLeaseFrame throws NullPointerException with null byteBufAllocator") @Test void createLeaseFrameNullByteBufAllocator() { assertThatNullPointerException() .isThrownBy(() -> createLeaseFrame(null, Duration.ofMillis(1), 1, null)) .withMessage("byteBufAllocator must not be null"); }
/** * Returns the time to live. * * @return the time to live */ public Duration getTimeToLive() { return Duration.ofMillis(getByteBuf().getInt(OFFSET_TIME_TO_LIVE)); }
@DisplayName("createLeaseFrame throws IllegalArgumentException with invalid timeToLive") @Test void createLeaseFrameInvalidTimeToLive() { assertThatIllegalArgumentException() .isThrownBy(() -> createLeaseFrame(DEFAULT, Duration.ofMillis(0), 1, null)) .withMessage("timeToLive must be a positive duration"); }
@DisplayName("createLeaseFrame throws NullPointerException with null timeToLive") @Test void createLeaseFrameNullTimeToLive() { assertThatNullPointerException() .isThrownBy(() -> createLeaseFrame(DEFAULT, null, 1, null)) .withMessage("timeToLive must not be null"); }
return createSetupFrame(byteBuf); case LEASE: return createLeaseFrame(byteBuf); case KEEPALIVE: return createKeepaliveFrame(byteBuf);
return createSetupFrame(byteBuf); case LEASE: return createLeaseFrame(byteBuf); case KEEPALIVE: return createKeepaliveFrame(byteBuf);