Skip to content

Commit ab821f7

Browse files
committed
changes for pipeline
1 parent cbcd651 commit ab821f7

File tree

3 files changed

+165
-163
lines changed

3 files changed

+165
-163
lines changed

tests/Chat/ChatMockTests.cs

Lines changed: 0 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -208,166 +208,6 @@ public void CompleteChatStreamingAsyncRespectsTheCancellationToken()
208208
Assert.That(async () => await enumerator.MoveNextAsync(), Throws.InstanceOf<OperationCanceledException>());
209209
}
210210

211-
[SyncOnly]
212-
[Test]
213-
public void StreamingChatCanBeCancelled()
214-
{
215-
MockPipelineResponse response = new MockPipelineResponse(200).WithContent("""
216-
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
217-
218-
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}],"usage":null}
219-
220-
data: [DONE]
221-
""");
222-
223-
OpenAIClientOptions options = new OpenAIClientOptions()
224-
{
225-
Transport = new MockPipelineTransport(_ => response)
226-
};
227-
228-
CancellationTokenSource cancellationTokenSource = new();
229-
cancellationTokenSource.CancelAfter(1000);
230-
231-
ChatClient client = CreateProxyFromClient(GetTestClient<ChatClient>(TestScenario.Chat, options: options));
232-
IEnumerable<ChatMessage> messages = [new UserChatMessage("What are the best pizza toppings? Give me a breakdown on the reasons.")];
233-
234-
CollectionResult<StreamingChatCompletionUpdate> streamingResult = client.CompleteChatStreaming(messages, cancellationToken: cancellationTokenSource.Token);
235-
IEnumerator<StreamingChatCompletionUpdate> enumerator = streamingResult.GetEnumerator();
236-
237-
enumerator.MoveNext();
238-
StreamingChatCompletionUpdate firstUpdate = enumerator.Current;
239-
240-
Assert.That(firstUpdate, Is.Not.Null);
241-
Assert.That(cancellationTokenSource.IsCancellationRequested, Is.False);
242-
243-
Thread.Sleep(1000);
244-
245-
Assert.Throws<OperationCanceledException>(() =>
246-
{
247-
// Should throw for the second update.
248-
Assert.That(cancellationTokenSource.IsCancellationRequested);
249-
Assert.That(cancellationTokenSource.Token.IsCancellationRequested);
250-
enumerator.MoveNext();
251-
enumerator.MoveNext();
252-
});
253-
}
254-
255-
[AsyncOnly]
256-
[Test]
257-
public async Task StreamingChatCanBeCancelledAsync()
258-
{
259-
MockPipelineResponse response = new MockPipelineResponse(200).WithContent("""
260-
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
261-
262-
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}],"usage":null}
263-
264-
data: [DONE]
265-
""");
266-
267-
OpenAIClientOptions options = new OpenAIClientOptions()
268-
{
269-
Transport = new MockPipelineTransport(_ => response)
270-
{
271-
ExpectSyncPipeline = !IsAsync
272-
}
273-
};
274-
275-
CancellationTokenSource cancellationTokenSource = new();
276-
cancellationTokenSource.CancelAfter(1000);
277-
278-
ChatClient client = GetTestClient<ChatClient>(TestScenario.Chat, options: options);
279-
IEnumerable<ChatMessage> messages = [new UserChatMessage("What are the best pizza toppings? Give me a breakdown on the reasons.")];
280-
281-
AsyncCollectionResult<StreamingChatCompletionUpdate> streamingResult = client.CompleteChatStreamingAsync(messages, cancellationToken: cancellationTokenSource.Token);
282-
IAsyncEnumerator<StreamingChatCompletionUpdate> enumerator = streamingResult.GetAsyncEnumerator();
283-
284-
await enumerator.MoveNextAsync();
285-
StreamingChatCompletionUpdate firstUpdate = enumerator.Current;
286-
287-
Assert.That(firstUpdate, Is.Not.Null);
288-
Assert.That(cancellationTokenSource.IsCancellationRequested, Is.False);
289-
290-
await Task.Delay(1000);
291-
292-
Assert.ThrowsAsync<OperationCanceledException>(async () =>
293-
{
294-
// Should throw for the second update.
295-
Assert.That(cancellationTokenSource.IsCancellationRequested);
296-
Assert.That(cancellationTokenSource.Token.IsCancellationRequested);
297-
await enumerator.MoveNextAsync();
298-
await enumerator.MoveNextAsync();
299-
});
300-
}
301-
302-
[Test]
303-
public async Task CompleteChatStreamingClosesNetworkStream()
304-
{
305-
MockPipelineResponse response = new MockPipelineResponse(200).WithContent("""
306-
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
307-
308-
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}],"usage":null}
309-
310-
data: [DONE]
311-
""");
312-
313-
OpenAIClientOptions options = new()
314-
{
315-
Transport = new MockPipelineTransport(_ => response)
316-
{
317-
ExpectSyncPipeline = !IsAsync
318-
}
319-
};
320-
321-
ChatClient client = CreateProxyFromClient(GetTestClient<ChatClient>(TestScenario.Chat, options: options));
322-
IEnumerable<ChatMessage> messages = [new UserChatMessage("What are the best pizza toppings? Give me a breakdown on the reasons.")];
323-
324-
int updateCount = 0;
325-
TimeSpan? firstTokenReceiptTime = null;
326-
TimeSpan? latestTokenReceiptTime = null;
327-
Stopwatch stopwatch = Stopwatch.StartNew();
328-
AsyncCollectionResult<StreamingChatCompletionUpdate> streamingResult = client.CompleteChatStreamingAsync(messages);
329-
330-
Assert.That(streamingResult, Is.InstanceOf<AsyncCollectionResult<StreamingChatCompletionUpdate>>());
331-
Assert.That(response.IsDisposed, Is.False);
332-
333-
await foreach (StreamingChatCompletionUpdate chatUpdate in streamingResult)
334-
{
335-
firstTokenReceiptTime ??= stopwatch.Elapsed;
336-
latestTokenReceiptTime = stopwatch.Elapsed;
337-
updateCount++;
338-
339-
Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds);
340-
}
341-
342-
stopwatch.Stop();
343-
344-
Assert.That(response.IsDisposed);
345-
}
346-
347-
[Test]
348-
public void GetChatCompletionMessagesWithInvalidParameters()
349-
{
350-
ChatClient client = CreateProxyFromClient(GetTestClient<ChatClient>(scenario: TestScenario.Chat));
351-
352-
// Test with null completion ID
353-
Assert.ThrowsAsync<ArgumentNullException>(async () =>
354-
{
355-
await foreach (var message in client.GetChatCompletionMessagesAsync(null))
356-
{
357-
// Should not reach here
358-
}
359-
});
360-
361-
// Test with empty completion ID
362-
Assert.ThrowsAsync<ArgumentException>(async () =>
363-
{
364-
await foreach (var message in client.GetChatCompletionMessagesAsync(""))
365-
{
366-
// Should not reach here
367-
}
368-
});
369-
}
370-
371211
private OpenAIClientOptions GetClientOptionsWithMockResponse(int status, string content)
372212
{
373213
MockPipelineResponse response = new MockPipelineResponse(status).WithContent(content);

tests/Chat/ChatTests.cs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.ClientModel.TestFramework;
2+
using Microsoft.ClientModel.TestFramework.Mocks;
23
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
34
using NUnit.Framework;
45
using OpenAI.Chat;
@@ -15,6 +16,7 @@
1516
using System.Net;
1617
using System.Text;
1718
using System.Text.Json;
19+
using System.Threading;
1820
using System.Threading.Tasks;
1921
using static OpenAI.Tests.Telemetry.TestMeterListener;
2022
using static OpenAI.Tests.TestHelpers;
@@ -877,6 +879,30 @@ public async Task GetChatCompletionMessagesHandlesNonExistentCompletion()
877879
}
878880
}
879881

