public async Task Should_Use_Connect_Policy_When_Connecting_To_Broker() { /* Setup */ var connection = new Mock <IConnection>(); var connectionFactory = new Mock <IConnectionFactory>(); connectionFactory .Setup(f => f.CreateConnection()) .Returns(connection.Object); connectionFactory .SetupSequence(c => c.CreateConnection( It.IsAny <List <string> >(), It.IsAny <string>() )) .Throws(new BrokerUnreachableException(new Exception())) .Throws(new BrokerUnreachableException(new Exception())) .Throws(new BrokerUnreachableException(new Exception())) .Returns(connection.Object); var policy = Policy .Handle <BrokerUnreachableException>() .WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(1), TimeSpan.FromMilliseconds(2), TimeSpan.FromMilliseconds(4), TimeSpan.FromMilliseconds(8), TimeSpan.FromMilliseconds(16) }); var factory = new ChannelFactory( connectionFactory.Object, ZyRabbitConfiguration.Local, NullLogger <ChannelFactory> .Instance, new ConnectionPolicies { Connect = policy }); /* Test */ /* Assert */ await factory.ConnectAsync(); }
public static IDependencyRegister AddRawRabbit(this IDependencyRegister register, RawRabbitOptions options = null) { register .AddSingleton(options?.ClientConfiguration ?? RawRabbitConfiguration.Local) .AddSingleton <IConnectionFactory, ConnectionFactory>(provider => { var cfg = provider.GetService <RawRabbitConfiguration>(); return(new ConnectionFactory { VirtualHost = cfg.VirtualHost, UserName = cfg.Username, Password = cfg.Password, Port = cfg.Port, HostName = cfg.Hostnames.FirstOrDefault() ?? string.Empty, AutomaticRecoveryEnabled = cfg.AutomaticRecovery, TopologyRecoveryEnabled = cfg.TopologyRecovery, NetworkRecoveryInterval = cfg.RecoveryInterval, ClientProperties = provider.GetService <IClientPropertyProvider>().GetClientProperties(cfg), Ssl = cfg.Ssl }); }) .AddSingleton <IChannelPoolFactory, AutoScalingChannelPoolFactory>() .AddSingleton(resolver => AutoScalingOptions.Default) .AddSingleton <IClientPropertyProvider, ClientPropertyProvider>() .AddSingleton <ISerializer>(resolver => new Serialization.JsonSerializer(new Newtonsoft.Json.JsonSerializer { TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple, Formatting = Formatting.None, CheckAdditionalContent = true, ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }, ObjectCreationHandling = ObjectCreationHandling.Auto, DefaultValueHandling = DefaultValueHandling.Ignore, TypeNameHandling = TypeNameHandling.Auto, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, MissingMemberHandling = MissingMemberHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.Objects, NullValueHandling = NullValueHandling.Ignore })) .AddSingleton <IConsumerFactory, ConsumerFactory>() .AddSingleton <IChannelFactory>(resolver => { var channelFactory = new ChannelFactory(resolver.GetService <IConnectionFactory>(), resolver.GetService <RawRabbitConfiguration>()); channelFactory .ConnectAsync() .ConfigureAwait(false) .GetAwaiter() .GetResult(); return(channelFactory); }) .AddSingleton <ISubscriptionRepository, SubscriptionRepository>() .AddSingleton <ITopologyProvider, TopologyProvider>() .AddTransient <IPublisherConfigurationFactory, PublisherConfigurationFactory>() .AddTransient <IBasicPublishConfigurationFactory, BasicPublishConfigurationFactory>() .AddTransient <IConsumerConfigurationFactory, ConsumerConfigurationFactory>() .AddTransient <IConsumeConfigurationFactory, ConsumeConfigurationFactory>() .AddTransient <IExchangeDeclarationFactory, ExchangeDeclarationFactory>() .AddTransient <IQueueConfigurationFactory, QueueDeclarationFactory>() .AddSingleton <INamingConventions, NamingConventions>() .AddSingleton <IExclusiveLock, ExclusiveLock>() .AddSingleton <IBusClient, BusClient>() .AddSingleton <IResourceDisposer, ResourceDisposer>() .AddTransient <IInstanceFactory>(resolver => new InstanceFactory(resolver)) .AddSingleton <IPipeContextFactory, PipeContextFactory>() .AddTransient <IExtendedPipeBuilder, PipeBuilder>(resolver => new PipeBuilder(resolver)) .AddSingleton <IPipeBuilderFactory>(provider => new PipeBuilderFactory(provider)); var clientBuilder = new ClientBuilder(); options?.Plugins?.Invoke(clientBuilder); clientBuilder.DependencyInjection?.Invoke(register); register.AddSingleton(clientBuilder.PipeBuilderAction); options?.DependencyInjection?.Invoke(register); return(register); }