Skip to content

Commit

Permalink
Test not save to file when warnings disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh committed Apr 10, 2024
1 parent 882f3ae commit c33baf2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public P2PDumpManager(final Path directory) {

public String saveGossipMessageDecodingError(
final String topic, final String arrivalTimestamp, final Bytes originalMessage) {
if (isIncludeP2pWarnings) {
if (!isIncludeP2pWarnings) {
return "";
}
final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, topic);
Expand All @@ -72,7 +72,7 @@ public String saveGossipMessageDecodingError(

public String saveGossipRejectedMessageToFile(
final String topic, final String arrivalTimestamp, final Bytes decodedMessage) {
if (isIncludeP2pWarnings) {
if (!isIncludeP2pWarnings) {
return "";
}
final String fileName = String.format("%s_%s.ssz", arrivalTimestamp, topic);
Expand All @@ -83,7 +83,7 @@ public String saveGossipRejectedMessageToFile(

public String saveInvalidBlockToFile(
final UInt64 slot, final Bytes32 blockRoot, final Bytes blockSsz) {
if (isIncludeP2pWarnings) {
if (!isIncludeP2pWarnings) {
return "";
}
final String fileName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import tech.pegasys.teku.infrastructure.logging.LoggingConfig;
import tech.pegasys.teku.infrastructure.logging.LoggingConfigurator;
import tech.pegasys.teku.infrastructure.time.StubTimeProvider;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
Expand All @@ -46,6 +48,18 @@ void saveGossipMessageDecodingError_shouldSaveToFile(@TempDir Path tempDir) {
checkBytesSavedToFile(expectedFile, messageBytes);
}

@Test
void saveGossipMessageDecodingError_shouldNotSaveToFileWhenWarningsDisabled(
@TempDir Path tempDir) {
disableP2pWarnings();
final P2PDumpManager manager = new P2PDumpManager(tempDir);
final Bytes messageBytes = dataStructureUtil.stateBuilderPhase0().build().sszSerialize();
final String arrivalTimestamp = timeProvider.getTimeInMillis().toString();
final String file =
manager.saveGossipMessageDecodingError("test_topic", arrivalTimestamp, messageBytes);
assertThat(file).isEqualTo("");
}

@Test
void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
final P2PDumpManager manager = new P2PDumpManager(tempDir);
Expand All @@ -60,6 +74,18 @@ void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
checkBytesSavedToFile(expectedFile, messageBytes);
}

@Test
void saveGossipRejectedMessageToFile_shouldNotSaveToFileWhenWarningsDisabled(
@TempDir Path tempDir) {
disableP2pWarnings();
final P2PDumpManager manager = new P2PDumpManager(tempDir);
final Bytes messageBytes = dataStructureUtil.stateBuilderPhase0().build().sszSerialize();
final String arrivalTimestamp = timeProvider.getTimeInMillis().toString();
final String file =
manager.saveGossipRejectedMessageToFile("test_topic", arrivalTimestamp, messageBytes);
assertThat(file).isEqualTo("");
}

@Test
void saveInvalidBlockToFile_shouldSaveToFile(@TempDir Path tempDir) {
final P2PDumpManager manager = new P2PDumpManager(tempDir);
Expand All @@ -75,6 +101,16 @@ void saveInvalidBlockToFile_shouldSaveToFile(@TempDir Path tempDir) {
checkBytesSavedToFile(expectedFile, block.sszSerialize());
}

@Test
void saveInvalidBlockToFile_shouldNotSaveToFileWhenWarningsDisabled(@TempDir Path tempDir) {
disableP2pWarnings();
final P2PDumpManager manager = new P2PDumpManager(tempDir);
final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock();
final String file =
manager.saveInvalidBlockToFile(block.getSlot(), block.getRoot(), block.sszSerialize());
assertThat(file).isEqualTo("");
}

private void checkBytesSavedToFile(final Path path, final Bytes expectedBytes) {
try {
final Bytes bytes = Bytes.wrap(Files.readAllBytes(path));
Expand All @@ -83,4 +119,13 @@ private void checkBytesSavedToFile(final Path path, final Bytes expectedBytes) {
fail();
}
}

private void disableP2pWarnings() {
LoggingConfigurator.update(
LoggingConfig.builder()
.logDirectory(".")
.logFileNamePrefix("prefix")
.includeP2pWarningsEnabled(false)
.build());
}
}

0 comments on commit c33baf2

Please sign in to comment.