-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Peterson <[email protected]>
- Loading branch information
1 parent
0ed0c9a
commit 0889c17
Showing
4 changed files
with
105 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
server/src/test/java/com/hedera/block/server/util/TestClock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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 | ||
* | ||
* http://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 com.hedera.block.server.util; | ||
|
||
import java.time.Instant; | ||
import java.time.InstantSource; | ||
|
||
public class TestClock implements InstantSource { | ||
|
||
private int index; | ||
private final Long[] millis; | ||
private final System.Logger LOGGER = System.getLogger(getClass().getName()); | ||
|
||
public TestClock(Long... millis) { | ||
if (millis.length == 0) { | ||
throw new IllegalArgumentException("At least 1 argument is required"); | ||
} | ||
|
||
this.millis = millis; | ||
} | ||
|
||
@Override | ||
public long millis() { | ||
LOGGER.log(System.Logger.Level.INFO, "millis() called"); | ||
long value = millis[index]; | ||
|
||
// cycle through the provided millis | ||
// and wrap around if necessary | ||
int temp = index + 1; | ||
index = (temp % millis.length == 0) ? 0 : temp; | ||
return value; | ||
} | ||
|
||
@Override | ||
public Instant instant() { | ||
return null; | ||
} | ||
|
||
public static InstantSource buildClockInsideWindow(long testTime, long timeoutThresholdMillis) { | ||
return new TestClock(testTime, testTime + timeoutThresholdMillis - 1); | ||
} | ||
|
||
public static InstantSource buildClockOutsideWindow( | ||
long testTime, long timeoutThresholdMillis) { | ||
return new TestClock(testTime, testTime + timeoutThresholdMillis + 1); | ||
} | ||
} |