Repository navigation
Expand file tree
/
Copy pathMachineToolProductionFactoryTests.cs
More file actions
328 lines (321 loc) · 16.6 KB
/
Copy pathMachineToolProductionFactoryTests.cs
File metadata and controls
328 lines (321 loc) · 16.6 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// SPDX-FileCopyrightText: Copyright (c) 2005-2026, OPC Federation AISBL
// SPDX-License-Identifier: MIT
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Client;
using Opc.Ua.Client.TestFramework;
using Opc.Ua.GMS;
using Opc.Ua.MachineTool.Server;
using Opc.Ua.Tests;
namespace Opc.Ua.MachineTool.Tests
{
/// <summary>
/// Verifies production composition on actual generated GMS types and rejects invalid ownership transfers.
/// </summary>
[TestFixture]
[Category("MachineTool")]
[NonParallelizable]
public sealed class MachineToolProductionFactoryTests
{
/// <summary>
/// GMSJob and GMSPart retain their types, added members and resource bindings across static job replacement.
/// </summary>
[Test]
public async Task ActualGmsFactoriesPreserveDerivedNodesAcrossStaticReplacementAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
await LoadGmsAsync(fixture).ConfigureAwait(false);
GMSState machine = fixture.Context.Context.CreateInstanceOfGMSType(
fixture.Context.MachinesFolder, new QualifiedName("Gms", fixture.Context.InstanceNamespaceIndex));
GMSJobState? jobNode = null;
GMSPartState? partNode = null;
var jobResource = new Resource();
var partResource = new Resource();
var controller = new MachineToolProductionController(fixture.Context, machine,
new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.ProductionParts,
CreateJobAsync = (context, parent, name, _) =>
{
jobNode = context.CreateInstanceOfGMSJobType(parent, name);
jobNode.AddBatchIdentifier(context).AddMeasurementReason(context);
jobNode.BatchIdentifier!.Value = "Batch-preserved";
jobNode.MeasurementReason!.Value = MeasurementReasonEnum.AuditMeasurement;
return new ValueTask<MachineToolProductionNode<ProductionJobState>>(
new MachineToolProductionNode<ProductionJobState>(jobNode, jobResource));
},
CreatePartAsync = (context, parent, name, _) =>
{
partNode = context.CreateInstanceOfGMSPartType(parent, name);
partNode.AddPartDescription(context);
partNode.PartDescription!.Value = "Description-preserved";
return new ValueTask<MachineToolProductionNode<ProductionPartState>>(
new MachineToolProductionNode<ProductionPartState>(partNode, partResource));
}
}, TimeProvider.System);
await using (controller.ConfigureAwait(false))
{
MachineToolSnapshot example = MachineToolProductionTests.Example();
MachineToolProductionJob job = example.Production.Jobs[0];
MachineToolProgram program = job.Programs[0] with
{
Key = "Program",
Phase = MachineToolProductionPhase.Ended
};
MachineToolProductionPartSet set = job.PartSets[0] with
{
PartsCompletedPerRun = 1,
Parts =
[
job.PartSets[0].Parts[0] with
{
Phase = MachineToolProductionPhase.Ended, Quality = PartQuality.Good
}
]
};
job = job with
{
Key = "Slot",
Phase = MachineToolProductionPhase.Ended,
RunsCompleted = 1,
RunsPlanned = 1,
PartsCompleted = 1,
PartsGood = 1,
Programs = [program],
PartSets = [set]
};
var initial = example with
{
ActiveProgram = program,
Production = example.Production with { Jobs = [job], Statistics = null }
};
await controller.InitializeAsync(initial).ConfigureAwait(false);
Assert.That(controller.State, Is.SameAs(machine.Production));
Assert.That(jobNode, Is.TypeOf<GMSJobState>());
Assert.That(partNode, Is.TypeOf<GMSPartState>());
NodeId jobId = jobNode!.NodeId;
NodeId partId = partNode!.NodeId;
Assert.That(jobNode.TypeDefinitionId,
Is.EqualTo(NodeId.Create(1016u, GmsModel.NamespaceUri, fixture.Context.Context.NamespaceUris)));
Assert.That(partNode.TypeDefinitionId,
Is.EqualTo(NodeId.Create(1017u, GmsModel.NamespaceUri, fixture.Context.Context.NamespaceUris)));
program = program with
{
Name = "NEXT.NC",
JobIdentifier = "NextJob",
Phase = MachineToolProductionPhase.Initializing,
TransitionSequence = 1
};
set = set with
{
PartsCompletedPerRun = 0,
Parts =
[
set.Parts[0] with
{
Identifier = "NextPart", Phase = MachineToolProductionPhase.Initializing,
TransitionSequence = 1, Quality = PartQuality.NotYetMeasured
}
]
};
job = job with
{
Identifier = "NextJob",
Phase = MachineToolProductionPhase.Initializing,
TransitionSequence = 1,
RunsCompleted = 0,
RunsPlanned = 2,
PartsCompleted = 0,
PartsGood = 0,
Programs = [program],
PartSets = [set]
};
await controller.ApplyAsync(initial with
{
Revision = 1,
ActiveProgram = program,
Production = initial.Production with { Jobs = [job] }
}).ConfigureAwait(false);
Assert.That(jobNode.NodeId, Is.EqualTo(jobId));
Assert.That(partNode.NodeId, Is.EqualTo(partId));
Assert.That(jobNode.Identifier!.Value, Is.EqualTo("NextJob"));
Assert.That(partNode.Identifier!.Value, Is.EqualTo("NextPart"));
Assert.That(jobNode.BatchIdentifier!.Value, Is.EqualTo("Batch-preserved"));
Assert.That(jobNode.MeasurementReason!.Value, Is.EqualTo(MeasurementReasonEnum.AuditMeasurement));
Assert.That(partNode.PartDescription!.Value, Is.EqualTo("Description-preserved"));
Assert.That(jobNode.State!.LastTransition!.Number!.Value, Is.EqualTo(2));
Assert.That(partNode.State!.LastTransition!.Number!.Value, Is.EqualTo(2));
Assert.That(controller.State.ActiveProgram!.JobNodeId!.Value, Is.EqualTo(jobId));
Assert.That(jobResource.Disposals, Is.Zero);
Assert.That(partResource.Disposals, Is.Zero);
}
Assert.That(jobResource.Disposals, Is.EqualTo(1));
Assert.That(partResource.Disposals, Is.EqualTo(1));
}
/// <summary>
/// Returning an already owned generated node must not dispose or delete the existing job on rollback.
/// </summary>
[Test]
public async Task FactoryCannotStealAnExistingJobOrItsResourceAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolState machine = fixture.Context.Context.CreateInstanceOfMachineToolType(
fixture.Context.MachinesFolder, new QualifiedName("Ownership", fixture.Context.InstanceNamespaceIndex));
ProductionJobState? owned = null;
var resource = new Resource();
await using var controller = new MachineToolProductionController(fixture.Context, machine,
new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.DynamicProductionPlan,
PublishFinalObservationsAsync =
(_, _) => throw new InvalidOperationException("No removal is requested."),
CreateJobAsync = (context, parent, name, _) =>
{
owned ??= context.CreateInstanceOfProductionJobType(parent, name);
return new ValueTask<MachineToolProductionNode<ProductionJobState>>(
new MachineToolProductionNode<ProductionJobState>(owned, resource));
}
}, TimeProvider.System);
var job = new MachineToolProductionJob
{
Identifier = "A",
Programs = [new MachineToolProgram { Name = "Main" }]
};
var initial = new MachineToolSnapshot
{
Production = new MachineToolProductionObservation { Jobs = [job] }
};
await controller.InitializeAsync(initial).ConfigureAwait(false);
await Assert.ThrowsAsync<ServiceResultException>(async () => await controller.ApplyAsync(initial with
{
Revision = 1,
Production = initial.Production with { Jobs = [job, job with { Identifier = "B" }] }
}).ConfigureAwait(false));
Assert.That(controller.Observation, Is.SameAs(initial));
Assert.That(controller.State.ProductionPlan!.FindChild(fixture.Context.Context,
new QualifiedName("A", fixture.Context.InstanceNamespaceIndex)), Is.SameAs(owned));
Assert.That(owned!.Identifier!.Value, Is.EqualTo("A"));
Assert.That(resource.Disposals, Is.Zero);
}
/// <summary>
/// The public builder passes real GMS factories into the composed runtime and its live address space.
/// </summary>
[Test]
public async Task PublicBuilderPublishesActualGmsJobAndPartNodesOverTcpAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
await LoadGmsAsync(fixture).ConfigureAwait(false);
var provider = new InMemoryMachineToolProvider(MachineToolProductionTests.Example());
GMSJobState? job = null;
GMSPartState? part = null;
await using MachineToolRuntime<GMSState> runtime = await fixture.Context
.AddMachineTool("GmsBuilder",
static (context, parent, name) => context.CreateInstanceOfGMSType(parent, name))
.WithProvider(provider)
.WithProduction(new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.ProductionParts |
MachineToolFeatures.ProductionStatistics,
CreateJobAsync = (context, parent, name, _) =>
{
job = context.CreateInstanceOfGMSJobType(parent, name);
job.AddBatchIdentifier(context);
job.BatchIdentifier!.Value = "GMS batch";
return new ValueTask<MachineToolProductionNode<ProductionJobState>>(
new MachineToolProductionNode<ProductionJobState>(job));
},
CreatePartAsync = (context, parent, name, _) =>
{
part = context.CreateInstanceOfGMSPartType(parent, name);
part.AddPartDescription(context);
part.PartDescription!.Value = "GMS part";
return new ValueTask<MachineToolProductionNode<ProductionPartState>>(
new MachineToolProductionNode<ProductionPartState>(part));
}
})
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("GMS production test");
identity.SerialNumber = "GMS-Production";
identity.ProductInstanceUri = "urn:production-tests:gms";
}).BuildAsync().ConfigureAwait(false);
Assert.That(runtime.State, Is.TypeOf<GMSState>());
Assert.That(fixture.Context.Manager.FindPredefinedNode(job!.NodeId), Is.SameAs(job));
Assert.That(fixture.Context.Manager.FindPredefinedNode(part!.NodeId), Is.SameAs(part));
await using var client = new ClientFixture(NUnitTelemetryContext.Create());
await client.LoadClientConfigurationAsync(fixture.Root, "GmsProductionClient").ConfigureAwait(false);
using ISession session = await client.ConnectAsync(fixture.Endpoint, SecurityPolicies.None)
.ConfigureAwait(false);
DataValue batch = await session.ReadValueAsync(job.BatchIdentifier!.NodeId).ConfigureAwait(false);
DataValue description = await session.ReadValueAsync(part.PartDescription!.NodeId).ConfigureAwait(false);
Assert.That(batch.WrappedValue.TryGetValue(out string? batchName), Is.True);
Assert.That(batchName, Is.EqualTo("GMS batch"));
Assert.That(description.WrappedValue.TryGetValue(out string? partName), Is.True);
Assert.That(partName, Is.EqualTo("GMS part"));
MachineToolSnapshot next = await provider.ReadAsync().ConfigureAwait(false);
MachineToolProgram running = next.ActiveProgram with
{
Phase = MachineToolProductionPhase.Running,
TransitionSequence = 1
};
await provider.SetSnapshotAsync(next with
{
ActiveProgram = running,
Production = next.Production with
{
Jobs =
[
next.Production.Jobs[0] with
{
Phase = MachineToolProductionPhase.Running, TransitionSequence = 1, Programs = [running]
}
]
}
}).ConfigureAwait(false);
await runtime.RefreshAsync().ConfigureAwait(false);
DataValue current = await session.ReadValueAsync(job.State!.CurrentState!.Id!.NodeId).ConfigureAwait(false);
Assert.That(current.WrappedValue.TryGetValue(out NodeId state), Is.True);
Assert.That(state, Is.EqualTo(NodeId.Create(5017u, Namespaces.MachineTool, session.NamespaceUris)));
Assert.That(runtime.State.Production!.ActiveProgram!.JobNodeId!.Value, Is.EqualTo(job.NodeId));
await session.CloseAsync(CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Loads released GMS declarations into the existing namespace owner without creating another runtime.
/// </summary>
private static async Task LoadGmsAsync(MachineToolRuntimeFixture fixture)
{
fixture.Context.Context.EncodeableFactory.Builder.AddGmsTypeSystem().Commit();
var definitions = new NodeStateCollection();
definitions.AddGmsTypeSystem(fixture.Context.Context);
for (int i = 0; i < definitions.Count; i++)
{
NodeState definition = definitions[i];
if (fixture.Context.Manager.FindPredefinedNode(definition.NodeId) == null)
{
await fixture.Context.Manager.AddPredefinedNodeAsync(definition).ConfigureAwait(false);
}
}
}
/// <summary>
/// Counts transfers of a companion-specific resource without inspecting module implementation fields.
/// </summary>
private sealed class Resource : IAsyncDisposable
{
/// <summary>
/// Gets the number of completed disposals.
/// </summary>
public int Disposals { get; private set; }
/// <inheritdoc/>
public ValueTask DisposeAsync()
{
Disposals++;
return default;
}
}
}
}