public IClusterableServer CreateServer(ClusterId clusterId, IClusterClock clusterClock, EndPoint endPoint) { ServerTuple result; if (!_servers.TryGetValue(endPoint, out result)) { var description = new ServerDescription(new ServerId(clusterId, endPoint), endPoint); if (_eventSubscriber == null) { var mockServer = new Mock <IClusterableServer>() { DefaultValue = DefaultValue.Mock }; mockServer.SetupGet(s => s.Description).Returns(description); mockServer.SetupGet(s => s.EndPoint).Returns(endPoint); mockServer.SetupGet(s => s.ServerId).Returns(new ServerId(clusterId, endPoint)); result = new ServerTuple { Server = mockServer.Object }; } else { var mockMonitorFactory = new Mock <IServerMonitorFactory>(); var mockMonitor = new Mock <IServerMonitor>() { DefaultValue = DefaultValue.Mock }; mockMonitorFactory.Setup(f => f.Create(It.IsAny <ServerId>(), It.IsAny <EndPoint>())).Returns(mockMonitor.Object); mockMonitor.SetupGet(m => m.Description).Returns(description); result = new ServerTuple { Server = new Server( clusterId, clusterClock, ClusterConnectionMode.Automatic, new ServerSettings(), endPoint, (new Mock <IConnectionPoolFactory> { DefaultValue = DefaultValue.Mock }).Object, mockMonitorFactory.Object, _eventSubscriber), Monitor = mockMonitor.Object }; } _servers[endPoint] = result; } return(result.Server); }
public IClusterableServer CreateServer(ClusterId clusterId, EndPoint endPoint) { ServerTuple result; if (!_servers.TryGetValue(endPoint, out result)) { var description = new ServerDescription(new ServerId(clusterId, endPoint), endPoint); if (_eventSubscriber == null) { var server = Substitute.For <IClusterableServer>(); server.Description.Returns(description); server.EndPoint.Returns(endPoint); server.ServerId.Returns(new ServerId(clusterId, endPoint)); result = new ServerTuple { Server = server }; } else { var monitorFactory = Substitute.For <IServerMonitorFactory>(); var monitor = Substitute.For <IServerMonitor>(); monitorFactory.Create(null, null).ReturnsForAnyArgs(monitor); monitor.Description.Returns(description); result = new ServerTuple { Server = new Server( clusterId, ClusterConnectionMode.Automatic, new ServerSettings(), endPoint, Substitute.For <IConnectionPoolFactory>(), monitorFactory, _eventSubscriber), Monitor = monitor }; } _servers[endPoint] = result; } return(result.Server); }
public IClusterableServer CreateServer(ClusterId clusterId, EndPoint endPoint) { ServerTuple result; if (!_servers.TryGetValue(endPoint, out result)) { var description = new ServerDescription(new ServerId(clusterId, endPoint), endPoint); if (_eventSubscriber == null) { var server = Substitute.For<IClusterableServer>(); server.Description.Returns(description); server.EndPoint.Returns(endPoint); server.ServerId.Returns(new ServerId(clusterId, endPoint)); result = new ServerTuple { Server = server }; } else { var monitorFactory = Substitute.For<IServerMonitorFactory>(); var monitor = Substitute.For<IServerMonitor>(); monitorFactory.Create(null, null).ReturnsForAnyArgs(monitor); monitor.Description.Returns(description); result = new ServerTuple { Server = new Server( clusterId, ClusterConnectionMode.Automatic, new ServerSettings(), endPoint, Substitute.For<IConnectionPoolFactory>(), monitorFactory, _eventSubscriber), Monitor = monitor }; } _servers[endPoint] = result; } return result.Server; }
public IClusterableServer CreateServer(ClusterId clusterId, EndPoint endPoint) { ServerTuple result; if (!_servers.TryGetValue(endPoint, out result)) { var description = new ServerDescription(new ServerId(clusterId, endPoint), endPoint); if (_eventSubscriber == null) { var mockServer = new Mock<IClusterableServer>() { DefaultValue = DefaultValue.Mock }; mockServer.SetupGet(s => s.Description).Returns(description); mockServer.SetupGet(s => s.EndPoint).Returns(endPoint); mockServer.SetupGet(s => s.ServerId).Returns(new ServerId(clusterId, endPoint)); result = new ServerTuple { Server = mockServer.Object }; } else { var mockMonitorFactory = new Mock<IServerMonitorFactory>(); var mockMonitor = new Mock<IServerMonitor>() { DefaultValue = DefaultValue.Mock }; mockMonitorFactory.Setup(f => f.Create(It.IsAny<ServerId>(), It.IsAny<EndPoint>())).Returns(mockMonitor.Object); mockMonitor.SetupGet(m => m.Description).Returns(description); result = new ServerTuple { Server = new Server( clusterId, ClusterConnectionMode.Automatic, new ServerSettings(), endPoint, (new Mock<IConnectionPoolFactory> { DefaultValue = DefaultValue.Mock }).Object, mockMonitorFactory.Object, _eventSubscriber), Monitor = mockMonitor.Object }; } _servers[endPoint] = result; } return result.Server; }
public IClusterableServer CreateServer(ClusterType clusterType, ClusterId clusterId, IClusterClock clusterClock, EndPoint endPoint) { ServerTuple result; if (!_servers.TryGetValue(endPoint, out result) || result.HasBeenRemoved) { var serverId = new ServerId(clusterId, endPoint); var description = new ServerDescription(serverId, endPoint, reasonChanged: "Initial D"); if (_eventSubscriber == null) { var mockServer = new Mock <IClusterableServer>() { DefaultValue = DefaultValue.Mock }; mockServer.SetupGet(s => s.Description).Returns(description); mockServer.SetupGet(s => s.EndPoint).Returns(endPoint); mockServer.SetupGet(s => s.ServerId).Returns(new ServerId(clusterId, endPoint)); mockServer .Setup(s => s.Dispose()) .Callback( () => { _servers[mockServer.Object.EndPoint].HasBeenRemoved = true; }); result = new ServerTuple { Server = mockServer.Object }; } else { var mockMonitorFactory = new Mock <IServerMonitorFactory>(); var mockMonitor = new Mock <IServerMonitor>() { DefaultValue = DefaultValue.Mock }; mockMonitorFactory.Setup(f => f.Create(It.IsAny <ServerId>(), It.IsAny <EndPoint>())).Returns(mockMonitor.Object); mockMonitor.SetupGet(m => m.Description).Returns(description); mockMonitor .Setup(s => s.Dispose()) .Callback( () => { _servers[mockMonitor.Object.Description.EndPoint].HasBeenRemoved = true; }); var mockConnection = new Mock <IConnectionHandle>(); var mockConnectionPool = new Mock <IConnectionPool>(); var poolGeneration = 0; var connectionGeneration = 0; // need to use a func to close over connectionGeneration mockConnection.Setup(c => c.Generation).Returns(valueFunction: () => connectionGeneration); // need to use a func to close over poolGeneration mockConnectionPool.Setup(p => p.Generation).Returns(valueFunction: () => poolGeneration); Action acquireConnectionCallback = () => { connectionGeneration = poolGeneration; }; mockConnectionPool .Setup(p => p.AcquireConnection(It.IsAny <CancellationToken>())) .Callback(acquireConnectionCallback) .Returns(mockConnection.Object); mockConnectionPool .Setup(p => p.AcquireConnectionAsync(It.IsAny <CancellationToken>())) .Callback(acquireConnectionCallback) .ReturnsAsync(mockConnection.Object); mockConnectionPool.Setup(p => p.Clear()).Callback(() => { ++poolGeneration; }); var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory> { DefaultValue = DefaultValue.Mock }; mockConnectionPoolFactory .Setup(f => f.CreateConnectionPool(It.IsAny <ServerId>(), endPoint)) .Returns(mockConnectionPool.Object); result = new ServerTuple { Server = CreateServer(clusterType, mockConnectionPoolFactory.Object, mockMonitorFactory.Object), Monitor = mockMonitor.Object }; } _servers[endPoint] = result; } return(result.Server); IClusterableServer CreateServer(ClusterType clusterType, IConnectionPoolFactory connectionPoolFactory, IServerMonitorFactory serverMonitorFactory) { switch (clusterType) { case ClusterType.LoadBalanced: return(new LoadBalancedServer( clusterId, clusterClock, new ServerSettings(), endPoint, connectionPoolFactory, _eventSubscriber, serverApi: null)); default: return(new DefaultServer( clusterId, clusterClock, #pragma warning disable CS0618 // Type or member is obsolete ClusterConnectionMode.Automatic, ConnectionModeSwitch.UseConnectionMode, #pragma warning restore CS0618 // Type or member is obsolete directConnection: null, new ServerSettings(), endPoint, connectionPoolFactory, serverMonitorFactory, _eventSubscriber, serverApi: null)); } } }