TaskDispatchBackgroundServiceTests.cs
5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Rcs.Domain.Entities;
using Rcs.Infrastructure.Services;
using Xunit;
namespace Rcs.Infrastructure.Tests;
public class TaskDispatchBackgroundServiceTests
{
[Fact]
public async Task MixedLineCheck_ReturnsSoftFailure_WhenActiveExclusiveTypeConflictsWithDispatchType()
{
var exclusiveBeginTypeId = Guid.NewGuid();
var dispatchBeginTypeId = Guid.NewGuid();
var activeTask = CreateTask("ACTIVE", priority: 20, exclusiveBeginTypeId);
var dispatchTask = CreateTask("P1001", priority: 55, dispatchBeginTypeId);
var dispatchBeginType = new StorageLocationType
{
TypeId = dispatchBeginTypeId,
TypeCode = "NORMAL",
TypeName = "Normal",
IsMixedLineShelfTaskExclusive = false
};
var result = await InvokeMixedLineCheckAsync(
dispatchTask,
dispatchBeginType,
new[] { activeTask },
new Dictionary<Guid, bool>
{
[exclusiveBeginTypeId] = true,
[dispatchBeginTypeId] = false
});
Assert.False(GetProperty<bool>(result, "CanAssign"));
Assert.True(GetProperty<bool>(result, "IsMixedLineSoftConstraintFailure"));
Assert.True(GetProperty<bool>(result, "HasActiveExclusiveLock"));
Assert.Equal(exclusiveBeginTypeId, GetProperty<Guid?>(result, "ActiveExclusiveBeginTypeId"));
}
[Fact]
public async Task MixedLineCheck_DoesNotReturnSoftFailure_WhenActiveTaskHasNoBeginLocationType()
{
var dispatchBeginTypeId = Guid.NewGuid();
var activeTask = new RobotTask
{
TaskId = Guid.NewGuid(),
TaskCode = "ACTIVE",
Priority = 20,
BeginLocation = new StorageLocation
{
LocationId = Guid.NewGuid(),
LocationCode = "ACTIVE_BEGIN"
}
};
var dispatchTask = CreateTask("P1001", priority: 55, dispatchBeginTypeId);
var dispatchBeginType = new StorageLocationType
{
TypeId = dispatchBeginTypeId,
TypeCode = "MIXED",
TypeName = "Mixed",
IsMixedLineShelfTaskExclusive = true
};
var result = await InvokeMixedLineCheckAsync(
dispatchTask,
dispatchBeginType,
new[] { activeTask },
new Dictionary<Guid, bool>());
Assert.False(GetProperty<bool>(result, "CanAssign"));
Assert.False(GetProperty<bool>(result, "IsMixedLineSoftConstraintFailure"));
}
[Fact]
public void RoundState_SkipsOnlyTasksBelowTheSoftFailurePriority()
{
var serviceType = typeof(TaskDispatchBackgroundService);
var stateType = serviceType.GetNestedType("DispatchRoundRobotState", BindingFlags.NonPublic)!;
var statesType = typeof(Dictionary<,>).MakeGenericType(typeof(Guid), stateType);
var states = Activator.CreateInstance(statesType)!;
var robotId = Guid.NewGuid();
serviceType.GetMethod("ExcludeRobotForSoftMixedLineFailure", BindingFlags.NonPublic | BindingFlags.Static)!
.Invoke(null, new object[] { robotId, 55, states });
var shouldSkip = serviceType.GetMethod(
"ShouldSkipRobotExcludedByHigherPrioritySoftMixedLineFailure",
BindingFlags.NonPublic | BindingFlags.Static)!;
var lowerPriorityTask = new RobotTask { TaskId = Guid.NewGuid(), TaskCode = "LOW", Priority = 20 };
var samePriorityTask = new RobotTask { TaskId = Guid.NewGuid(), TaskCode = "SAME", Priority = 55 };
Assert.True((bool)shouldSkip.Invoke(null, new object[] { lowerPriorityTask, robotId, states })!);
Assert.False((bool)shouldSkip.Invoke(null, new object[] { samePriorityTask, robotId, states })!);
}
private static async Task<object> InvokeMixedLineCheckAsync(
RobotTask dispatchTask,
StorageLocationType dispatchBeginType,
IReadOnlyCollection<RobotTask> activeTasks,
Dictionary<Guid, bool> mixedLineExclusiveCache)
{
var service = new TaskDispatchBackgroundService(
NullLogger<TaskDispatchBackgroundService>.Instance,
new ServiceCollection().BuildServiceProvider(),
agvPathService: null!);
var method = typeof(TaskDispatchBackgroundService).GetMethod(
"CanAssignMixedLineExclusiveTaskAsync",
BindingFlags.Instance | BindingFlags.NonPublic)!;
var invocation = (Task)method.Invoke(
service,
new object?[]
{
dispatchTask,
dispatchBeginType,
new Robot { RobotId = Guid.NewGuid(), RobotCode = "001" },
activeTasks,
null,
mixedLineExclusiveCache,
CancellationToken.None
})!;
await invocation;
return invocation.GetType().GetProperty("Result")!.GetValue(invocation)!;
}
private static RobotTask CreateTask(string taskCode, int priority, Guid beginTypeId)
{
return new RobotTask
{
TaskId = Guid.NewGuid(),
TaskCode = taskCode,
Priority = priority,
BeginLocation = new StorageLocation
{
LocationId = Guid.NewGuid(),
LocationCode = $"{taskCode}_BEGIN",
MapNode = new MapNode
{
NodeId = Guid.NewGuid(),
NodeCode = $"{taskCode}_NODE",
StorageLocationTypeId = beginTypeId
}
}
};
}
private static T GetProperty<T>(object instance, string propertyName)
{
return (T)instance.GetType().GetProperty(propertyName)!.GetValue(instance)!;
}
}