@@ -92,6 +92,105 @@ TEST_F(NcclLoggerTest, LogDisplay) {
9292 finishLogging ();
9393}
9494
95+ TEST_F (NcclLoggerTest, GetLastCommsErrorTest) {
96+ auto debugGuard = EnvRAII (NCCL_DEBUG, std::string{" INFO" });
97+ ncclResetDebugInit ();
98+
99+ initLogging ();
100+
101+ // Initially, the last error should be empty
102+ auto lastError = meta::comms::logger::getLastCommsError ();
103+ EXPECT_THAT (lastError, ::testing::StrEq (" " ));
104+
105+ // Log an info message - should not update last error
106+ std::string infoMsg = " INFO MESSAGE" ;
107+ INFO (NCCL_ALL, " %s" , infoMsg.c_str ());
108+ sleep (1 );
109+ lastError = meta::comms::logger::getLastCommsError ();
110+ EXPECT_THAT (lastError, ::testing::StrEq (" " ));
111+
112+ // Log a warning message - should not update last error
113+ std::string warnMsg = " WARN MESSAGE" ;
114+ WARN (" %s" , warnMsg.c_str ());
115+ sleep (1 );
116+ lastError = meta::comms::logger::getLastCommsError ();
117+ EXPECT_THAT (lastError, ::testing::StrEq (" " ));
118+
119+ // Log an error message - should update last error
120+ std::string errorMsg = " ERROR MESSAGE" ;
121+ ERR (" %s" , errorMsg.c_str ());
122+ sleep (1 );
123+ lastError = meta::comms::logger::getLastCommsError ();
124+ EXPECT_THAT (lastError, ::testing::StrEq (errorMsg));
125+
126+ // Log another error message - should update to the new error
127+ std::string errorMsg2 = " SECOND ERROR MESSAGE" ;
128+ ERR (" %s" , errorMsg2.c_str ());
129+ sleep (1 );
130+ lastError = meta::comms::logger::getLastCommsError ();
131+ EXPECT_THAT (lastError, ::testing::StrEq (errorMsg2));
132+
133+ // Log info and warn - last error should remain unchanged
134+ INFO (NCCL_ALL, " Another info" );
135+ WARN (" Another warn" );
136+ sleep (1 );
137+ lastError = meta::comms::logger::getLastCommsError ();
138+ EXPECT_THAT (lastError, ::testing::StrEq (errorMsg2));
139+
140+ finishLogging ();
141+ }
142+
143+ TEST_F (NcclLoggerTest, GetLastCommsErrorMultilineTest) {
144+ auto debugGuard = EnvRAII (NCCL_DEBUG, std::string{" INFO" });
145+ ncclResetDebugInit ();
146+
147+ initLogging ();
148+
149+ // Log a multiline error message
150+ std::string multilineError = " First line\n Second line\n Third line" ;
151+ ERR (" %s" , multilineError.c_str ());
152+ sleep (1 );
153+
154+ auto lastError = meta::comms::logger::getLastCommsError ();
155+ EXPECT_THAT (lastError, ::testing::StrEq (multilineError));
156+
157+ finishLogging ();
158+ }
159+
160+ TEST_F (NcclLoggerTest, GetLastCommsErrorLongMessageTest) {
161+ auto debugGuard = EnvRAII (NCCL_DEBUG, std::string{" INFO" });
162+ ncclResetDebugInit ();
163+
164+ initLogging ();
165+
166+ // Create a long error message (but within the 1024 char buffer)
167+ std::string longError (500 , ' X' );
168+ ERR (" %s" , longError.c_str ());
169+ sleep (1 );
170+
171+ auto lastError = meta::comms::logger::getLastCommsError ();
172+ EXPECT_THAT (lastError, ::testing::StrEq (longError));
173+
174+ finishLogging ();
175+ }
176+
177+ TEST_F (NcclLoggerTest, GetLastCommsErrorLongMessageTestXLOG) {
178+ auto debugGuard = EnvRAII (NCCL_DEBUG, std::string{" INFO" });
179+ ncclResetDebugInit ();
180+
181+ initLogging ();
182+
183+ // Create a long error message (but within the 1024 char buffer)
184+ std::string longError (500 , ' X' );
185+ XLOG (ERR) << longError;
186+ sleep (1 );
187+
188+ auto lastError = meta::comms::logger::getLastCommsError ();
189+ EXPECT_THAT (lastError, ::testing::StrEq (longError));
190+
191+ finishLogging ();
192+ }
193+
95194TEST_F (NcclLoggerTest, WarnLogTest) {
96195 auto debugGuard = EnvRAII (NCCL_DEBUG, std::string{" WARN" });
97196 ncclResetDebugInit ();
0 commit comments