Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/DurableTask.ServiceBus/Common/ServiceBusUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,29 @@ static async Task<Message> GenerateBrokeredMessageWithBlobKeyPropertyAsync(
return brokeredMessage;
}

/// <summary>
/// Reads the remaining contents of a session state stream into a byte array.
/// </summary>
public static async Task<byte[]> GetSessionStateBytesAsync(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}

// Avoid an intermediate copy when the stream is already a MemoryStream positioned at the start.
if (stream is MemoryStream memoryStream && memoryStream.Position == 0)
{
return memoryStream.ToArray();
}

using (var ms = new MemoryStream())
{
await stream.CopyToAsync(ms);
return ms.ToArray();
}
}

public static async Task<T> GetObjectFromBrokeredMessageAsync<T>(Message message, IOrchestrationServiceBlobStore orchestrationServiceBlobStore)
{
if (message == null)
Expand Down
12 changes: 6 additions & 6 deletions src/DurableTask.ServiceBus/ServiceBusOrchestrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,22 +1716,22 @@ async Task<bool> TrySetSessionStateAsync(

try
{
Stream rawStream = await
byte[] sessionState;
using (Stream rawStream = await
RuntimeStateStreamConverter.OrchestrationRuntimeStateToRawStream(
newOrchestrationRuntimeState,
runtimeState,
JsonDataConverter.Default,
this.Settings.TaskOrchestrationDispatcherSettings.CompressOrchestrationState,
this.Settings.SessionSettings,
this.BlobStore,
session.SessionId);

using (var ms = new MemoryStream())
session.SessionId))
{
await rawStream.CopyToAsync(ms);
await session.SetStateAsync(ms.ToArray());
sessionState = await ServiceBusUtils.GetSessionStateBytesAsync(rawStream);
}

await session.SetStateAsync(sessionState);

this.ServiceStats.OrchestrationDispatcherStats.SessionSets.Increment();
}
catch (OrchestrationException exception)
Expand Down
56 changes: 56 additions & 0 deletions test/DurableTask.ServiceBus.Tests/ServiceBusUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,62 @@ public async Task ExternalMessageDeserializationAwaitsBlobStore()
}
}

[TestMethod]
public async Task SessionStateBytesFromMemoryStreamAtStart()
{
byte[] expected = { 1, 2, 3, 4, 5 };
using (var stream = new MemoryStream(expected))
{
byte[] actual = await ServiceBusUtils.GetSessionStateBytesAsync(stream);

CollectionAssert.AreEqual(expected, actual);
Assert.AreNotSame(expected, actual);
}
}

[TestMethod]
public async Task SessionStateBytesFromMemoryStreamNotAtStartReadsRemainingBytes()
{
using (var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }))
{
stream.Position = 2;

byte[] actual = await ServiceBusUtils.GetSessionStateBytesAsync(stream);

CollectionAssert.AreEqual(new byte[] { 3, 4, 5 }, actual);
}
}

[TestMethod]
public async Task SessionStateBytesFromNonMemoryStream()
{
byte[] expected = { 10, 20, 30 };
using (var stream = new BufferedStream(new MemoryStream(expected)))
{
byte[] actual = await ServiceBusUtils.GetSessionStateBytesAsync(stream);

CollectionAssert.AreEqual(expected, actual);
}
}

[TestMethod]
public async Task SessionStateBytesFromEmptyMemoryStream()
{
using (var stream = new MemoryStream())
{
byte[] actual = await ServiceBusUtils.GetSessionStateBytesAsync(stream);

Assert.AreEqual(0, actual.Length);
}
}

[TestMethod]
public async Task SessionStateBytesNullStreamThrows()
{
await Assert.ThrowsExceptionAsync<ArgumentNullException>(
() => ServiceBusUtils.GetSessionStateBytesAsync(null));
}

static async Task AssertInlineMessageRoundTrips(
CompressionStyle compressionStyle,
bool expectSynchronousCompletion)
Expand Down
Loading