Skip to content

[C#] fix: When streaming operation failed the exception is suppressed. #2122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Teams.AI.Application;
using Microsoft.Teams.AI.Exceptions;
using Microsoft.Teams.AI.Tests.TestUtils;
using Moq;

namespace Microsoft.Teams.AI.Tests.Application
{
Expand Down Expand Up @@ -234,5 +235,27 @@ void CaptureSend(Activity[] arg)
Assert.Equal(2, streamer.UpdatesSent());
Assert.Single(streamer.Attachments);
}

[Fact]
public async Task Test_SendActivityThrowsException_AssertThrows()
{
// Arrange
Activity[]? activitiesToSend = null;
void CaptureSend(Activity[] arg)
{
activitiesToSend = arg;
}
var adapter = new SimpleAdapter(CaptureSend);
var turnContextMock = new Mock<ITurnContext>();
turnContextMock.Setup((tc) => tc.SendActivityAsync(It.IsAny<Activity>(), It.IsAny<CancellationToken>())).ThrowsAsync(new Exception("Forbidden operation"));

// Act
StreamingResponse streamer = new(turnContextMock.Object);
Exception ex = await Assert.ThrowsAsync<TeamsAIException>(() => streamer.EndStream());


// Assert
Assert.Equal("Error occured when sending activity while streaming: Forbidden operation", ex.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public Task EndStream()
QueueNextChunk();

// Wait for the queue to drain
return this._queueSync!;
return WaitForQueue()!;
}

/// <summary>
Expand All @@ -133,7 +133,7 @@ private void QueueActivity(Func<Activity> factory)
this._queue.Add(factory);

// If there's no sync in progress, start one
if (this._queueSync == null)
if (this._queueSync == null || this._queueSync.IsCompleted)
{
this._queueSync = DrainQueue();
}
Expand Down Expand Up @@ -198,25 +198,20 @@ private void QueueNextChunk()
/// </summary>
private async Task DrainQueue()
{
await Task.Run(async () =>
try
{
try
while (this._queue.Count > 0)
{
while (this._queue.Count > 0)
{
// Get next activity from queue
Activity activity = _queue[0]();
await SendActivity(activity).ConfigureAwait(false);
_queue.RemoveAt(0);
}
}

finally
{
// Queue is empty, mark as idle
this._queueSync = null;
// Get next activity from queue
Activity activity = _queue[0]();
await SendActivity(activity).ConfigureAwait(false);
_queue.RemoveAt(0);
}
}).ConfigureAwait(false);
}
catch (Exception ex)
{
throw new TeamsAIException($"Error occured when sending activity while streaming: {ex.Message}", ex);
}
}

/// <summary>
Expand Down
Loading