| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information.using System; |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.EventHubs.ServiceFabricProcessor |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Wrappers for the underlying Event Hub client which allow mocking. |
| | 12 | | /// The interfaces include only the client functionality used by the Service Fabric Processor. |
| | 13 | | /// </summary> |
| | 14 | | public class EventHubWrappers |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Interface for a partition receiver. |
| | 18 | | /// </summary> |
| | 19 | | public interface IPartitionReceiver |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// |
| | 23 | | /// </summary> |
| | 24 | | int PrefetchCount { get; set; } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="maxEventCount"></param> |
| | 29 | | /// <param name="waitTime"></param> |
| | 30 | | /// <returns></returns> |
| | 31 | | Task<IEnumerable<EventData>> ReceiveAsync(int maxEventCount, TimeSpan waitTime); |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="receiveHandler"></param> |
| | 36 | | /// <param name="invokeWhenNoEvents"></param> |
| | 37 | | void SetReceiveHandler(IPartitionReceiveHandler receiveHandler, bool invokeWhenNoEvents = false); |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// </summary> |
| | 41 | | /// <returns></returns> |
| | 42 | | Task CloseAsync(); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Interface representing EventHubClient |
| | 47 | | /// </summary> |
| | 48 | | public interface IEventHubClient |
| | 49 | | { |
| | 50 | | /// <summary> |
| | 51 | | /// </summary> |
| | 52 | | /// <returns></returns> |
| | 53 | | Task<EventHubRuntimeInformation> GetRuntimeInformationAsync(); |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="consumerGroupName"></param> |
| | 58 | | /// <param name="partitionId"></param> |
| | 59 | | /// <param name="eventPosition"></param> |
| | 60 | | /// <param name="epoch"></param> |
| | 61 | | /// <param name="receiverOptions"></param> |
| | 62 | | /// <returns></returns> |
| | 63 | | IPartitionReceiver CreateEpochReceiver(string consumerGroupName, string partitionId, EventPosition eventPosi |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// </summary> |
| | 67 | | /// <returns></returns> |
| | 68 | | Task CloseAsync(); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Interface for an EventHubClient factory so that we can have factories which dispense different implementatio |
| | 73 | | /// </summary> |
| | 74 | | public interface IEventHubClientFactory |
| | 75 | | { |
| | 76 | | /// <summary> |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="connectionString"></param> |
| | 79 | | /// <param name="receiveTimeout"></param> |
| | 80 | | /// <returns></returns> |
| | 81 | | IEventHubClient Create(string connectionString, TimeSpan receiveTimeout); |
| | 82 | | } |
| | 83 | |
|
| | 84 | | internal class PartitionReceiverWrapper : IPartitionReceiver |
| | 85 | | { |
| | 86 | | private readonly PartitionReceiver inner; |
| | 87 | |
|
| 0 | 88 | | internal PartitionReceiverWrapper(PartitionReceiver receiver) |
| | 89 | | { |
| 0 | 90 | | this.inner = receiver; |
| 0 | 91 | | this.MaxBatchSize = 10; // TODO get this from somewhere real |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | public int PrefetchCount |
| | 95 | | { |
| 0 | 96 | | get => this.inner.PrefetchCount; |
| | 97 | | set |
| | 98 | | { |
| 0 | 99 | | this.inner.PrefetchCount = value; |
| 0 | 100 | | } |
| | 101 | | } |
| | 102 | |
|
| | 103 | | public Task<IEnumerable<EventData>> ReceiveAsync(int maxEventCount, TimeSpan waitTime) |
| | 104 | | { |
| 0 | 105 | | return this.inner.ReceiveAsync(maxEventCount, waitTime); |
| | 106 | | } |
| | 107 | |
|
| | 108 | | public void SetReceiveHandler(IPartitionReceiveHandler receiveHandler, bool invokeWhenNoEvents = false) |
| | 109 | | { |
| 0 | 110 | | this.inner.SetReceiveHandler(receiveHandler, invokeWhenNoEvents); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public Task CloseAsync() |
| | 114 | | { |
| 0 | 115 | | return this.inner.CloseAsync(); |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | public int MaxBatchSize { get; set; } |
| | 119 | | } |
| | 120 | |
|
| | 121 | | internal class EventHubClientWrapper : IEventHubClient |
| | 122 | | { |
| | 123 | | private readonly EventHubClient inner; |
| | 124 | |
|
| 0 | 125 | | internal EventHubClientWrapper(EventHubClient ehc) |
| | 126 | | { |
| 0 | 127 | | this.inner = ehc; |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | public Task<EventHubRuntimeInformation> GetRuntimeInformationAsync() |
| | 131 | | { |
| 0 | 132 | | return this.inner.GetRuntimeInformationAsync(); |
| | 133 | | } |
| | 134 | |
|
| | 135 | | public IPartitionReceiver CreateEpochReceiver(string consumerGroupName, string partitionId, EventPosition ev |
| | 136 | | { |
| 0 | 137 | | return new PartitionReceiverWrapper(this.inner.CreateEpochReceiver(consumerGroupName, partitionId, event |
| | 138 | | } |
| | 139 | |
|
| | 140 | | public Task CloseAsync() |
| | 141 | | { |
| 0 | 142 | | return this.inner.CloseAsync(); |
| | 143 | | } |
| | 144 | | } |
| | 145 | |
|
| | 146 | | internal class EventHubClientFactory : IEventHubClientFactory |
| | 147 | | { |
| | 148 | | public IEventHubClient Create(string connectionString, TimeSpan receiveTimeout) |
| | 149 | | { |
| 0 | 150 | | EventHubsConnectionStringBuilder csb = new EventHubsConnectionStringBuilder(connectionString); |
| 0 | 151 | | csb.OperationTimeout = receiveTimeout; |
| 0 | 152 | | return new EventHubClientWrapper(EventHubClient.Create(csb)); |
| | 153 | | } |
| | 154 | | } |
| | 155 | | } |
| | 156 | | } |