|
| 1 | +namespace Serilog.Sinks.MongoDB.Tests; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Tests for MongoDB error handling in MongoDbHelper. |
| 5 | +/// These tests verify that PR #99 correctly handles MongoDB exceptions using error codes |
| 6 | +/// instead of string matching, ensuring compatibility across MongoDB versions. |
| 7 | +/// </summary> |
| 8 | +[TestFixture] |
| 9 | +[NonParallelizable] |
| 10 | +public class MongoDbHelperErrorHandlingTests |
| 11 | +{ |
| 12 | + private static string MongoConnectionString => MongoTestFixture.ConnectionString; |
| 13 | + |
| 14 | + private const string MongoDatabaseName = "mongodb-sink-error-handling-tests"; |
| 15 | + |
| 16 | + private const string MongoCollectionName = "test-collection"; |
| 17 | + |
| 18 | + private static (MongoClient, IMongoDatabase) GetDatabase() |
| 19 | + { |
| 20 | + var mongoClient = new MongoClient(MongoConnectionString); |
| 21 | + return (mongoClient, mongoClient.GetDatabase(MongoDatabaseName)); |
| 22 | + } |
| 23 | + |
| 24 | + [TearDown] |
| 25 | + public void Cleanup() |
| 26 | + { |
| 27 | + var mongoClient = new MongoClient(MongoConnectionString); |
| 28 | + } |
| 29 | + |
| 30 | + #region VerifyCollectionExists Tests |
| 31 | + |
| 32 | + [Test] |
| 33 | + public void VerifyCollectionExists_WhenCollectionDoesNotExist_ShouldCreateCollection() |
| 34 | + { |
| 35 | + // Arrange |
| 36 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 37 | + var collectionName = $"{MongoCollectionName}_new"; |
| 38 | + |
| 39 | + // Act |
| 40 | + mongoDatabase.VerifyCollectionExists(collectionName); |
| 41 | + |
| 42 | + // Assert |
| 43 | + var collectionExists = mongoDatabase.CollectionExists(collectionName); |
| 44 | + collectionExists.Should().BeTrue("Collection should be created when it doesn't exist"); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void VerifyCollectionExists_WhenCollectionAlreadyExists_ShouldNotThrowException() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 52 | + var collectionName = $"{MongoCollectionName}_exists"; |
| 53 | + |
| 54 | + // Create the collection first |
| 55 | + mongoDatabase.CreateCollection(collectionName); |
| 56 | + |
| 57 | + // Act & Assert - Should not throw when collection already exists |
| 58 | + var act = () => mongoDatabase.VerifyCollectionExists(collectionName); |
| 59 | + act.Should().NotThrow("VerifyCollectionExists should handle existing collections gracefully"); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public void VerifyCollectionExists_WithRaceCondition_ShouldHandleNamespaceExistsError() |
| 64 | + { |
| 65 | + // Arrange |
| 66 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 67 | + var collectionName = $"{MongoCollectionName}_race"; |
| 68 | + |
| 69 | + // Act & Assert - Simulate race condition by calling verify concurrently |
| 70 | + // This forces actual NamespaceExists exceptions when multiple threads |
| 71 | + // try to create the same collection simultaneously |
| 72 | + var act = () => Parallel.Invoke( |
| 73 | + () => mongoDatabase.VerifyCollectionExists(collectionName), |
| 74 | + () => mongoDatabase.VerifyCollectionExists(collectionName), |
| 75 | + () => mongoDatabase.VerifyCollectionExists(collectionName) |
| 76 | + ); |
| 77 | + |
| 78 | + act.Should().NotThrow("Should handle NamespaceExists error code gracefully during concurrent creation"); |
| 79 | + |
| 80 | + // Verify the collection was created successfully |
| 81 | + mongoDatabase.CollectionExists(collectionName).Should().BeTrue(); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public void VerifyCollectionExists_WithCollectionCreationOptions_ShouldCreateWithOptions() |
| 86 | + { |
| 87 | + // Arrange |
| 88 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 89 | + var collectionName = $"{MongoCollectionName}_with_options"; |
| 90 | + var options = new CreateCollectionOptions |
| 91 | + { |
| 92 | + Capped = true, |
| 93 | + MaxSize = 1024 * 1024, // 1MB |
| 94 | + MaxDocuments = 1000 |
| 95 | + }; |
| 96 | + |
| 97 | + // Act |
| 98 | + mongoDatabase.VerifyCollectionExists(collectionName, options); |
| 99 | + |
| 100 | + // Assert |
| 101 | + var collectionExists = mongoDatabase.CollectionExists(collectionName); |
| 102 | + collectionExists.Should().BeTrue("Collection should be created with options"); |
| 103 | + } |
| 104 | + |
| 105 | + #endregion |
| 106 | + |
| 107 | + #region VerifyExpireTTLSetup Tests |
| 108 | + |
| 109 | + [Test] |
| 110 | + public void VerifyExpireTTLSetup_WhenNoIndexExists_ShouldCreateTTLIndex() |
| 111 | + { |
| 112 | + // Arrange |
| 113 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 114 | + var collectionName = $"{MongoCollectionName}_ttl_new"; |
| 115 | + var expireTtl = TimeSpan.FromMinutes(30); |
| 116 | + |
| 117 | + // Create the collection first |
| 118 | + mongoDatabase.CreateCollection(collectionName); |
| 119 | + |
| 120 | + // Act |
| 121 | + mongoDatabase.VerifyExpireTTLSetup(collectionName, expireTtl); |
| 122 | + |
| 123 | + // Assert |
| 124 | + var collection = mongoDatabase.GetCollection<LogEntry>(collectionName); |
| 125 | + var indexes = collection.Indexes.List().ToList(); |
| 126 | + var ttlIndex = indexes.FirstOrDefault(idx => |
| 127 | + idx.Contains("name") && idx["name"].AsString == "serilog_sink_expired_ttl"); |
| 128 | + |
| 129 | + ttlIndex.Should().NotBeNull("TTL index should be created"); |
| 130 | + // Convert to long to handle MongoDB's potential Int64 or Double serialization across versions |
| 131 | + Convert.ToInt64(ttlIndex!["expireAfterSeconds"].ToDouble()) |
| 132 | + .Should().Be((long)expireTtl.TotalSeconds); |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + public void VerifyExpireTTLSetup_WhenIndexExistsWithSameOptions_ShouldNotThrow() |
| 138 | + { |
| 139 | + // Arrange |
| 140 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 141 | + var collectionName = $"{MongoCollectionName}_ttl_same"; |
| 142 | + var expireTtl = TimeSpan.FromMinutes(30); |
| 143 | + |
| 144 | + mongoDatabase.CreateCollection(collectionName); |
| 145 | + var collection = mongoDatabase.GetCollection<LogEntry>(collectionName); |
| 146 | + |
| 147 | + // Create the TTL index first |
| 148 | + var indexKeysDefinition = Builders<LogEntry>.IndexKeys.Ascending(s => s.UtcTimeStamp); |
| 149 | + var indexOptions = new CreateIndexOptions |
| 150 | + { |
| 151 | + Name = "serilog_sink_expired_ttl", |
| 152 | + ExpireAfter = expireTtl |
| 153 | + }; |
| 154 | + collection.Indexes.CreateOne(new CreateIndexModel<LogEntry>(indexKeysDefinition, indexOptions)); |
| 155 | + |
| 156 | + // Act & Assert - Should not throw when index exists with same options |
| 157 | + var act = () => mongoDatabase.VerifyExpireTTLSetup(collectionName, expireTtl); |
| 158 | + act.Should().NotThrow("Should handle existing TTL index with same options"); |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + [Test] |
| 163 | + public void VerifyExpireTTLSetup_WhenIndexExistsWithDifferentOptions_ShouldRecreateIndex() |
| 164 | + { |
| 165 | + // Arrange |
| 166 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 167 | + var collectionName = $"{MongoCollectionName}_ttl_different"; |
| 168 | + var originalExpireTtl = TimeSpan.FromMinutes(30); |
| 169 | + var newExpireTtl = TimeSpan.FromMinutes(60); |
| 170 | + |
| 171 | + mongoDatabase.CreateCollection(collectionName); |
| 172 | + var collection = mongoDatabase.GetCollection<LogEntry>(collectionName); |
| 173 | + |
| 174 | + // Create the TTL index with original expiration |
| 175 | + var indexKeysDefinition = Builders<LogEntry>.IndexKeys.Ascending(s => s.UtcTimeStamp); |
| 176 | + var originalIndexOptions = new CreateIndexOptions |
| 177 | + { |
| 178 | + Name = "serilog_sink_expired_ttl", |
| 179 | + ExpireAfter = originalExpireTtl |
| 180 | + }; |
| 181 | + collection.Indexes.CreateOne(new CreateIndexModel<LogEntry>(indexKeysDefinition, originalIndexOptions)); |
| 182 | + |
| 183 | + // Act - Update with different expiration time |
| 184 | + // This should trigger IndexOptionsConflict error code and handle it by dropping and recreating |
| 185 | + mongoDatabase.VerifyExpireTTLSetup(collectionName, newExpireTtl); |
| 186 | + |
| 187 | + // Assert |
| 188 | + var indexes = collection.Indexes.List().ToList(); |
| 189 | + var ttlIndex = indexes.FirstOrDefault(idx => |
| 190 | + idx.Contains("name") && idx["name"].AsString == "serilog_sink_expired_ttl"); |
| 191 | + |
| 192 | + ttlIndex.Should().NotBeNull("TTL index should still exist"); |
| 193 | + // Convert to long to handle MongoDB's potential Int64 or Double serialization across versions |
| 194 | + Convert.ToInt64(ttlIndex!["expireAfterSeconds"].ToDouble()) |
| 195 | + .Should().Be((long)newExpireTtl.TotalSeconds, |
| 196 | + "Index should be recreated with new expiration time"); |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | + [Test] |
| 201 | + public void VerifyExpireTTLSetup_WhenNullExpireTTL_ShouldRemoveIndex() |
| 202 | + { |
| 203 | + // Arrange |
| 204 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 205 | + var collectionName = $"{MongoCollectionName}_ttl_remove"; |
| 206 | + var expireTtl = TimeSpan.FromMinutes(30); |
| 207 | + |
| 208 | + mongoDatabase.CreateCollection(collectionName); |
| 209 | + var collection = mongoDatabase.GetCollection<LogEntry>(collectionName); |
| 210 | + |
| 211 | + // Create the TTL index first |
| 212 | + var indexKeysDefinition = Builders<LogEntry>.IndexKeys.Ascending(s => s.UtcTimeStamp); |
| 213 | + var indexOptions = new CreateIndexOptions |
| 214 | + { |
| 215 | + Name = "serilog_sink_expired_ttl", |
| 216 | + ExpireAfter = expireTtl |
| 217 | + }; |
| 218 | + collection.Indexes.CreateOne(new CreateIndexModel<LogEntry>(indexKeysDefinition, indexOptions)); |
| 219 | + |
| 220 | + // Act - Call with null to remove the index |
| 221 | + mongoDatabase.VerifyExpireTTLSetup(collectionName, null); |
| 222 | + |
| 223 | + // Assert |
| 224 | + var indexes = collection.Indexes.List().ToList(); |
| 225 | + var ttlIndex = indexes.FirstOrDefault(idx => |
| 226 | + idx.Contains("name") && idx["name"].AsString == "serilog_sink_expired_ttl"); |
| 227 | + |
| 228 | + ttlIndex.Should().BeNull("TTL index should be removed when expireTTL is null"); |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + [Test] |
| 233 | + public void VerifyExpireTTLSetup_WhenNullExpireTTLAndNoIndex_ShouldNotThrow() |
| 234 | + { |
| 235 | + // Arrange |
| 236 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 237 | + var collectionName = $"{MongoCollectionName}_ttl_null_no_index"; |
| 238 | + |
| 239 | + mongoDatabase.CreateCollection(collectionName); |
| 240 | + |
| 241 | + // Act & Assert - Should not throw when trying to remove non-existent index |
| 242 | + var act = () => mongoDatabase.VerifyExpireTTLSetup(collectionName, null); |
| 243 | + act.Should().NotThrow("Should handle removal of non-existent index gracefully"); |
| 244 | + |
| 245 | + } |
| 246 | + |
| 247 | + [Test] |
| 248 | + public void VerifyExpireTTLSetup_MultipleTimes_ShouldBeIdempotent() |
| 249 | + { |
| 250 | + // Arrange |
| 251 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 252 | + var collectionName = $"{MongoCollectionName}_ttl_idempotent"; |
| 253 | + var expireTtl = TimeSpan.FromMinutes(45); |
| 254 | + |
| 255 | + mongoDatabase.CreateCollection(collectionName); |
| 256 | + |
| 257 | + // Act - Call multiple times with same expiration |
| 258 | + mongoDatabase.VerifyExpireTTLSetup(collectionName, expireTtl); |
| 259 | + mongoDatabase.VerifyExpireTTLSetup(collectionName, expireTtl); |
| 260 | + mongoDatabase.VerifyExpireTTLSetup(collectionName, expireTtl); |
| 261 | + |
| 262 | + // Assert |
| 263 | + var collection = mongoDatabase.GetCollection<LogEntry>(collectionName); |
| 264 | + var indexes = collection.Indexes.List().ToList(); |
| 265 | + var ttlIndexes = indexes.Where(idx => |
| 266 | + idx.Contains("name") && idx["name"].AsString == "serilog_sink_expired_ttl").ToList(); |
| 267 | + |
| 268 | + ttlIndexes.Should().HaveCount(1, "Should only have one TTL index even after multiple calls"); |
| 269 | + // Convert to long to handle MongoDB's potential Int64 or Double serialization across versions |
| 270 | + Convert.ToInt64(ttlIndexes[0]["expireAfterSeconds"].ToDouble()) |
| 271 | + .Should().Be((long)expireTtl.TotalSeconds); |
| 272 | + |
| 273 | + } |
| 274 | + |
| 275 | + #endregion |
| 276 | + |
| 277 | + #region Integration Tests - Error Code Validation |
| 278 | + |
| 279 | + /// <summary> |
| 280 | + /// This test validates that the MongoDB driver actually returns CodeName "NamespaceExists" |
| 281 | + /// for error code 48 when a collection already exists. This ensures our fix in PR #99 |
| 282 | + /// is compatible with the actual MongoDB behavior. |
| 283 | + /// Note: We need to use CreateCollectionOptions to force MongoDB to throw the exception, |
| 284 | + /// as calling CreateCollection without options on an existing collection is idempotent. |
| 285 | + /// </summary> |
| 286 | + [Test] |
| 287 | + public void MongoCommandException_WhenCollectionExists_ShouldHaveNamespaceExistsCodeName() |
| 288 | + { |
| 289 | + // Arrange |
| 290 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 291 | + var collectionName = $"{MongoCollectionName}_namespace_error"; |
| 292 | + |
| 293 | + // Create the collection first with specific options |
| 294 | + var options = new CreateCollectionOptions |
| 295 | + { |
| 296 | + Capped = true, |
| 297 | + MaxSize = 1024 * 1024 |
| 298 | + }; |
| 299 | + mongoDatabase.CreateCollection(collectionName, options); |
| 300 | + |
| 301 | + // Act & Assert - Try to create the same collection again with different options |
| 302 | + MongoCommandException? caughtException = null; |
| 303 | + try |
| 304 | + { |
| 305 | + var differentOptions = new CreateCollectionOptions |
| 306 | + { |
| 307 | + Capped = false |
| 308 | + }; |
| 309 | + mongoDatabase.CreateCollection(collectionName, differentOptions); |
| 310 | + } |
| 311 | + catch (MongoCommandException ex) |
| 312 | + { |
| 313 | + caughtException = ex; |
| 314 | + } |
| 315 | + |
| 316 | + caughtException.Should().NotBeNull("Should throw MongoCommandException when creating duplicate collection"); |
| 317 | + caughtException!.CodeName.Should().Be("NamespaceExists", |
| 318 | + "MongoDB should return CodeName 'NamespaceExists' for duplicate collection"); |
| 319 | + caughtException.Code.Should().Be(48, "Error code should be 48 for NamespaceExists"); |
| 320 | + |
| 321 | + } |
| 322 | + |
| 323 | + /// <summary> |
| 324 | + /// This test validates that the MongoDB driver actually returns CodeName "IndexOptionsConflict" |
| 325 | + /// for error code 85 when an index exists with different options. This ensures our fix in PR #99 |
| 326 | + /// is compatible with the actual MongoDB behavior. |
| 327 | + /// </summary> |
| 328 | + [Test] |
| 329 | + public void MongoCommandException_WhenIndexExistsWithDifferentOptions_ShouldHaveIndexOptionsConflictCodeName() |
| 330 | + { |
| 331 | + // Arrange |
| 332 | + var (mongoClient, mongoDatabase) = GetDatabase(); |
| 333 | + var collectionName = $"{MongoCollectionName}_index_error"; |
| 334 | + |
| 335 | + mongoDatabase.CreateCollection(collectionName); |
| 336 | + var collection = mongoDatabase.GetCollection<LogEntry>(collectionName); |
| 337 | + |
| 338 | + // Create index with one expiration time |
| 339 | + var indexKeysDefinition = Builders<LogEntry>.IndexKeys.Ascending(s => s.UtcTimeStamp); |
| 340 | + var indexOptions = new CreateIndexOptions |
| 341 | + { |
| 342 | + Name = "test_ttl_index", |
| 343 | + ExpireAfter = TimeSpan.FromMinutes(30) |
| 344 | + }; |
| 345 | + collection.Indexes.CreateOne(new CreateIndexModel<LogEntry>(indexKeysDefinition, indexOptions)); |
| 346 | + |
| 347 | + // Act & Assert - Try to create same index with different expiration |
| 348 | + MongoCommandException? caughtException = null; |
| 349 | + try |
| 350 | + { |
| 351 | + var differentIndexOptions = new CreateIndexOptions |
| 352 | + { |
| 353 | + Name = "test_ttl_index", |
| 354 | + ExpireAfter = TimeSpan.FromMinutes(60) |
| 355 | + }; |
| 356 | + collection.Indexes.CreateOne(new CreateIndexModel<LogEntry>(indexKeysDefinition, differentIndexOptions)); |
| 357 | + } |
| 358 | + catch (MongoCommandException ex) |
| 359 | + { |
| 360 | + caughtException = ex; |
| 361 | + } |
| 362 | + |
| 363 | + caughtException.Should().NotBeNull("Should throw MongoCommandException when creating index with different options"); |
| 364 | + caughtException!.CodeName.Should().Be("IndexOptionsConflict", |
| 365 | + "MongoDB should return CodeName 'IndexOptionsConflict' for index with different options"); |
| 366 | + caughtException.Code.Should().Be(85, "Error code should be 85 for IndexOptionsConflict"); |
| 367 | + |
| 368 | + } |
| 369 | + |
| 370 | + #endregion |
| 371 | +} |
0 commit comments