Skip to content

Commit 198a05a

Browse files
committed
Extract Version getter implementation to OrchestrationVersionExtractor
1 parent d7dae33 commit 198a05a

File tree

3 files changed

+125
-7
lines changed

3 files changed

+125
-7
lines changed

src/DurableSDK/OrchestrationContext.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ public string Version
4141
{
4242
get
4343
{
44-
if (History == null)
45-
{
46-
return null;
47-
}
48-
49-
var executionStartedEvent = Array.Find(History, e => e.EventType == HistoryEventType.ExecutionStarted);
50-
return executionStartedEvent?.Version;
44+
return OrchestrationVersionExtractor.GetVersionFromHistory(History);
5145
}
5246
}
5347
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
7+
{
8+
using System;
9+
10+
/// <summary>
11+
/// Helper class to extract version information from orchestration context.
12+
/// </summary>
13+
internal static class OrchestrationVersionExtractor
14+
{
15+
/// <summary>
16+
/// Gets the orchestration version from a collection of history events.
17+
/// </summary>
18+
/// <param name="historyEvents">The history events to search.</param>
19+
/// <returns>The version, or null if not found.</returns>
20+
public static string GetVersionFromHistory(HistoryEvent[] historyEvents)
21+
{
22+
if (historyEvents == null)
23+
{
24+
return null;
25+
}
26+
27+
var executionStartedEvent = Array.Find(historyEvents, e => e.EventType == HistoryEventType.ExecutionStarted);
28+
return executionStartedEvent?.Version;
29+
}
30+
}
31+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.Azure.Functions.PowerShellWorker.Test.Durable
7+
{
8+
using System;
9+
using Microsoft.Azure.Functions.PowerShellWorker.Durable;
10+
using Xunit;
11+
12+
public class OrchestrationVersionExtractorTests
13+
{
14+
[Fact]
15+
public void GetVersionFromHistory_ReturnsNull_WhenHistoryIsNull()
16+
{
17+
// Act
18+
string result = OrchestrationVersionExtractor.GetVersionFromHistory(null);
19+
20+
// Assert
21+
Assert.Null(result);
22+
}
23+
24+
[Fact]
25+
public void GetVersionFromHistory_ReturnsNull_WhenHistoryHasNoExecutionStartedEvent()
26+
{
27+
// Arrange
28+
var historyEvents = new[]
29+
{
30+
new HistoryEvent { EventType = HistoryEventType.TaskScheduled, EventId = 1 },
31+
new HistoryEvent { EventType = HistoryEventType.TaskCompleted, EventId = 2, TaskScheduledId = 1 }
32+
};
33+
34+
// Act
35+
string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);
36+
37+
// Assert
38+
Assert.Null(result);
39+
}
40+
41+
[Fact]
42+
public void GetVersionFromHistory_ReturnsVersion_WhenExecutionStartedEventExists()
43+
{
44+
// Arrange
45+
const string expectedVersion = "1.0.0";
46+
var historyEvents = new[]
47+
{
48+
new HistoryEvent {
49+
EventType = HistoryEventType.ExecutionStarted,
50+
EventId = 1,
51+
Version = expectedVersion
52+
},
53+
new HistoryEvent { EventType = HistoryEventType.TaskScheduled, EventId = 2 }
54+
};
55+
56+
// Act
57+
string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);
58+
59+
// Assert
60+
Assert.Equal(expectedVersion, result);
61+
}
62+
63+
[Fact]
64+
public void GetVersionFromHistory_ReturnsFirstExecutionStartedVersion_WhenMultipleExecutionStartedEventsExist()
65+
{
66+
// Arrange
67+
const string expectedVersion = "1.0.0";
68+
const string secondVersion = "2.0.0";
69+
70+
var historyEvents = new[]
71+
{
72+
new HistoryEvent {
73+
EventType = HistoryEventType.ExecutionStarted,
74+
EventId = 1,
75+
Version = expectedVersion
76+
},
77+
new HistoryEvent { EventType = HistoryEventType.TaskScheduled, EventId = 2 },
78+
new HistoryEvent {
79+
EventType = HistoryEventType.ExecutionStarted,
80+
EventId = 3,
81+
Version = secondVersion
82+
}
83+
};
84+
85+
// Act
86+
string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);
87+
88+
// Assert
89+
Assert.Equal(expectedVersion, result);
90+
Assert.NotEqual(secondVersion, result);
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)