Skip to content

Commit

Permalink
[Java] Ensure that deadline checking is wrapping safe and use separat…
Browse files Browse the repository at this point in the history
…e variable for disabling status messages.
  • Loading branch information
mikeb01 committed Nov 11, 2024
1 parent fd58598 commit b3379ae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
42 changes: 42 additions & 0 deletions aeron-client/src/test/java/io/aeron/TimestampUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2014-2024 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.aeron;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class TimestampUtilTest
{
static boolean hasReachedDeadline(final long now, final long deadline)
{
return deadline - now < 0;
}

@Test
void shouldCorrectlyReachDeadline()
{
assertFalse(hasReachedDeadline(1, 2));
assertFalse(hasReachedDeadline(2, 2));
assertTrue(hasReachedDeadline(3, 2));
assertFalse(hasReachedDeadline(Long.MAX_VALUE, Long.MAX_VALUE));
//noinspection NumericOverflow
assertFalse(hasReachedDeadline(Long.MAX_VALUE, Long.MAX_VALUE + 1));
assertFalse(hasReachedDeadline(Long.MIN_VALUE, Long.MIN_VALUE + 1));
assertFalse(hasReachedDeadline(-1, -1 + Long.MAX_VALUE));
assertTrue(hasReachedDeadline(-1, Long.MAX_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ enum State
private final int initialTermId;
private final short flags;
private final boolean isReliable;
private boolean smEnabled = true;

private boolean isRebuilding = true;
private volatile boolean isReceiverReleaseTriggered = false;
Expand Down Expand Up @@ -721,7 +722,7 @@ int sendPendingStatusMessage(final long nowNs)
{
int workCount = 0;
final long changeNumber = (long)END_SM_CHANGE_VH.getAcquire(this);
final boolean hasSmTimedOut = nowNs > nextSmDeadlineNs;
final boolean hasSmTimedOut = smEnabled && nextSmDeadlineNs - nowNs < 0;

if (null != rejectionReason)
{
Expand Down Expand Up @@ -943,7 +944,7 @@ void stopStatusMessagesIfNotActive()
{
if (State.ACTIVE != state)
{
nextSmDeadlineNs = Long.MAX_VALUE;
smEnabled = false;
}
}

Expand Down

0 comments on commit b3379ae

Please sign in to comment.