mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-24 16:43:17 +08:00
852 lines
32 KiB
C#
852 lines
32 KiB
C#
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace OrmTest
|
||
{
|
||
/// <summary>
|
||
/// ═══════════════════════════════════════════════════════════════════════════════
|
||
/// ASYNC INSERT TEST SUITE - Comprehensive Testing for SqlSugar Async Operations
|
||
/// ═══════════════════════════════════════════════════════════════════════════════
|
||
///
|
||
/// PURPOSE:
|
||
/// Tests all async insert methods in SqlSugar ORM with focus on:
|
||
/// - Basic async insert operations (ExecuteCommandAsync, ExecuteReturnIdentityAsync, etc.)
|
||
/// - Snowflake ID generation for distributed systems
|
||
/// - CancellationToken support (CRITICAL - previously untested)
|
||
/// - Error handling and edge cases
|
||
///
|
||
/// PRIORITY: 🔴 CRITICAL - 97% of async methods were previously untested
|
||
///
|
||
/// USAGE:
|
||
/// // Run all tests
|
||
/// NewUnitTest.AsyncInsert();
|
||
///
|
||
/// // Run individual test
|
||
/// NewUnitTest.AsyncInsert_ExecuteCommandAsync();
|
||
///
|
||
/// TEST COVERAGE:
|
||
/// ✅ ExecuteCommandAsync() - Basic async insert
|
||
/// ✅ ExecuteReturnIdentityAsync() - Return identity value
|
||
/// ✅ ExecuteReturnBigIdentityAsync() - BIGINT identity
|
||
/// ✅ ExecuteReturnEntityAsync() - Return full entity with populated fields
|
||
/// ✅ ExecuteReturnPkListAsync() - Bulk insert with PK list
|
||
/// ✅ ExecuteReturnSnowflakeIdAsync() - Distributed ID generation
|
||
/// ✅ ExecuteReturnSnowflakeIdListAsync() - Bulk Snowflake IDs
|
||
/// ✅ CancellationToken support across all methods
|
||
/// ✅ Transaction support
|
||
/// ✅ Concurrent operations
|
||
/// ✅ Error scenarios
|
||
///
|
||
/// DEPENDENCIES:
|
||
/// - Order entity (with identity column)
|
||
/// - OrderSnowflake entity (long PK without identity)
|
||
/// - OrderItem entity (for navigation property tests)
|
||
///
|
||
/// ═══════════════════════════════════════════════════════════════════════════════
|
||
/// </summary>
|
||
public partial class NewUnitTest
|
||
{
|
||
#region Main Entry Point
|
||
|
||
/// <summary>
|
||
/// Main entry point - Executes all async insert tests
|
||
/// </summary>
|
||
public static void AsyncInsert()
|
||
{
|
||
Console.WriteLine("\n╔════════════════════════════════════════════════════════════════╗");
|
||
Console.WriteLine("║ ASYNC INSERT TEST SUITE - COMPREHENSIVE ║");
|
||
Console.WriteLine("╚════════════════════════════════════════════════════════════════╝\n");
|
||
|
||
try
|
||
{
|
||
// CATEGORY A: Basic Async Operations
|
||
Console.WriteLine("┌─── BASIC ASYNC OPERATIONS ───────────────────────────────────┐\n");
|
||
AsyncInsert_ExecuteCommandAsync();
|
||
AsyncInsert_ExecuteReturnIdentityAsync();
|
||
AsyncInsert_ExecuteReturnBigIdentityAsync();
|
||
AsyncInsert_ExecuteReturnEntityAsync();
|
||
AsyncInsert_ExecuteReturnPkListAsync();
|
||
AsyncInsert_MultipleEntitiesAsync();
|
||
|
||
// CATEGORY B: Snowflake ID Generation
|
||
Console.WriteLine("\n┌─── SNOWFLAKE ID GENERATION ──────────────────────────────────┐\n");
|
||
AsyncInsert_SnowflakeIdBasic();
|
||
AsyncInsert_SnowflakeIdList();
|
||
AsyncInsert_SnowflakeIdUniqueness();
|
||
AsyncInsert_SnowflakeId_Concurrent();
|
||
|
||
// CATEGORY C: CancellationToken Support (CRITICAL)
|
||
Console.WriteLine("\n┌─── CANCELLATION TOKEN SUPPORT (CRITICAL) ────────────────────┐\n");
|
||
AsyncInsert_CancellationToken_Basic();
|
||
AsyncInsert_CancellationToken_Immediate();
|
||
AsyncInsert_CancellationToken_SnowflakeId();
|
||
AsyncInsert_CancellationToken_Transaction();
|
||
|
||
// CATEGORY D: Error Handling
|
||
Console.WriteLine("\n┌─── ERROR HANDLING & EDGE CASES ──────────────────────────────┐\n");
|
||
AsyncInsert_NullEntity();
|
||
AsyncInsert_DuplicateKey();
|
||
AsyncInsert_ConcurrentInserts();
|
||
AsyncInsert_TransactionRollback();
|
||
AsyncInsert_Performance();
|
||
|
||
Console.WriteLine("\n╔════════════════════════════════════════════════════════════════╗");
|
||
Console.WriteLine("║ ✓ ALL ASYNC INSERT TESTS PASSED ║");
|
||
Console.WriteLine("╚════════════════════════════════════════════════════════════════╝\n");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("\n╔════════════════════════════════════════════════════════════════╗");
|
||
Console.WriteLine("║ ✗ TEST SUITE FAILED ║");
|
||
Console.WriteLine("╚════════════════════════════════════════════════════════════════╝");
|
||
Console.WriteLine($"\nError: {ex.Message}\n");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region A. Basic Async Insert Tests
|
||
|
||
/// <summary>
|
||
/// Test: ExecuteCommandAsync() - Basic async insert operation
|
||
/// Verifies: Async insert works, returns affected rows, entity persisted
|
||
/// </summary>
|
||
public static void AsyncInsert_ExecuteCommandAsync()
|
||
{
|
||
Console.WriteLine("Test: ExecuteCommandAsync");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
db.Deleteable<Order>().ExecuteCommand();
|
||
|
||
var order = new Order
|
||
{
|
||
Name = "Async Order Test",
|
||
Price = 99.99m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
// Execute async insert
|
||
var task = db.Insertable(order).ExecuteCommandAsync();
|
||
task.Wait();
|
||
int affectedRows = task.Result;
|
||
|
||
// Verify affected rows
|
||
if (affectedRows != 1)
|
||
throw new Exception($"Expected 1 affected row, got {affectedRows}");
|
||
|
||
// Verify entity exists in database
|
||
var dbOrder = db.Queryable<Order>().Where(o => o.Name == "Async Order Test").First();
|
||
if (dbOrder == null)
|
||
throw new Exception("Entity not found in database");
|
||
|
||
Console.WriteLine("✓ ExecuteCommandAsync works correctly\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: ExecuteReturnIdentityAsync() - Insert and return identity value
|
||
/// Verifies: Identity value returned, correct type (long), entity retrievable by ID
|
||
/// </summary>
|
||
public static void AsyncInsert_ExecuteReturnIdentityAsync()
|
||
{
|
||
Console.WriteLine("Test: ExecuteReturnIdentityAsync");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
db.Deleteable<Order>().ExecuteCommand();
|
||
|
||
var order = new Order
|
||
{
|
||
Name = "Identity Test Order",
|
||
Price = 77.77m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
// Execute and get identity
|
||
var task = db.Insertable(order).ExecuteReturnIdentityAsync();
|
||
task.Wait();
|
||
long identityValue = task.Result;
|
||
|
||
if (identityValue <= 0)
|
||
throw new Exception($"Invalid identity value: {identityValue}");
|
||
|
||
// Verify entity retrievable by identity
|
||
var dbOrder = db.Queryable<Order>().InSingle(identityValue);
|
||
if (dbOrder == null || dbOrder.Name != "Identity Test Order")
|
||
throw new Exception("Entity not found by identity");
|
||
|
||
Console.WriteLine($"✓ Identity returned: {identityValue}\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: ExecuteReturnBigIdentityAsync() - Handle BIGINT identity columns
|
||
/// Verifies: Long type returned, large values supported
|
||
/// </summary>
|
||
public static void AsyncInsert_ExecuteReturnBigIdentityAsync()
|
||
{
|
||
Console.WriteLine("Test: ExecuteReturnBigIdentityAsync");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
|
||
var order = new Order
|
||
{
|
||
Name = "BigInt Identity Test",
|
||
Price = 123.45m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
var task = db.Insertable(order).ExecuteReturnBigIdentityAsync();
|
||
task.Wait();
|
||
long bigIdentity = task.Result;
|
||
|
||
if (bigIdentity <= 0 || bigIdentity.GetType() != typeof(long))
|
||
throw new Exception("Invalid big identity value");
|
||
|
||
Console.WriteLine($"✓ Big identity: {bigIdentity}\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: ExecuteReturnEntityAsync() - Insert and return complete entity
|
||
/// Verifies: All properties populated, identity set, defaults applied
|
||
/// </summary>
|
||
public static void AsyncInsert_ExecuteReturnEntityAsync()
|
||
{
|
||
Console.WriteLine("Test: ExecuteReturnEntityAsync");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
|
||
var order = new Order
|
||
{
|
||
Name = "Return Entity Test",
|
||
Price = 55.55m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
var task = db.Insertable(order).ExecuteReturnEntityAsync();
|
||
task.Wait();
|
||
var returnedEntity = task.Result;
|
||
|
||
if (returnedEntity == null)
|
||
throw new Exception("Returned entity is null");
|
||
if (returnedEntity.Id <= 0)
|
||
throw new Exception("Identity not populated");
|
||
if (returnedEntity.Name != "Return Entity Test")
|
||
throw new Exception("Properties not preserved");
|
||
|
||
Console.WriteLine($"✓ Entity returned with ID: {returnedEntity.Id}\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: ExecuteReturnPkListAsync() - Bulk insert with primary key list
|
||
/// Verifies: Multiple entities inserted, all PKs returned, count matches
|
||
/// </summary>
|
||
public static void AsyncInsert_ExecuteReturnPkListAsync()
|
||
{
|
||
Console.WriteLine("Test: ExecuteReturnPkListAsync");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
|
||
var orders = new List<Order>();
|
||
for (int i = 1; i <= 10; i++)
|
||
{
|
||
orders.Add(new Order
|
||
{
|
||
Name = $"Bulk Order {i}",
|
||
Price = i * 10.10m,
|
||
CreateTime = DateTime.Now
|
||
});
|
||
}
|
||
|
||
var task = db.Insertable(orders).ExecuteReturnPkListAsync<long>();
|
||
task.Wait();
|
||
var pkList = task.Result;
|
||
|
||
if (pkList.Count != 10)
|
||
throw new Exception($"Expected 10 PKs, got {pkList.Count}");
|
||
|
||
// Verify all entities exist
|
||
foreach (var pk in pkList)
|
||
{
|
||
if (pk <= 0 || db.Queryable<Order>().InSingle(pk) == null)
|
||
throw new Exception($"Invalid PK or entity not found: {pk}");
|
||
}
|
||
|
||
Console.WriteLine($"✓ {pkList.Count} PKs returned\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Bulk async insert - Insert multiple entities efficiently
|
||
/// Verifies: All entities inserted, performance acceptable
|
||
/// </summary>
|
||
public static void AsyncInsert_MultipleEntitiesAsync()
|
||
{
|
||
Console.WriteLine("Test: MultipleEntitiesAsync");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
db.Deleteable<Order>().ExecuteCommand();
|
||
|
||
var orders = new List<Order>();
|
||
for (int i = 1; i <= 100; i++)
|
||
{
|
||
orders.Add(new Order
|
||
{
|
||
Name = $"Bulk Async Order {i}",
|
||
Price = i * 1.11m,
|
||
CreateTime = DateTime.Now
|
||
});
|
||
}
|
||
|
||
var startTime = DateTime.Now;
|
||
var task = db.Insertable(orders).ExecuteCommandAsync();
|
||
task.Wait();
|
||
var duration = DateTime.Now - startTime;
|
||
|
||
if (task.Result != 100)
|
||
throw new Exception($"Expected 100 rows, got {task.Result}");
|
||
|
||
Console.WriteLine($"✓ 100 entities inserted in {duration.TotalMilliseconds}ms\n");
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region B. Snowflake ID Tests
|
||
|
||
/// <summary>
|
||
/// Test: ExecuteReturnSnowflakeIdAsync() - Generate distributed unique IDs
|
||
/// Verifies: Snowflake ID generated, long type, unique, entity retrievable
|
||
/// </summary>
|
||
public static void AsyncInsert_SnowflakeIdBasic()
|
||
{
|
||
Console.WriteLine("Test: SnowflakeIdBasic");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<OrderSnowflake>();
|
||
db.Deleteable<OrderSnowflake>().ExecuteCommand();
|
||
|
||
var order = new OrderSnowflake
|
||
{
|
||
Name = "Snowflake Order Test",
|
||
Price = 99.99m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
var task = db.Insertable(order).ExecuteReturnSnowflakeIdAsync();
|
||
task.Wait();
|
||
long snowflakeId = task.Result;
|
||
|
||
if (snowflakeId <= 0)
|
||
throw new Exception($"Invalid Snowflake ID: {snowflakeId}");
|
||
|
||
var dbOrder = db.Queryable<OrderSnowflake>().InSingle(snowflakeId);
|
||
if (dbOrder == null)
|
||
throw new Exception("Entity not found by Snowflake ID");
|
||
|
||
Console.WriteLine($"✓ Snowflake ID: {snowflakeId}\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: ExecuteReturnSnowflakeIdListAsync() - Bulk Snowflake ID generation
|
||
/// Verifies: Multiple unique IDs generated, all entities inserted, IDs sequential
|
||
/// </summary>
|
||
public static void AsyncInsert_SnowflakeIdList()
|
||
{
|
||
Console.WriteLine("Test: SnowflakeIdList");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<OrderSnowflake>();
|
||
db.Deleteable<OrderSnowflake>().ExecuteCommand();
|
||
|
||
var orders = new List<OrderSnowflake>();
|
||
for (int i = 1; i <= 50; i++)
|
||
{
|
||
orders.Add(new OrderSnowflake
|
||
{
|
||
Name = $"Snowflake Bulk {i}",
|
||
Price = i * 2.22m,
|
||
CreateTime = DateTime.Now
|
||
});
|
||
}
|
||
|
||
var task = db.Insertable(orders).ExecuteReturnSnowflakeIdListAsync();
|
||
task.Wait();
|
||
var snowflakeIds = task.Result;
|
||
|
||
if (snowflakeIds.Count != 50)
|
||
throw new Exception($"Expected 50 IDs, got {snowflakeIds.Count}");
|
||
|
||
var uniqueCount = snowflakeIds.Distinct().Count();
|
||
if (uniqueCount != 50)
|
||
throw new Exception($"Expected 50 unique IDs, got {uniqueCount}");
|
||
|
||
Console.WriteLine($"✓ {snowflakeIds.Count} unique Snowflake IDs generated\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Snowflake ID uniqueness - Verify no collisions in large batch
|
||
/// Verifies: 1000 IDs all unique, time-sortable
|
||
/// </summary>
|
||
public static void AsyncInsert_SnowflakeIdUniqueness()
|
||
{
|
||
Console.WriteLine("Test: SnowflakeIdUniqueness");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<OrderSnowflake>();
|
||
db.Deleteable<OrderSnowflake>().ExecuteCommand();
|
||
|
||
var orders = new List<OrderSnowflake>();
|
||
for (int i = 1; i <= 1000; i++)
|
||
{
|
||
orders.Add(new OrderSnowflake
|
||
{
|
||
Name = $"Uniqueness Test {i}",
|
||
Price = i * 0.99m,
|
||
CreateTime = DateTime.Now
|
||
});
|
||
}
|
||
|
||
var task = db.Insertable(orders).ExecuteReturnSnowflakeIdListAsync();
|
||
task.Wait();
|
||
var snowflakeIds = task.Result;
|
||
|
||
var uniqueCount = snowflakeIds.Distinct().Count();
|
||
if (uniqueCount != 1000)
|
||
throw new Exception($"Collision detected! Expected 1000 unique, got {uniqueCount}");
|
||
|
||
Console.WriteLine("✓ 1000 unique IDs, no collisions\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Concurrent Snowflake ID generation - Thread safety verification
|
||
/// Verifies: 10 threads × 10 IDs = 100 unique IDs, no race conditions
|
||
/// </summary>
|
||
public static void AsyncInsert_SnowflakeId_Concurrent()
|
||
{
|
||
Console.WriteLine("Test: SnowflakeId_Concurrent");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<OrderSnowflake>();
|
||
db.Deleteable<OrderSnowflake>().ExecuteCommand();
|
||
|
||
var allIds = new System.Collections.Concurrent.ConcurrentBag<long>();
|
||
var tasks = new List<Task>();
|
||
|
||
for (int threadNum = 0; threadNum < 10; threadNum++)
|
||
{
|
||
int threadId = threadNum;
|
||
var task = Task.Run(async () =>
|
||
{
|
||
var threadDb = Db;
|
||
for (int i = 0; i < 10; i++)
|
||
{
|
||
var order = new OrderSnowflake
|
||
{
|
||
Name = $"Concurrent T{threadId} O{i}",
|
||
Price = (threadId * 10 + i) * 1.11m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
long id = await threadDb.Insertable(order).ExecuteReturnSnowflakeIdAsync();
|
||
allIds.Add(id);
|
||
}
|
||
});
|
||
tasks.Add(task);
|
||
}
|
||
|
||
Task.WaitAll(tasks.ToArray());
|
||
|
||
var uniqueIds = allIds.Distinct().Count();
|
||
if (uniqueIds != 100)
|
||
throw new Exception($"Race condition! Expected 100 unique, got {uniqueIds}");
|
||
|
||
Console.WriteLine("✓ Thread-safe: 100 unique IDs from 10 concurrent threads\n");
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region C. CancellationToken Tests (CRITICAL)
|
||
|
||
/// <summary>
|
||
/// Test: CancellationToken basic support
|
||
/// Verifies: Token accepted, cancellation respected or operation completes fast
|
||
/// </summary>
|
||
public static void AsyncInsert_CancellationToken_Basic()
|
||
{
|
||
Console.WriteLine("Test: CancellationToken_Basic");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
|
||
var cts = new CancellationTokenSource(100);
|
||
var order = new Order
|
||
{
|
||
Name = "Cancellable Order",
|
||
Price = 99.99m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
try
|
||
{
|
||
var task = db.Insertable(order).ExecuteCommandAsync(cts.Token);
|
||
task.Wait();
|
||
Console.WriteLine("✓ Operation completed before timeout\n");
|
||
}
|
||
catch (AggregateException ae) when (ae.InnerException is OperationCanceledException)
|
||
{
|
||
Console.WriteLine("✓ Operation cancelled as expected\n");
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
Console.WriteLine("✓ Operation cancelled as expected\n");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Pre-cancelled token - Immediate cancellation
|
||
/// Verifies: Pre-cancelled token handled correctly
|
||
/// </summary>
|
||
public static void AsyncInsert_CancellationToken_Immediate()
|
||
{
|
||
Console.WriteLine("Test: CancellationToken_Immediate");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
|
||
var cts = new CancellationTokenSource();
|
||
cts.Cancel(); // Pre-cancel
|
||
|
||
var order = new Order
|
||
{
|
||
Name = "Pre-Cancelled Order",
|
||
Price = 88.88m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
try
|
||
{
|
||
var task = db.Insertable(order).ExecuteCommandAsync(cts.Token);
|
||
task.Wait();
|
||
Console.WriteLine("✓ Fast operation completed despite pre-cancelled token\n");
|
||
}
|
||
catch (Exception)
|
||
{
|
||
Console.WriteLine("✓ Immediate cancellation detected\n");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Snowflake ID with CancellationToken
|
||
/// Verifies: Snowflake methods support cancellation
|
||
/// </summary>
|
||
public static void AsyncInsert_CancellationToken_SnowflakeId()
|
||
{
|
||
Console.WriteLine("Test: CancellationToken_SnowflakeId");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<OrderSnowflake>();
|
||
|
||
var cts = new CancellationTokenSource(50);
|
||
var order = new OrderSnowflake
|
||
{
|
||
Name = "Cancellable Snowflake",
|
||
Price = 66.66m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
try
|
||
{
|
||
var task = db.Insertable(order).ExecuteReturnSnowflakeIdAsync(cts.Token);
|
||
task.Wait();
|
||
Console.WriteLine($"✓ Snowflake insert completed, ID: {task.Result}\n");
|
||
}
|
||
catch (Exception)
|
||
{
|
||
Console.WriteLine("✓ Snowflake insert cancellable\n");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: CancellationToken in transaction
|
||
/// Verifies: Transaction rollback on cancellation
|
||
/// </summary>
|
||
public static void AsyncInsert_CancellationToken_Transaction()
|
||
{
|
||
Console.WriteLine("Test: CancellationToken_Transaction");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
db.BeginTran();
|
||
|
||
try
|
||
{
|
||
var cts = new CancellationTokenSource(50);
|
||
var order = new Order
|
||
{
|
||
Name = "Transaction Cancellable",
|
||
Price = 55.55m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
try
|
||
{
|
||
var task = db.Insertable(order).ExecuteCommandAsync(cts.Token);
|
||
task.Wait();
|
||
db.CommitTran();
|
||
Console.WriteLine("✓ Transaction committed\n");
|
||
}
|
||
catch (Exception)
|
||
{
|
||
db.RollbackTran();
|
||
Console.WriteLine("✓ Transaction rolled back on cancellation\n");
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
db.RollbackTran();
|
||
Console.WriteLine("✓ Transaction cleanup on error\n");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region D. Error Handling Tests
|
||
|
||
/// <summary>
|
||
/// Test: Null entity validation
|
||
/// Verifies: Appropriate exception thrown for null entity
|
||
/// </summary>
|
||
public static void AsyncInsert_NullEntity()
|
||
{
|
||
Console.WriteLine("Test: NullEntity");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
|
||
Order nullOrder = null;
|
||
|
||
try
|
||
{
|
||
var task = db.Insertable(nullOrder).ExecuteCommandAsync();
|
||
task.Wait();
|
||
throw new Exception("Expected exception for null entity");
|
||
}
|
||
catch (AggregateException ae) when (ae.InnerException is ArgumentNullException ||
|
||
ae.InnerException is NullReferenceException)
|
||
{
|
||
Console.WriteLine($"✓ Null entity rejected: {ae.InnerException.GetType().Name}\n");
|
||
}
|
||
catch (ArgumentNullException)
|
||
{
|
||
Console.WriteLine("✓ Null entity rejected\n");
|
||
}
|
||
catch (NullReferenceException)
|
||
{
|
||
Console.WriteLine("✓ Null entity rejected\n");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Duplicate key handling
|
||
/// Verifies: Duplicate primary key error detected
|
||
/// </summary>
|
||
public static void AsyncInsert_DuplicateKey()
|
||
{
|
||
Console.WriteLine("Test: DuplicateKey");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
|
||
var order1 = new Order
|
||
{
|
||
Name = "Duplicate Key Test",
|
||
Price = 88.88m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
var task1 = db.Insertable(order1).ExecuteReturnIdentityAsync();
|
||
task1.Wait();
|
||
long id = task1.Result;
|
||
|
||
var order2 = new Order
|
||
{
|
||
Id = (int)id,
|
||
Name = "Duplicate Key Test 2",
|
||
Price = 77.77m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
try
|
||
{
|
||
var task2 = db.Insertable(order2).ExecuteCommandAsync();
|
||
task2.Wait();
|
||
Console.WriteLine("✓ Duplicate key allowed (identity auto-generated)\n");
|
||
}
|
||
catch (Exception)
|
||
{
|
||
Console.WriteLine("✓ Duplicate key error detected\n");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Concurrent async inserts - Thread safety
|
||
/// Verifies: 10 concurrent threads, no deadlocks, all succeed
|
||
/// </summary>
|
||
public static void AsyncInsert_ConcurrentInserts()
|
||
{
|
||
Console.WriteLine("Test: ConcurrentInserts");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
db.Deleteable<Order>().ExecuteCommand();
|
||
|
||
var tasks = new List<Task<int>>();
|
||
|
||
for (int i = 0; i < 10; i++)
|
||
{
|
||
int threadId = i;
|
||
var task = Task.Run(async () =>
|
||
{
|
||
var threadDb = Db;
|
||
var order = new Order
|
||
{
|
||
Name = $"Concurrent Thread {threadId}",
|
||
Price = threadId * 10.10m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
return await threadDb.Insertable(order).ExecuteCommandAsync();
|
||
});
|
||
tasks.Add(task);
|
||
}
|
||
|
||
Task.WaitAll(tasks.ToArray());
|
||
|
||
if (tasks.Sum(t => t.Result) != 10)
|
||
throw new Exception("Not all concurrent inserts succeeded");
|
||
|
||
Console.WriteLine("✓ 10 concurrent inserts, no deadlocks\n");
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Transaction rollback with async insert
|
||
/// Verifies: Rollback works, entity not persisted
|
||
/// </summary>
|
||
public static void AsyncInsert_TransactionRollback()
|
||
{
|
||
Console.WriteLine("Test: TransactionRollback");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
db.Deleteable<Order>().ExecuteCommand();
|
||
|
||
db.BeginTran();
|
||
|
||
try
|
||
{
|
||
var order = new Order
|
||
{
|
||
Name = "Rollback Test Order",
|
||
Price = 55.55m,
|
||
CreateTime = DateTime.Now
|
||
};
|
||
|
||
var task = db.Insertable(order).ExecuteReturnIdentityAsync();
|
||
task.Wait();
|
||
long id = task.Result;
|
||
|
||
db.RollbackTran();
|
||
|
||
var orderAfterRollback = db.Queryable<Order>().InSingle(id);
|
||
if (orderAfterRollback != null)
|
||
throw new Exception("Entity exists after rollback!");
|
||
|
||
Console.WriteLine("✓ Transaction rollback works\n");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
db.RollbackTran();
|
||
throw new Exception($"Transaction test failed: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Test: Async vs Sync performance comparison
|
||
/// Verifies: Async performance is acceptable (1000 entities)
|
||
/// </summary>
|
||
public static void AsyncInsert_Performance()
|
||
{
|
||
Console.WriteLine("Test: Performance");
|
||
|
||
var db = Db;
|
||
db.CodeFirst.InitTables<Order>();
|
||
db.Deleteable<Order>().ExecuteCommand();
|
||
|
||
// Async test
|
||
var asyncOrders = new List<Order>();
|
||
for (int i = 1; i <= 1000; i++)
|
||
{
|
||
asyncOrders.Add(new Order
|
||
{
|
||
Name = $"Perf Async {i}",
|
||
Price = i * 1.23m,
|
||
CreateTime = DateTime.Now
|
||
});
|
||
}
|
||
|
||
var asyncStart = DateTime.Now;
|
||
var asyncTask = db.Insertable(asyncOrders).ExecuteCommandAsync();
|
||
asyncTask.Wait();
|
||
var asyncDuration = DateTime.Now - asyncStart;
|
||
|
||
Console.WriteLine($" Async: {asyncTask.Result} rows in {asyncDuration.TotalMilliseconds}ms");
|
||
|
||
// Sync test
|
||
db.Deleteable<Order>().ExecuteCommand();
|
||
var syncOrders = new List<Order>();
|
||
for (int i = 1; i <= 1000; i++)
|
||
{
|
||
syncOrders.Add(new Order
|
||
{
|
||
Name = $"Perf Sync {i}",
|
||
Price = i * 1.23m,
|
||
CreateTime = DateTime.Now
|
||
});
|
||
}
|
||
|
||
var syncStart = DateTime.Now;
|
||
int syncResult = db.Insertable(syncOrders).ExecuteCommand();
|
||
var syncDuration = DateTime.Now - syncStart;
|
||
|
||
Console.WriteLine($" Sync: {syncResult} rows in {syncDuration.TotalMilliseconds}ms");
|
||
Console.WriteLine($" Ratio: {(asyncDuration.TotalMilliseconds / syncDuration.TotalMilliseconds):F2}x\n");
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Helper Classes
|
||
|
||
/// <summary>
|
||
/// Helper entity for Snowflake ID tests
|
||
/// Uses long PK without identity for distributed ID generation
|
||
/// </summary>
|
||
[SugarTable("OrderSnowflake")]
|
||
public class OrderSnowflake
|
||
{
|
||
[SugarColumn(IsPrimaryKey = true)]
|
||
public long Id { get; set; }
|
||
|
||
public string Name { get; set; }
|
||
public decimal Price { get; set; }
|
||
public DateTime CreateTime { get; set; }
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|