174 lines
5.6 KiB
C#
174 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace HanvonF710XAttendanceService
|
|
{
|
|
internal static class MachineUserDeleteSyncTests
|
|
{
|
|
public static int RunAll()
|
|
{
|
|
int failed = 0;
|
|
failed += Run("651 IDs split into 33 batches of 20", Test651IdsSplitInto33Batches);
|
|
failed += Run("exactly 20 IDs is one batch", TestExactly20IdsOneBatch);
|
|
failed += Run("fewer than 20 IDs is one batch", TestFewerThan20IdsOneBatch);
|
|
failed += Run("empty deletion list", TestEmptyDeletionList);
|
|
failed += Run("successful batch marks DB deleted", TestSuccessfulBatchMarksDeleted);
|
|
failed += Run("failed batch remains pending", TestFailedBatchRemainsPending);
|
|
return failed;
|
|
}
|
|
|
|
private static int Run(string name, Func<bool> test)
|
|
{
|
|
try
|
|
{
|
|
if (!test())
|
|
{
|
|
Console.WriteLine("FAIL: " + name);
|
|
return 1;
|
|
}
|
|
|
|
Console.WriteLine("PASS: " + name);
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("FAIL: " + name + " err=" + ex.Message);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
private static bool Test651IdsSplitInto33Batches()
|
|
{
|
|
var ids = BuildIds(651);
|
|
var batches = MachineUserDeleteBatcher.SplitIntoBatches(ids, 20);
|
|
return batches.Count == 33
|
|
&& batches.Take(32).All(b => b.Count == 20)
|
|
&& batches[32].Count == 11
|
|
&& MachineUserDeleteBatcher.CountBatches(651, 20) == 33;
|
|
}
|
|
|
|
private static bool TestExactly20IdsOneBatch()
|
|
{
|
|
var ids = BuildIds(20);
|
|
var batches = MachineUserDeleteBatcher.SplitIntoBatches(ids, 20);
|
|
return batches.Count == 1 && batches[0].Count == 20;
|
|
}
|
|
|
|
private static bool TestFewerThan20IdsOneBatch()
|
|
{
|
|
var ids = BuildIds(7);
|
|
var batches = MachineUserDeleteBatcher.SplitIntoBatches(ids, 20);
|
|
return batches.Count == 1 && batches[0].Count == 7;
|
|
}
|
|
|
|
private static bool TestEmptyDeletionList()
|
|
{
|
|
var batches = MachineUserDeleteBatcher.SplitIntoBatches(new List<string>(), 20);
|
|
var result = MachineUserDeleteSync.Execute(
|
|
"105",
|
|
"192.168.90.223",
|
|
new List<MachineUserDeleteItem>(),
|
|
20,
|
|
null,
|
|
null,
|
|
null);
|
|
return batches.Count == 0
|
|
&& result.TotalPending == 0
|
|
&& result.SuccessCount == 0
|
|
&& result.FailedCount == 0;
|
|
}
|
|
|
|
private static bool TestSuccessfulBatchMarksDeleted()
|
|
{
|
|
var pending = new List<MachineUserDeleteItem>
|
|
{
|
|
new MachineUserDeleteItem("1001", "1001"),
|
|
new MachineUserDeleteItem("1002", "1002"),
|
|
new MachineUserDeleteItem("1003", "1003")
|
|
};
|
|
var marked = new List<string>();
|
|
var logs = new List<string>();
|
|
|
|
var result = MachineUserDeleteSync.Execute(
|
|
"105",
|
|
"192.168.90.223",
|
|
pending,
|
|
20,
|
|
(IReadOnlyList<string> ids, out string error) =>
|
|
{
|
|
error = null;
|
|
return ids.Count == 3;
|
|
},
|
|
serial => marked.Add(serial),
|
|
logs.Add);
|
|
|
|
return result.TotalPending == 3
|
|
&& result.SuccessCount == 3
|
|
&& result.FailedCount == 0
|
|
&& marked.Count == 3
|
|
&& marked.Contains("1001")
|
|
&& marked.Contains("1002")
|
|
&& marked.Contains("1003")
|
|
&& logs.Any(l => l.Contains("result=SUCCESS"));
|
|
}
|
|
|
|
private static bool TestFailedBatchRemainsPending()
|
|
{
|
|
var pending = BuildDeleteItems(40);
|
|
var marked = new List<string>();
|
|
int callCount = 0;
|
|
|
|
var result = MachineUserDeleteSync.Execute(
|
|
"105",
|
|
"192.168.90.223",
|
|
pending,
|
|
20,
|
|
(IReadOnlyList<string> ids, out string error) =>
|
|
{
|
|
callCount++;
|
|
if (callCount == 2)
|
|
{
|
|
error = "The operation has timed out";
|
|
return false;
|
|
}
|
|
|
|
error = null;
|
|
return true;
|
|
},
|
|
serial => marked.Add(serial),
|
|
_ => { });
|
|
|
|
return result.TotalPending == 40
|
|
&& result.SuccessCount == 20
|
|
&& result.FailedCount == 20
|
|
&& marked.Count == 20
|
|
&& !marked.Contains("21")
|
|
&& marked.Contains("1")
|
|
&& marked.Contains("20");
|
|
}
|
|
|
|
private static List<string> BuildIds(int count)
|
|
{
|
|
var ids = new List<string>(count);
|
|
for (int i = 1; i <= count; i++)
|
|
{
|
|
ids.Add(i.ToString());
|
|
}
|
|
|
|
return ids;
|
|
}
|
|
|
|
private static List<MachineUserDeleteItem> BuildDeleteItems(int count)
|
|
{
|
|
var items = new List<MachineUserDeleteItem>(count);
|
|
for (int i = 1; i <= count; i++)
|
|
{
|
|
items.Add(new MachineUserDeleteItem(i.ToString(), i.ToString()));
|
|
}
|
|
|
|
return items;
|
|
}
|
|
}
|
|
}
|