22
22
import java .nio .file .Files ;
23
23
import java .nio .file .NoSuchFileException ;
24
24
import java .nio .file .Path ;
25
+ import java .util .Optional ;
25
26
import org .apache .tuweni .bytes .Bytes ;
26
27
import org .junit .jupiter .api .Test ;
27
28
import org .junit .jupiter .api .condition .DisabledOnOs ;
28
29
import org .junit .jupiter .api .condition .OS ;
29
30
import org .junit .jupiter .api .io .TempDir ;
30
31
import tech .pegasys .teku .infrastructure .time .StubTimeProvider ;
32
+ import tech .pegasys .teku .infrastructure .unsigned .UInt64 ;
31
33
import tech .pegasys .teku .spec .TestSpecFactory ;
32
34
import tech .pegasys .teku .spec .datastructures .blocks .SignedBeaconBlock ;
33
35
import tech .pegasys .teku .spec .util .DataStructureUtil ;
@@ -41,11 +43,12 @@ class DebugDataDumperTest {
41
43
void saveGossipMessageDecodingError_shouldSaveToFile (@ TempDir Path tempDir ) {
42
44
final DebugDataDumper manager = new DebugDataDumper (tempDir , true );
43
45
final Bytes messageBytes = dataStructureUtil .stateBuilderPhase0 ().build ().sszSerialize ();
44
- final String arrivalTimestamp = timeProvider .getTimeInMillis (). toString ( );
46
+ final Optional < UInt64 > arrivalTimestamp = Optional . of ( timeProvider .getTimeInMillis ());
45
47
final String topic = "test_topic" ;
46
48
manager .saveGossipMessageDecodingError ("test_topic" , arrivalTimestamp , messageBytes );
47
49
48
- final String fileName = String .format ("%s.ssz" , arrivalTimestamp );
50
+ final String fileName =
51
+ String .format ("%s.ssz" , DebugDataDumper .formatTimestamp (arrivalTimestamp ));
49
52
final Path expectedFile =
50
53
tempDir
51
54
.resolve ("gossip_messages" )
@@ -59,11 +62,12 @@ void saveGossipMessageDecodingError_shouldSaveToFile(@TempDir Path tempDir) {
59
62
void saveGossipMessageDecodingError_shouldNotSaveToFileWhenDisabled (@ TempDir Path tempDir ) {
60
63
final DebugDataDumper manager = new DebugDataDumper (tempDir , false );
61
64
final Bytes messageBytes = dataStructureUtil .stateBuilderPhase0 ().build ().sszSerialize ();
62
- final String arrivalTimestamp = timeProvider .getTimeInMillis (). toString ( );
65
+ final Optional < UInt64 > arrivalTimestamp = Optional . of ( timeProvider .getTimeInMillis ());
63
66
manager .saveGossipMessageDecodingError ("test_topic" , arrivalTimestamp , messageBytes );
64
67
assertThat (manager .isEnabled ()).isFalse ();
65
68
66
- final String fileName = String .format ("%s.ssz" , arrivalTimestamp );
69
+ final String fileName =
70
+ String .format ("%s.ssz" , DebugDataDumper .formatTimestamp (arrivalTimestamp ));
67
71
final Path expectedFile =
68
72
tempDir .resolve ("gossip_messages" ).resolve ("decoding_error" ).resolve (fileName );
69
73
checkFileNotExist (expectedFile );
@@ -73,11 +77,12 @@ void saveGossipMessageDecodingError_shouldNotSaveToFileWhenDisabled(@TempDir Pat
73
77
void saveGossipRejectedMessageToFile_shouldSaveToFile (@ TempDir Path tempDir ) {
74
78
final DebugDataDumper manager = new DebugDataDumper (tempDir , true );
75
79
final Bytes messageBytes = dataStructureUtil .stateBuilderPhase0 ().build ().sszSerialize ();
76
- final String arrivalTimestamp = timeProvider .getTimeInMillis (). toString ( );
80
+ final Optional < UInt64 > arrivalTimestamp = Optional . of ( timeProvider .getTimeInMillis ());
77
81
final String topic = "test_topic" ;
78
82
manager .saveGossipRejectedMessageToFile ("test_topic" , arrivalTimestamp , messageBytes );
79
83
80
- final String fileName = String .format ("%s.ssz" , arrivalTimestamp );
84
+ final String fileName =
85
+ String .format ("%s.ssz" , DebugDataDumper .formatTimestamp (arrivalTimestamp ));
81
86
final Path expectedFile =
82
87
tempDir .resolve ("gossip_messages" ).resolve ("rejected" ).resolve (topic ).resolve (fileName );
83
88
checkBytesSavedToFile (expectedFile , messageBytes );
@@ -87,7 +92,7 @@ void saveGossipRejectedMessageToFile_shouldSaveToFile(@TempDir Path tempDir) {
87
92
void saveGossipRejectedMessageToFile_shouldNotSaveToFileWhenDisabled (@ TempDir Path tempDir ) {
88
93
final DebugDataDumper manager = new DebugDataDumper (tempDir , false );
89
94
final Bytes messageBytes = dataStructureUtil .stateBuilderPhase0 ().build ().sszSerialize ();
90
- final String arrivalTimestamp = timeProvider .getTimeInMillis (). toString ( );
95
+ final Optional < UInt64 > arrivalTimestamp = Optional . of ( timeProvider .getTimeInMillis ());
91
96
manager .saveGossipRejectedMessageToFile ("test_topic" , arrivalTimestamp , messageBytes );
92
97
assertThat (manager .isEnabled ()).isFalse ();
93
98
@@ -150,6 +155,19 @@ void constructionOfDirectories_shouldDisableWhenFailedToCreate(@TempDir Path tem
150
155
assertThat (manager .isEnabled ()).isFalse ();
151
156
}
152
157
158
+ @ Test
159
+ void formatTimestamp_shouldFormatDate () {
160
+ final String formattedTimestamp =
161
+ DebugDataDumper .formatTimestamp (Optional .of (dataStructureUtil .randomUInt64 ()));
162
+ assertThat (formattedTimestamp ).isEqualTo ("147882977-02-28T19:22:42.956" );
163
+ }
164
+
165
+ @ Test
166
+ void formatTimestamp_shouldReturnConsistentUnknown () {
167
+ final String formattedTimestamp = DebugDataDumper .formatTimestamp (Optional .empty ());
168
+ assertThat (formattedTimestamp ).isEqualTo ("unknown" );
169
+ }
170
+
153
171
private void checkBytesSavedToFile (final Path path , final Bytes expectedBytes ) {
154
172
try {
155
173
final Bytes bytes = Bytes .wrap (Files .readAllBytes (path ));
0 commit comments