Skip to content

Commit 839ee4d

Browse files
authored
[Web PubSub] Adding live test for web pubsub (Azure#25106)
* Adding live test for web pubsub * Add live test * Adding arm template * Ignore live test until it is supported by CI * Update code snnipet
1 parent a21ada1 commit 839ee4d

File tree

9 files changed

+563
-21
lines changed

9 files changed

+563
-21
lines changed

sdk/webpubsub/Azure.Messaging.WebPubSub/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ When a client is connected, it can send messages to the upstream application, or
7373
### Broadcast a text message to all clients
7474

7575
```C# Snippet:WebPubSubHelloWorld
76-
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
76+
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
7777

7878
serviceClient.SendToAll("Hello World!");
7979
```
8080

8181
### Broadcast a JSON message to all clients
8282

8383
```C# Snippet:WebPubSubSendJson
84-
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
84+
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
8585

8686
serviceClient.SendToAll(RequestContent.Create(
8787
new
@@ -95,7 +95,7 @@ serviceClient.SendToAll(RequestContent.Create(
9595
### Broadcast a binary message to all clients
9696

9797
```C# Snippet:WebPubSubSendBinary
98-
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
98+
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
9999

100100
Stream stream = BinaryData.FromString("Hello World!").ToStream();
101101
serviceClient.SendToAll(RequestContent.Create(stream), ContentType.ApplicationOctetStream);

sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
7+
using System.Linq;
8+
using System.Web.UI;
69

710
using Azure.Core;
811
using Azure.Core.TestFramework;
@@ -15,20 +18,20 @@ public class WebPubSubSamples : SamplesBase<WebPubSubTestEnvironment>
1518
{
1619
public void HelloWorld()
1720
{
18-
var endpoint = TestEnvironment.Endpoint;
19-
var key = TestEnvironment.Key;
21+
var connectionString = TestEnvironment.ConnectionString;
2022

2123
#region Snippet:WebPubSubHelloWorld
22-
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
24+
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
2325

2426
serviceClient.SendToAll("Hello World!");
2527
#endregion
2628
}
2729

2830
public void Authenticate()
2931
{
30-
var endpoint = TestEnvironment.Endpoint;
31-
var key = TestEnvironment.Key;
32+
var connectionString = TestEnvironment.ConnectionString;
33+
var endpoint = ParseConnectionString(connectionString)["Endpoint"];
34+
var key = ParseConnectionString(connectionString)["AccessKey"];
3235

3336
#region Snippet:WebPubSubAuthenticate
3437
// Create a WebPubSubServiceClient that will authenticate using a key credential.
@@ -47,11 +50,10 @@ public void HelloWorldWithConnectionString()
4750

4851
public void JsonMessage()
4952
{
50-
var endpoint = TestEnvironment.Endpoint;
51-
var key = TestEnvironment.Key;
53+
var connectionString = TestEnvironment.ConnectionString;
5254

5355
#region Snippet:WebPubSubSendJson
54-
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
56+
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
5557

5658
serviceClient.SendToAll(RequestContent.Create(
5759
new
@@ -65,11 +67,10 @@ public void JsonMessage()
6567

6668
public void BinaryMessage()
6769
{
68-
var endpoint = TestEnvironment.Endpoint;
69-
var key = TestEnvironment.Key;
70+
var connectionString = TestEnvironment.ConnectionString;
7071

7172
#region Snippet:WebPubSubSendBinary
72-
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
73+
var serviceClient = new WebPubSubServiceClient(connectionString, "some_hub");
7374

7475
Stream stream = BinaryData.FromString("Hello World!").ToStream();
7576
serviceClient.SendToAll(RequestContent.Create(stream), ContentType.ApplicationOctetStream);
@@ -78,11 +79,10 @@ public void BinaryMessage()
7879

7980
public void AddUserToGroup()
8081
{
81-
var endpoint = TestEnvironment.Endpoint;
82-
var key = TestEnvironment.Key;
83-
84-
var client = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
82+
var connectionString = TestEnvironment.ConnectionString;
83+
var client = new WebPubSubServiceClient(connectionString, "some_hub");
8584

85+
#region Snippet:WebPubSubAddUserToGroup
8686
client.AddUserToGroup("some_group", "some_user");
8787

8888
// Avoid sending messages to users who do not exist.
@@ -92,6 +92,20 @@ public void AddUserToGroup()
9292
}
9393

9494
client.RemoveUserFromGroup("some_group", "some_user");
95+
#endregion
96+
}
97+
98+
private static Dictionary<string, string> ParseConnectionString(string connectionString)
99+
{
100+
return connectionString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(pair =>
101+
{
102+
var map = pair.Split('=');
103+
if (map.Length != 2)
104+
{
105+
return default;
106+
}
107+
return new KeyValuePair<string, string>(map[0], map[1]);
108+
}).Where(s => !string.IsNullOrEmpty(s.Key)).ToDictionary(p => p.Key, p => p.Value, StringComparer.OrdinalIgnoreCase);
95109
}
96110
}
97111
}

sdk/webpubsub/Azure.Messaging.WebPubSub/tests/SessionRecords/WebPubSubGeneralTests/ServiceClientCanBroadcastMessages.json

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/webpubsub/Azure.Messaging.WebPubSub/tests/SessionRecords/WebPubSubGeneralTests/ServiceClientCanBroadcastMessagesAsync.json

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
7+
namespace Azure.Rest.WebPubSub.Tests
8+
{
9+
public static class TaskExtensions
10+
{
11+
public static async Task OrTimeout(this Task task, int millisecondsDelay = 5000)
12+
{
13+
var completed = await Task.WhenAny(task, Task.Delay(millisecondsDelay));
14+
if (!task.IsCompleted)
15+
{
16+
throw new TimeoutException();
17+
}
18+
}
19+
}
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Net.WebSockets;
8+
using System.Text;
9+
using System.Text.Json;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
13+
using Azure.Core;
14+
using Azure.Core.TestFramework;
15+
using Azure.Messaging.WebPubSub;
16+
17+
using NUnit.Framework;
18+
19+
namespace Azure.Rest.WebPubSub.Tests
20+
{
21+
public class WebPubSubGeneralTests : RecordedTestBase<WebPubSubTestEnvironment>
22+
{
23+
public WebPubSubGeneralTests(bool isAsync) : base(isAsync)
24+
{
25+
}
26+
27+
[Test]
28+
public async Task ServiceClientCanBroadcastMessages()
29+
{
30+
WebPubSubServiceClientOptions options = InstrumentClientOptions(new WebPubSubServiceClientOptions());
31+
32+
var serviceClient = new WebPubSubServiceClient(TestEnvironment.ConnectionString, nameof(ServiceClientCanBroadcastMessages), options);
33+
// broadcast messages
34+
var textContent = "Hello";
35+
var response = await serviceClient.SendToAllAsync(textContent, ContentType.TextPlain);
36+
37+
Assert.AreEqual(202, response.Status);
38+
39+
var jsonContent = BinaryData.FromObjectAsJson(new { hello = "world" });
40+
response = await serviceClient.SendToAllAsync(RequestContent.Create(jsonContent), ContentType.ApplicationJson);
41+
Assert.AreEqual(202, response.Status);
42+
var binaryContent = BinaryData.FromString("Hello");
43+
response = await serviceClient.SendToAllAsync(RequestContent.Create(binaryContent), ContentType.ApplicationOctetStream);
44+
Assert.AreEqual(202, response.Status);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)