public async Task ConnectionTransportPartitionIdsMatchPartitionProperties() { await using (EventHubScope scope = await EventHubScope.CreateAsync(4)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); await using (var connection = new TestConnectionWithTransport(connectionString)) { EventHubProperties properties = await connection.GetPropertiesAsync(); var partitions = await connection.GetPartitionIdsAsync(); Assert.That(properties, Is.Not.Null, "A set of properties should have been returned."); Assert.That(properties.PartitionIds, Is.Not.Null, "A set of partition identifiers for the properties should have been returned."); Assert.That(partitions, Is.Not.Null, "A set of partition identifiers should have been returned."); Assert.That(partitions, Is.EquivalentTo(properties.PartitionIds), "The partition identifiers returned directly should match those returned with properties."); } } }
/// <summary> /// Runs the sample using the specified Event Hubs connection information. /// </summary> /// /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should target.</param> /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should run against.</param> /// public async Task RunAsync(string connectionString, string eventHubName) { // To interact with Event Hubs, a client is needed. The client manages resources and should be // explicitly closed or disposed, but it is not necessary to do both. In our case, we will take // advantage of the new asynchonous dispose to ensure that we clean up our client when we are // done or when an exception is encountered. await using (var client = new EventHubClient(connectionString, eventHubName)) { // Using the client, we will inspect the Event Hub that it is connected to, getting // access to its informational properties. EventHubProperties properties = await client.GetPropertiesAsync(); Console.WriteLine("The Event Hub has the following properties:"); Console.WriteLine($"\tThe path to the Event Hub from the namespace is: { properties.Path }"); Console.WriteLine($"\tThe Event Hub was created at: { properties.CreatedAtUtc.ToString("yyyy-MM-dd hh:mm:ss tt") }, in UTC."); Console.WriteLine(); // Partitions of an Event Hub are an important concept. Using the Event Hub properties, we'll inspect each of its partitions, // getting access to partition-level properties. foreach (string partitionId in properties.PartitionIds) { PartitionProperties partitionProperties = await client.GetPartitionPropertiesAsync(partitionId); Console.WriteLine($"\tPartiton: { partitionProperties.Id }"); Console.WriteLine($"\t\tThe partition contains no events: { partitionProperties.IsEmpty }"); Console.WriteLine($"\t\tThe first sequence number of an event in the partition is: { partitionProperties.BeginningSequenceNumber }"); Console.WriteLine($"\t\tThe last sequence number of an event in the partition is: { partitionProperties.LastEnqueuedSequenceNumber }"); Console.WriteLine($"\t\tThe last offset of an event in the partition is: { partitionProperties.LastEnqueuedOffset }"); Console.WriteLine($"\t\tThe last time that an event was enqueued in the partition is: { partitionProperties.LastEnqueuedTimeUtc.ToString("yyyy-MM-dd hh:mm:ss tt") }, in UTC."); Console.WriteLine(); } } // At this point, our client has passed its "using" scope and has safely been disposed of. We have no // further obligations. Console.WriteLine(); }
public async Task GetPartitionIdsAsyncDelegatesToGetProperties() { var date = DateTimeOffset.Parse("2015-10-27T12:00:00Z").DateTime; var partitionIds = new[] { "first", "second", "third" }; var properties = new EventHubProperties("dummy", date, partitionIds); var mockClient = new Mock <EventHubClient> { CallBase = true }; mockClient .Setup(client => client.GetPropertiesAsync(It.IsAny <CancellationToken>())) .Returns(Task.FromResult(properties)) .Verifiable("GetPropertiesAcync should have been delegated to."); var actual = await mockClient.Object.GetPartitionIdsAsync(CancellationToken.None); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.EqualTo(partitionIds)); mockClient.VerifyAll(); }
public async Task ConnectionTransportCanRetrieveProperties(EventHubsTransportType transportType) { var partitionCount = 4; await using (EventHubScope scope = await EventHubScope.CreateAsync(partitionCount)) { var connectionString = TestEnvironment.EventHubsConnectionString; await using (var connection = new TestConnectionWithTransport(connectionString, scope.EventHubName, new EventHubConnectionOptions { TransportType = transportType })) { EventHubProperties properties = await connection.GetPropertiesAsync(); Assert.That(properties, Is.Not.Null, "A set of properties should have been returned."); Assert.That(properties.Name, Is.EqualTo(scope.EventHubName), "The property Event Hub name should match the scope."); Assert.That(properties.PartitionIds.Length, Is.EqualTo(partitionCount), "The properties should have the requested number of partitions."); Assert.That(properties.CreatedOn, Is.EqualTo(DateTimeOffset.UtcNow).Within(TimeSpan.FromSeconds(60)), "The Event Hub should have been created just about now."); } } }
public async Task ClientCanRetrievePartitionProperties(TransportType transportType) { var partitionCount = 4; await using (EventHubScope scope = await EventHubScope.CreateAsync(partitionCount)) { var clientOptions = new EventHubClientOptions(); var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); ConnectionStringProperties connectionProperties = ConnectionStringParser.Parse(connectionString); var credential = new SharedAccessSignatureCredential ( new SharedAccessSignature ( $"{ clientOptions.TransportType.GetUriScheme() }://{ connectionProperties.Endpoint.Host }/{ connectionProperties.EventHubName }".ToLowerInvariant(), connectionProperties.SharedAccessKeyName, connectionProperties.SharedAccessKey, TimeSpan.FromHours(4) ) ); await using (var client = new EventHubClient(connectionProperties.Endpoint.Host, connectionProperties.EventHubName, credential, new EventHubClientOptions { TransportType = transportType })) { var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(20)); EventHubProperties properties = await client.GetPropertiesAsync(); var partition = properties.PartitionIds.First(); PartitionProperties partitionProperties = await client.GetPartitionPropertiesAsync(partition, cancellation.Token); Assert.That(partitionProperties, Is.Not.Null, "A set of partition properties should have been returned."); Assert.That(partitionProperties.Id, Is.EqualTo(partition), "The partition identifier should match."); Assert.That(partitionProperties.EventHubName, Is.EqualTo(connectionProperties.EventHubName).Using((IEqualityComparer <string>)StringComparer.InvariantCultureIgnoreCase), "The Event Hub path should match."); Assert.That(partitionProperties.BeginningSequenceNumber, Is.Not.EqualTo(default(long)), "The beginning sequence number should have been populated."); Assert.That(partitionProperties.LastEnqueuedSequenceNumber, Is.Not.EqualTo(default(long)), "The last sequence number should have been populated."); Assert.That(partitionProperties.LastEnqueuedOffset, Is.Not.EqualTo(default(long)), "The last offset should have been populated."); } } }