Skip to content

Commit

Permalink
Merge pull request #87 from PHOENIXCONTACT/fix/immutable-session
Browse files Browse the repository at this point in the history
Session context struct avoids changes on previos sequence
  • Loading branch information
Toxantron authored Sep 9, 2024
2 parents 53c140a + 79112d7 commit 968f80d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
10 changes: 3 additions & 7 deletions src/Moryx.ControlSystem/Cells/ReadyToWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,16 @@ internal ReadyToWork(Session currentSession)
/// </summary>
/// <param name="activity">The activity.</param>
public ActivityStart StartActivity(IActivity activity)
{
// Update process before next stage
Process = activity.Process;
return new ActivityStart(this, activity);
{
return new ActivityStart(this, activity) { Process = activity.Process };
}

/// <summary>
/// Creates the SequenceCompleted message
/// </summary>
public SequenceCompleted CompleteSequence(IProcess process, bool processActive, params long[] nextCells)
{
// Update process before next stage
Process = process;
return new SequenceCompleted(this, processActive, nextCells);
return new SequenceCompleted(this, processActive, nextCells) { Process = process };
}

/// <summary>
Expand Down
17 changes: 10 additions & 7 deletions src/Moryx.ControlSystem/Cells/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected Session(Session currentSession)
/// <summary>
/// Context class holding all session information
/// </summary>
private readonly SessionContext _context;
private SessionContext _context;

/// <summary>
/// Unique id of the current production transaction
Expand Down Expand Up @@ -134,24 +134,27 @@ private static ReadyToWork CreateSession(ActivityClassification classification,

#endregion

private class SessionContext
private struct SessionContext
{
internal SessionContext(ActivityClassification classification, Guid sessionId, ProcessReference reference)
{
Classification = classification;
SessionId = sessionId;
Reference = reference;

Tag = null;
Process = null;
}

public Guid SessionId { get; }
public Guid SessionId;

public IProcess Process { get; set; }
public IProcess Process;

public ProcessReference Reference { get; set; }
public ProcessReference Reference;

public ActivityClassification Classification { get; }
public ActivityClassification Classification;

public object Tag { get; set; }
public object Tag;
}
}
}
9 changes: 5 additions & 4 deletions src/Tests/Moryx.ControlSystem.Tests/ProductionSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public void TestCreateActivityStart()
{
var readyToWork = Session.StartSession(ActivityClassification.Production, ReadyToWorkType.Pull, 4242);

var activityStart = readyToWork.StartActivity(new DummyActivity { Process = new Process() });
var activityStart = readyToWork.StartActivity(new DummyActivity { Process = new Process { Id = 4242 } });

Assert.AreEqual(readyToWork.Reference, activityStart.Reference);
Assert.AreEqual(readyToWork.Id, activityStart.Id);
Assert.AreNotEqual(readyToWork.Process, activityStart.Process); // Make sure process is not populated back to RTW
}

[Test]
Expand Down Expand Up @@ -82,7 +83,7 @@ public void TestCreateActivityResult()
{
var readyToWork = Session.StartSession(ActivityClassification.Production, ReadyToWorkType.Pull, 4242);

var activityStart = readyToWork.StartActivity(new DummyActivity { Process = new Process() });
var activityStart = readyToWork.StartActivity(new DummyActivity { Process = new Process { Id = 4242 } });
activityStart.Activity.Complete(1);

var activityCompleted = activityStart.CreateResult();
Expand All @@ -96,7 +97,7 @@ public void TestCompleteSequence()
{
var readyToWork = Session.StartSession(ActivityClassification.Production, ReadyToWorkType.Pull, 4242);

var activityStart = readyToWork.StartActivity(new DummyActivity {Process = new Process()});
var activityStart = readyToWork.StartActivity(new DummyActivity { Process = new Process { Id = 4242 } });
activityStart.Activity.Complete(1);

var activityCompleted = activityStart.CreateResult();
Expand All @@ -123,7 +124,7 @@ public void TestContinueSession(ReadyToWorkType readyToWorkType)
{
var readyToWork = Session.StartSession(ActivityClassification.Production, readyToWorkType, 4242);

var activityStart = readyToWork.StartActivity(new DummyActivity { Process = new Process() });
var activityStart = readyToWork.StartActivity(new DummyActivity { Process = new Process { Id = 4242 } });
activityStart.Activity.Complete(1);

var activityCompleted = activityStart.CreateResult();
Expand Down

0 comments on commit 968f80d

Please sign in to comment.