Skip to content

Commit 0efa174

Browse files
committedJan 28, 2025
Add CosmosDB trigger test for Functions
1 parent 59ba067 commit 0efa174

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed
 

‎playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.ApiService/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using Azure.Messaging.EventHubs.Producer;
55
#if !SKIP_UNSTABLE_EMULATORS
66
using Azure.Messaging.ServiceBus;
7-
using Microsoft.Azure.Cosmos;
87
#endif
8+
using Microsoft.Azure.Cosmos;
99
using Azure.Storage.Blobs;
1010
using Azure.Storage.Queues;
1111
using Newtonsoft.Json;
@@ -17,9 +17,9 @@
1717
builder.AddAzureQueueClient("queue");
1818
builder.AddAzureBlobClient("blob");
1919
builder.AddAzureEventHubProducerClient("eventhubs", static settings => settings.EventHubName = "myhub");
20+
builder.AddAzureCosmosClient("cosmosdb");
2021
#if !SKIP_UNSTABLE_EMULATORS
2122
builder.AddAzureServiceBusClient("messaging");
22-
builder.AddAzureCosmosClient("cosmosdb");
2323
#endif
2424

2525
var app = builder.Build();
@@ -67,6 +67,7 @@ static string RandomString(int length)
6767
await sender.SendMessageAsync(message, cancellationToken);
6868
return Results.Ok("Message sent to Azure Service Bus.");
6969
});
70+
#endif
7071

7172
app.MapGet("/publish/cosmosdb", async (CosmosClient cosmosClient) =>
7273
{
@@ -78,7 +79,6 @@ static string RandomString(int length)
7879

7980
return Results.Ok("Document created in Azure Cosmos DB.");
8081
});
81-
#endif
8282

8383
app.MapGet("/", async (HttpClient client) =>
8484
{

‎playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
var eventHubs = builder.AddAzureEventHubs("eventhubs").RunAsEmulator().WithHub("myhub");
77
#if !SKIP_UNSTABLE_EMULATORS
88
var serviceBus = builder.AddAzureServiceBus("messaging").RunAsEmulator().WithQueue("myqueue");
9+
#endif
910
var cosmosDb = builder.AddAzureCosmosDB("cosmosdb")
1011
.RunAsEmulator()
1112
.WithDatabase("mydatabase", (database)
1213
=> database.Containers.AddRange([new("mycontainer", "/id")]));
13-
#endif
1414

1515
var funcApp = builder.AddAzureFunctionsProject<Projects.AzureFunctionsEndToEnd_Functions>("funcapp")
1616
.WithExternalHttpEndpoints()
1717
.WithReference(eventHubs).WaitFor(eventHubs)
1818
#if !SKIP_UNSTABLE_EMULATORS
1919
.WithReference(serviceBus).WaitFor(serviceBus)
20-
.WithReference(cosmosDb).WaitFor(cosmosDb)
2120
#endif
21+
.WithReference(cosmosDb).WaitFor(cosmosDb)
2222
.WithReference(blob)
2323
.WithReference(queue);
2424

2525
builder.AddProject<Projects.AzureFunctionsEndToEnd_ApiService>("apiservice")
2626
.WithReference(eventHubs).WaitFor(eventHubs)
2727
#if !SKIP_UNSTABLE_EMULATORS
2828
.WithReference(serviceBus).WaitFor(serviceBus)
29-
.WithReference(cosmosDb).WaitFor(cosmosDb)
3029
#endif
30+
.WithReference(cosmosDb).WaitFor(cosmosDb)
3131
.WithReference(queue)
3232
.WithReference(blob)
3333
.WithReference(funcApp);

‎playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.Functions/MyCosmosDbTrigger.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if !SKIP_UNSTABLE_EMULATORS
21
using System.Reflection.Metadata;
32
using Microsoft.Azure.Functions.Worker;
43
using Microsoft.Extensions.Logging;
@@ -17,4 +16,3 @@ public void Run([CosmosDBTrigger(
1716
logger.LogInformation("C# cosmosdb trigger function processed: {Count} messages", input.Count);
1817
}
1918
}
20-
#endif

‎tests/Aspire.Playground.Tests/ProjectSpecificTests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ await WaitForAllTextAsync(app,
125125
timeoutSecs: 160);
126126
#endif
127127

128+
// Assert that CosmosDB triggers work correctly
129+
await apiServiceClient.GetAsync("/publish/cosmosdb");
130+
await WaitForAllTextAsync(app,
131+
[
132+
"Executed 'Functions.MyCosmosDbTrigger'"
133+
],
134+
resourceName: "funcapp",
135+
timeoutSecs: 160);
136+
128137
// TODO: The following line is commented out because the test fails due to an erroneous log in the Functions App
129138
// resource that happens after the Functions host has been built. The error log shows up after the Functions
130139
// worker extension has been built and before the host has launched.

0 commit comments

Comments
 (0)