public void CreateSenderCreatesDefaultWhenOptionsAreNotSet() { var clientOptions = new EventHubClientOptions { Retry = new ExponentialRetry(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3), 5), DefaultTimeout = TimeSpan.FromHours(24) }; var senderOptions = new EventSenderOptions { PartitionId = "123", Retry = null, Timeout = TimeSpan.Zero }; var expected = new EventSenderOptions { PartitionId = senderOptions.PartitionId, Retry = clientOptions.Retry, Timeout = clientOptions.DefaultTimeout }; var connectionString = "Endpoint=value.com;SharedAccessKeyName=[value];SharedAccessKey=[value];EntityPath=[value]"; var mockClient = new ReadableOptionsMock(connectionString, clientOptions); mockClient.CreateSender(senderOptions); Assert.That(mockClient.SenderOptions, Is.Not.Null, "The sender options should have been set."); Assert.That(mockClient.SenderOptions, Is.Not.SameAs(senderOptions), "The options should have been cloned."); Assert.That(mockClient.SenderOptions.PartitionId, Is.EqualTo(expected.PartitionId), "The partition identifiers should match."); Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)mockClient.SenderOptions.Retry, (ExponentialRetry)expected.Retry), "The retries should match."); Assert.That(mockClient.SenderOptions.TimeoutOrDefault, Is.EqualTo(expected.TimeoutOrDefault), "The timeouts should match."); }
public void DefaultTimeoutUsesDefaultValueIfNormalizesValueINotSpecified(int?noTimeoutValue) { var options = new EventSenderOptions(); var timeoutValue = (noTimeoutValue.HasValue) ? TimeSpan.Zero : (TimeSpan?)null; options.Timeout = timeoutValue; Assert.That(options.Timeout, Is.EqualTo(timeoutValue), "The value supplied by the caller should be preserved."); Assert.That(options.TimeoutOrDefault, Is.Null, "The timeout value should be normalized to null internally."); }
public void CreateSenderInvokesTheTransportClient() { var transportClient = new ObservableTransportClientMock(); var client = new InjectableTransportClientMock(transportClient, "Endpoint=sb://not-real.servicebus.windows.net/;SharedAccessKeyName=DummyKey;SharedAccessKey=[not_real];EntityPath=fake"); var expectedOptions = new EventSenderOptions { Retry = Retry.Default }; client.CreateSender(expectedOptions); var actualOptions = transportClient.CreateSenderCalledWithOptions; Assert.That(actualOptions, Is.Not.Null, "The sender options should have been set."); Assert.That(actualOptions.PartitionId, Is.EqualTo(expectedOptions.PartitionId), "The partition identifiers should match."); Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)actualOptions.Retry, (ExponentialRetry)expectedOptions.Retry), "The retries should match."); Assert.That(actualOptions.TimeoutOrDefault, Is.EqualTo(expectedOptions.TimeoutOrDefault), "The timeouts should match."); }
private static async Task CreateSenderAndReceiver() { Console.Write("Creating the Sender and Receivers... "); var partition = (await client.GetPartitionIdsAsync()).First(); var senderOptions = new EventSenderOptions { PartitionId = partition }; var receiverOptions = new EventReceiverOptions { BeginReceivingAt = EventPosition.NewEventsOnly }; sender = client.CreateSender(senderOptions); receiver = client.CreateReceiver(partition, receiverOptions); Console.WriteLine("\tdone"); }
public async Task SenderWithOptionsCanSend() { await using (var scope = await EventHubScope.CreateAsync(4)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); var senderOptions = new EventSenderOptions { Retry = new ExponentialRetry(TimeSpan.FromSeconds(0.25), TimeSpan.FromSeconds(45), 5) }; await using (var client = new EventHubClient(connectionString)) await using (var sender = client.CreateSender(senderOptions)) { var events = new[] { new EventData(Encoding.UTF8.GetBytes("AWord")) }; Assert.That(async() => await sender.SendAsync(events), Throws.Nothing); } } }
public void CloneProducesACopy() { var options = new EventSenderOptions { PartitionId = "some_partition_id_123", Retry = new ExponentialRetry(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(5), 6), Timeout = TimeSpan.FromMinutes(65) }; var clone = options.Clone(); Assert.That(clone, Is.Not.Null, "The clone should not be null."); Assert.That(clone.PartitionId, Is.EqualTo(options.PartitionId), "The partition identifier of the clone should match."); Assert.That(clone.Timeout, Is.EqualTo(options.Timeout), "The timeout of the clone should match."); Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)clone.Retry, (ExponentialRetry)options.Retry), "The retry of the clone should be considered equal."); Assert.That(clone.Retry, Is.Not.SameAs(options.Retry), "The retry of the clone should be a copy, not the same instance."); }
/// <summary> /// Creates an event sender responsible for transmitting <see cref="EventData" /> to the /// Event Hub, grouped together in batches. Depending on the <paramref name="senderOptions"/> /// specified, the sender may be created to allow event data to be automatically routed to an available /// partition or specific to a partition. /// </summary> /// /// <param name="senderOptions">The set of options to apply when creating the sender.</param> /// /// <returns>An event sender configured in the requested manner.</returns> /// public override EventSender CreateSender(EventSenderOptions senderOptions) { TrackOne.EventDataSender CreateSenderFactory() { var sender = TrackOneClient.CreateEventSender(senderOptions.PartitionId); (TimeSpan minBackoff, TimeSpan maxBackoff, int maxRetries) = ((ExponentialRetry)senderOptions.Retry).GetProperties(); sender.RetryPolicy = new RetryExponential(minBackoff, maxBackoff, maxRetries); return(sender); } return(new EventSender ( new TrackOneEventSender(CreateSenderFactory), TrackOneClient.EventHubName, senderOptions )); }
public async Task SenderCanSendToASpecificPartition() { await using (var scope = await EventHubScope.CreateAsync(4)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); await using (var client = new EventHubClient(connectionString)) { var partition = (await client.GetPartitionIdsAsync()).First(); var senderOptions = new EventSenderOptions { PartitionId = partition }; await using (var sender = client.CreateSender(senderOptions)) { var events = new[] { new EventData(Encoding.UTF8.GetBytes("AWord")) }; Assert.That(async() => await sender.SendAsync(events), Throws.Nothing); } } } }
static void Main(string[] args) { Console.WriteLine("Starting..."); // Create options object var options = new EventSenderOptions { // Url to send data to ServerUri = new Uri(@"http://*****:*****@"C:\Users\Admin\Desktop\batches", // Type of the serialization when sending events Serialization = Serialization.Json, // Size of the batch to send events to the server EventBatchSize = 2 }; // Create objects var obj1 = new { EventName = "EventHello", Hello = "Lol", World = 123, }; var obj2 = new { EventName = "EventHello", Hello = "Kek", World = 123456, }; if (EventSender.BatchEventToSend(obj1, options)) { Console.WriteLine($"Server accepted events after batching {nameof(obj1)}!"); } if (EventSender.BatchEventToSend(obj2, options)) { Console.WriteLine($"Server accepted events after batching {nameof(obj2)}!"); } var complexOne = new { EventName = "EventComplex", ObjectProp = new { DateOfTheProp = DateTime.Now, CodeOfTheProp = 999 }, Integers = new int[] { 5, 6, 7 } }; EventSender.DefaultOptions = options; if (EventSender.SendEventNow(complexOne)) { Console.WriteLine($"Server accepted when sending {nameof(complexOne)}!"); } Console.WriteLine("Done"); }
public override EventSender CreateSender(EventSenderOptions senderOptions = default) { CreateSenderCalledWithOptions = senderOptions; return(default(EventSender)); }
/// <summary> /// Creates an event sender responsible for transmitting <see cref="EventData" /> to the /// Event Hub, grouped together in batches. Depending on the <paramref name="senderOptions"/> /// specified, the sender may be created to allow event data to be automatically routed to an available /// partition or specific to a partition. /// </summary> /// /// <param name="senderOptions">The set of options to apply when creating the sender.</param> /// /// <returns>An event sender configured in the requested manner.</returns> /// public abstract EventSender CreateSender(EventSenderOptions senderOptions);