882+
[Test]
883+
public void GetChatCompletionMessagesWithInvalidParameters()
884+
{
885+
ChatClient client = CreateProxyFromClient(GetTestClient<ChatClient>(scenario: TestScenario.Chat));
886+
887+
// Test with null completion ID
888+
Assert.ThrowsAsync<ArgumentNullException>(async () =>
889+
{
890+
await foreach (var message in client.GetChatCompletionMessagesAsync(null))
891+
{
892+
// Should not reach here
893+
}
894+
});
895+
896+
// Test with empty completion ID
897+
Assert.ThrowsAsync<ArgumentException>(async () =>
898+
{
899+
await foreach (var message in client.GetChatCompletionMessagesAsync(""))
900+
{
901+
// Should not reach here
902+
}
903+
});
904+
}
905+
880906
[Test]
881907
public async Task ChatServiceTierWorks()
882908
{
@@ -894,6 +920,142 @@ public async Task ChatServiceTierWorks()
894920
Assert.That(completion.ServiceTier, Is.EqualTo(ChatServiceTier.Default));
895921
}
896922

923+
[SyncOnly]
924+
[Test]
925+
public void StreamingChatCanBeCancelled()
926+
{
927+
MockPipelineResponse response = new MockPipelineResponse(200).WithContent("""
928+
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
929+
930+
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}],"usage":null}
931+
932+
data: [DONE]
933+
""");
934+
935+
OpenAIClientOptions options = new OpenAIClientOptions()
936+
{
937+
Transport = new MockPipelineTransport(_ => response)
938+
};
939+
940+
CancellationTokenSource cancellationTokenSource = new();
941+
cancellationTokenSource.CancelAfter(1000);
942+
943+
ChatClient client = CreateProxyFromClient(GetTestClient<ChatClient>(TestScenario.Chat, options: options));
944+
IEnumerable<ChatMessage> messages = [new UserChatMessage("What are the best pizza toppings? Give me a breakdown on the reasons.")];
945+
946+
CollectionResult<StreamingChatCompletionUpdate> streamingResult = client.CompleteChatStreaming(messages, cancellationToken: cancellationTokenSource.Token);
947+
IEnumerator<StreamingChatCompletionUpdate> enumerator = streamingResult.GetEnumerator();
948+
949+
enumerator.MoveNext();
950+
StreamingChatCompletionUpdate firstUpdate = enumerator.Current;
951+
952+
Assert.That(firstUpdate, Is.Not.Null);
953+
Assert.That(cancellationTokenSource.IsCancellationRequested, Is.False);
954+
955+
Thread.Sleep(1000);
956+
957+
Assert.Throws<OperationCanceledException>(() =>
958+
{
959+
// Should throw for the second update.
960+
Assert.That(cancellationTokenSource.IsCancellationRequested);
961+
Assert.That(cancellationTokenSource.Token.IsCancellationRequested);
962+
enumerator.MoveNext();
963+
enumerator.MoveNext();
964+
});
965+
}
966+
967+
[AsyncOnly]
968+
[Test]
969+
public async Task StreamingChatCanBeCancelledAsync()
970+
{
971+
MockPipelineResponse response = new MockPipelineResponse(200).WithContent("""
972+
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
973+
974+
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}],"usage":null}
975+
976+
data: [DONE]
977+
""");
978+
979+
OpenAIClientOptions options = new OpenAIClientOptions()
980+
{
981+
Transport = new MockPipelineTransport(_ => response)
982+
{
983+
ExpectSyncPipeline = !IsAsync
984+
}
985+
};
986+
987+
CancellationTokenSource cancellationTokenSource = new();
988+
cancellationTokenSource.CancelAfter(1000);
989+
990+
ChatClient client = GetTestClient<ChatClient>(TestScenario.Chat, options: options);
991+
IEnumerable<ChatMessage> messages = [new UserChatMessage("What are the best pizza toppings? Give me a breakdown on the reasons.")];
992+
993+
AsyncCollectionResult<StreamingChatCompletionUpdate> streamingResult = client.CompleteChatStreamingAsync(messages, cancellationToken: cancellationTokenSource.Token);
994+
IAsyncEnumerator<StreamingChatCompletionUpdate> enumerator = streamingResult.GetAsyncEnumerator();
995+
996+
await enumerator.MoveNextAsync();
997+
StreamingChatCompletionUpdate firstUpdate = enumerator.Current;
998+
999+
Assert.That(firstUpdate, Is.Not.Null);
1000+
Assert.That(cancellationTokenSource.IsCancellationRequested, Is.False);
1001+
1002+
await Task.Delay(1000);
1003+
1004+
Assert.ThrowsAsync<OperationCanceledException>(async () =>
1005+
{
1006+
// Should throw for the second update.
1007+
Assert.That(cancellationTokenSource.IsCancellationRequested);
1008+
Assert.That(cancellationTokenSource.Token.IsCancellationRequested);
1009+
await enumerator.MoveNextAsync();
1010+
await enumerator.MoveNextAsync();
1011+
});
1012+
}
1013+
1014+
[Test]
1015+
public async Task CompleteChatStreamingClosesNetworkStream()
1016+
{
1017+
MockPipelineResponse response = new MockPipelineResponse(200).WithContent("""
1018+
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
1019+
1020+
data: {"id":"chatcmpl-A7mKGugwaczn3YyrJLlZY6CM0Wlkr","object":"chat.completion.chunk","created":1726417424,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_483d39d857","choices":[{"index":0,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}],"usage":null}
1021+
1022+
data: [DONE]
1023+
""");
1024+
1025+
OpenAIClientOptions options = new()
1026+
{
1027+
Transport = new MockPipelineTransport(_ => response)
1028+
{
1029+
ExpectSyncPipeline = !IsAsync
1030+
}
1031+
};
1032+
1033+
ChatClient client = CreateProxyFromClient(GetTestClient<ChatClient>(TestScenario.Chat, options: options));
1034+
IEnumerable<ChatMessage> messages = [new UserChatMessage("What are the best pizza toppings? Give me a breakdown on the reasons.")];
1035+
1036+
int updateCount = 0;
1037+
TimeSpan? firstTokenReceiptTime = null;
1038+
TimeSpan? latestTokenReceiptTime = null;
1039+
Stopwatch stopwatch = Stopwatch.StartNew();
1040+
AsyncCollectionResult<StreamingChatCompletionUpdate> streamingResult = client.CompleteChatStreamingAsync(messages);
1041+
1042+
Assert.That(streamingResult, Is.InstanceOf<AsyncCollectionResult<StreamingChatCompletionUpdate>>());
1043+
Assert.That(response.IsDisposed, Is.False);
1044+
1045+
await foreach (StreamingChatCompletionUpdate chatUpdate in streamingResult)
1046+
{
1047+
firstTokenReceiptTime ??= stopwatch.Elapsed;
1048+
latestTokenReceiptTime = stopwatch.Elapsed;
1049+
updateCount++;
1050+
1051+
Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds);
1052+
}
1053+
1054+
stopwatch.Stop();
1055+
1056+
Assert.That(response.IsDisposed);
1057+
}
1058+
8971059
[OneTimeTearDown]
8981060
public void TearDown()
8991061
{

tests/Realtime/RealtimeSmokeTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using Microsoft.ClientModel.TestFramework;
2+
using NUnit.Framework;
23
using OpenAI.Realtime;
34
using System;
45
using System.ClientModel.Primitives;
@@ -9,9 +10,8 @@ namespace OpenAI.Tests.Realtime;
910

1011
#pragma warning disable OPENAI002
1112

12-
[TestFixture(false)]
1313
[Category("Smoke")]
14-
public class RealtimeSmokeTests : RealtimeTestFixtureBase
14+
public class RealtimeSmokeTests : ClientTestBase
1515
{
1616
public RealtimeSmokeTests(bool isAsync) : base(isAsync)
1717
{ }

0 commit comments

Comments
 (0)