Skip to content

Commit

Permalink
[Event Hubs] Move T1 migration guide snippets (#37908)
Browse files Browse the repository at this point in the history
* [Event Hubs] Move T1 migration guide snippets

The focus of these changes is to move the migration snippets demonstrating
use of `Microsoft.Azure.EventHubs` to the `Azure.Messaging.EventHubs` test
projects.  This consolidates the build pipeline used for validating the
snippets and allows for removal of the T1 code once the library is formally
retired.
  • Loading branch information
jsquire authored Jul 28, 2023
1 parent 97ca9be commit 4b50567
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 176 deletions.
2 changes: 2 additions & 0 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@
<PackageReference Update="Microsoft.Azure.Core.NewtonsoftJson" Version="1.0.0" />
<PackageReference Update="Microsoft.Azure.Devices" Version="1.38.2" />
<PackageReference Update="Microsoft.Azure.Devices.Client" Version="1.41.3" />
<PackageReference Update="Microsoft.Azure.EventHubs" Version="4.3.2" />
<PackageReference Update="Microsoft.Azure.EventHubs.Processor" Version="4.3.2" />
<PackageReference Update="Microsoft.Azure.Graph.RBAC" Version="2.2.2-preview" />
<PackageReference Update="Microsoft.Azure.KeyVault" Version="3.0.5" />
<PackageReference Update="Microsoft.Azure.KeyVault.Core" Version="3.0.5" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

<ItemGroup>
<PackageReference Include="Azure.ResourceManager.EventHubs" />
<PackageReference Include="Microsoft.Azure.EventHubs" />
<PackageReference Include="Microsoft.Azure.EventHubs.Processor" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NUnit" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs.Tests;
using Microsoft.Azure.EventHubs.Processor;
using NUnit.Framework;

namespace Microsoft.Azure.EventHubs.Tests.Snippets
{
/// <summary>
/// The suite of tests defining the T1 snippets used in the Event Hubs
/// migration guides.
/// </summary>
///
[TestFixture]
[Category(TestCategory.Live)]
[Category(TestCategory.DisallowVisualStudioLiveUnitTesting)]
public class TrackOneMigrationGuideSnippets
{
/// <summary>
/// Performs basic smoke test validation of the contained snippet.
/// </summary>
///
[Test]
public async Task BasicEventProcessorHost()
{
await using var scope = await EventHubScope.CreateAsync(1);

#region Snippet:EventHubs_Migrate_T1_BasicEventProcessorHost
#if SNIPPET
var storageConnectionString = "<< CONNECTION STRING FOR THE STORAGE ACCOUNT >>";
var blobContainerName = "<< NAME OF THE BLOB CONTAINER >>";

var eventHubsConnectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";
var consumerGroup = "<< NAME OF THE EVENT HUB CONSUMER GROUP >>";
#else
var storageConnectionString = StorageTestEnvironment.Instance.StorageConnectionString;
var blobContainerName = "migragionsample";

var eventHubsConnectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString;
var eventHubName = scope.EventHubName;
var consumerGroup = PartitionReceiver.DefaultConsumerGroupName;
#endif

var eventProcessorHost = new EventProcessorHost(
eventHubName,
consumerGroup,
eventHubsConnectionString,
storageConnectionString,
blobContainerName);

try
{
// Registering the processor class will also signal the
// host to begin processing events.

await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();

// The processor runs in the background, to allow it to process,
// this example will wait for 30 seconds and then trigger
// cancellation.

using var cancellationSource = new CancellationTokenSource();
cancellationSource.CancelAfter(TimeSpan.FromSeconds(30));

await Task.Delay(Timeout.Infinite, cancellationSource.Token);
}
catch (TaskCanceledException)
{
// This is expected when the cancellation token is
// signaled.
}
finally
{
// Unregistering the processor class will signal the
// host to stop processing.

await eventProcessorHost.UnregisterEventProcessorAsync();
}

#endregion
}
}

#pragma warning disable SA1402 // File may only contain a single type
#region Snippet:EventHubs_Migrate_T1_SimpleEventProcessor

public class SimpleEventProcessor : IEventProcessor
{
public Task CloseAsync(PartitionContext context, CloseReason reason)
{
Debug.WriteLine($"Partition '{context.PartitionId}' is closing.");
return Task.CompletedTask;
}

public Task OpenAsync(PartitionContext context)
{
Debug.WriteLine($"Partition: '{context.PartitionId}' was initialized.");
return Task.CompletedTask;
}

public Task ProcessErrorAsync(PartitionContext context, Exception error)
{
Debug.WriteLine(
$"Error for partition: {context.PartitionId}, " +
$"Error: {error.Message}");

return Task.CompletedTask;
}

public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (var eventData in messages)
{
var data = Encoding.UTF8.GetString(
eventData.Body.Array,
eventData.Body.Offset,
eventData.Body.Count);

Debug.WriteLine(
$"Event received for partition: '{context.PartitionId}', " +
$"Data: '{data}'");
}

return Task.CompletedTask;
}
}

#endregion
#pragma warning restore SA1402 // File may only contain a single type
}
9 changes: 6 additions & 3 deletions sdk/eventhub/Azure.Messaging.EventHubs/MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ AzureActiveDirectoryTokenProvider.AuthenticationCallback authCallback =
};

EventHubClient client = EventHubClient.CreateWithAzureActiveDirectory(
new Uri(fullyQualifiedNamespace),
new Uri($"sb://{ fullyQualifiedNamespace }"),
eventHubName,
authCallback,
authority);
Expand Down Expand Up @@ -643,9 +643,12 @@ try

IEnumerable<EventData> events = await receiver.ReceiveAsync(50);

foreach (var eventData in events)
if (events != null)
{
Debug.WriteLine($"Read event of length { eventData.Body.Count } from { firstPartition }");
foreach (var eventData in events)
{
Debug.WriteLine($"Read event of length { eventData.Body.Count } from { firstPartition }");
}
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Azure.ResourceManager.EventHubs" />
<PackageReference Include="Azure.Storage.Blobs" />
<PackageReference Include="Microsoft.Azure.EventHubs" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NUnit" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Identity;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Producer;
using Azure.Identity;
using NUnit.Framework;

namespace Azure.Messaging.EventHubs.Tests.Snippets
Expand All @@ -18,10 +18,6 @@ namespace Azure.Messaging.EventHubs.Tests.Snippets
[Category(TestCategory.DisallowVisualStudioLiveUnitTesting)]
public class MigrationGuideSnippetsLiveTests
{
/// <summary>
/// Performs basic smoke test validation of the contained snippet.
/// </summary>
///
[Test]
public void CreateWithConnectionString()
{
Expand Down
Loading

0 comments on commit 4b50567

Please sign in to comment.