|
1 | 1 | #include <cstdint> |
2 | 2 | #include <fstream> |
3 | 3 |
|
| 4 | +#include "envoy/common/exception.h" |
| 5 | + |
4 | 6 | #include "source/common/common/assert.h" |
5 | 7 | #include "source/common/event/dispatcher_impl.h" |
6 | 8 | #include "source/common/filesystem/watcher_impl.h" |
7 | 9 |
|
8 | 10 | #include "test/test_common/environment.h" |
| 11 | +#include "test/test_common/logging.h" |
9 | 12 | #include "test/test_common/utility.h" |
10 | 13 |
|
11 | 14 | #include "gmock/gmock.h" |
@@ -214,5 +217,92 @@ TEST_F(WatcherImplTest, SymlinkAtomicRename) { |
214 | 217 | } |
215 | 218 | #endif |
216 | 219 |
|
| 220 | +// Test that callback returning error status is logged and doesn't crash. |
| 221 | +TEST_F(WatcherImplTest, CallbackReturnsErrorStatus) { |
| 222 | + Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher(); |
| 223 | + |
| 224 | + TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test")); |
| 225 | + std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target")); |
| 226 | + |
| 227 | + WatchCallback callback; |
| 228 | + EXPECT_CALL(callback, called(Watcher::Events::Modified)); |
| 229 | + ASSERT_TRUE(watcher |
| 230 | + ->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_target"), |
| 231 | + Watcher::Events::Modified, |
| 232 | + [&](uint32_t events) { |
| 233 | + callback.called(events); |
| 234 | + dispatcher_->exit(); |
| 235 | + // Return an error status - should be logged but not crash. |
| 236 | + return absl::InternalError("simulated callback error"); |
| 237 | + }) |
| 238 | + .ok()); |
| 239 | + dispatcher_->run(Event::Dispatcher::RunType::NonBlock); |
| 240 | + |
| 241 | + EXPECT_LOG_CONTAINS("warn", "Filesystem watch callback for", file << "text" << std::flush; |
| 242 | + file.close(); dispatcher_->run(Event::Dispatcher::RunType::Block);); |
| 243 | +} |
| 244 | + |
| 245 | +// Test that callback throwing exception is caught and logged. |
| 246 | +TEST_F(WatcherImplTest, CallbackThrowsException) { |
| 247 | + Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher(); |
| 248 | + |
| 249 | + TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test")); |
| 250 | + std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target")); |
| 251 | + |
| 252 | + WatchCallback callback; |
| 253 | + EXPECT_CALL(callback, called(Watcher::Events::Modified)); |
| 254 | + ASSERT_TRUE(watcher |
| 255 | + ->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_target"), |
| 256 | + Watcher::Events::Modified, |
| 257 | + [&](uint32_t events) -> absl::Status { |
| 258 | + callback.called(events); |
| 259 | + dispatcher_->exit(); |
| 260 | + // Throw an exception - should be caught and logged. |
| 261 | + throw EnvoyException("simulated callback exception"); |
| 262 | + }) |
| 263 | + .ok()); |
| 264 | + dispatcher_->run(Event::Dispatcher::RunType::NonBlock); |
| 265 | + |
| 266 | + EXPECT_LOG_CONTAINS("warn", "threw exception", file << "text" << std::flush; file.close(); |
| 267 | + dispatcher_->run(Event::Dispatcher::RunType::Block);); |
| 268 | +} |
| 269 | + |
| 270 | +// Test that multiple callbacks can fail without affecting each other. |
| 271 | +TEST_F(WatcherImplTest, MultipleCallbacksWithErrors) { |
| 272 | + Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher(); |
| 273 | + |
| 274 | + TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test")); |
| 275 | + std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target")); |
| 276 | + |
| 277 | + int callback_count = 0; |
| 278 | + ASSERT_TRUE(watcher |
| 279 | + ->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_target"), |
| 280 | + Watcher::Events::Modified, |
| 281 | + [&](uint32_t) { |
| 282 | + callback_count++; |
| 283 | + if (callback_count >= 2) { |
| 284 | + dispatcher_->exit(); |
| 285 | + } |
| 286 | + // First callback returns error, second returns OK. |
| 287 | + if (callback_count == 1) { |
| 288 | + return absl::InternalError("first callback error"); |
| 289 | + } |
| 290 | + return absl::OkStatus(); |
| 291 | + }) |
| 292 | + .ok()); |
| 293 | + dispatcher_->run(Event::Dispatcher::RunType::NonBlock); |
| 294 | + |
| 295 | + // Trigger first modification. The first callback returns error, but watcher continues. |
| 296 | + file << "text1" << std::flush; |
| 297 | + dispatcher_->run(Event::Dispatcher::RunType::NonBlock); |
| 298 | + |
| 299 | + // Trigger second modification. It should still work. |
| 300 | + file << "text2" << std::flush; |
| 301 | + file.close(); |
| 302 | + dispatcher_->run(Event::Dispatcher::RunType::Block); |
| 303 | + |
| 304 | + EXPECT_EQ(2, callback_count); |
| 305 | +} |
| 306 | + |
217 | 307 | } // namespace Filesystem |
218 | 308 | } // namespace Envoy |
0 commit comments