public async Task TestIntegration() { const int NODE_COUNT = 20; const int STARTING_PORT = 18888; var logger = loggerFactory.CreateLogger(nameof(TestIntegration)); logger.LogInformation("{0}({1}, {2})", nameof(TestIntegration), NODE_COUNT, STARTING_PORT); var options = Options.Create(new EpidemicBroadcastServiceOption { MinimalSleepIntervalMilliseconds = 10, RoundIntervalMilliseconds = 1000, EstimatedNodeCountMin = NODE_COUNT, FanInNodeCountMax = 1, FanOutNodeCountMax = 2, PushRoundsFactor = 1, PullRoundsFactor = 1, DeadRoundsFactor = 1 }); var stopwatchProvider = new SystemStopwatchProvider(); var services = Enumerable.Repeat <object>(null, NODE_COUNT) .Select((_, idx) => new EpidemicBroadcastService( (idx == 0 ? (ILoggerFactory)loggerFactory : NullLoggerFactory.Instance).CreateLogger <EpidemicBroadcastService>(), options, stopwatchProvider) { GossiperClientFactory = target => new Gossiper.GossiperClient(new Channel(target, ChannelCredentials.Insecure)), MemberListProvider = () => Enumerable.Range(STARTING_PORT, NODE_COUNT).Select(p => $"127.0.0.1:{p}").ToArray() }) .ToArray(); var servers = services.Select((s, i) => new Server { Ports = { new ServerPort("127.0.0.1", STARTING_PORT + i, ServerCredentials.Insecure) }, Services = { Gossiper.BindService(s.GossiperImpl) } }).ToArray(); foreach (var s in servers) { s.Start(); } services.Select <EpidemicBroadcastService, object>((s, idx) => { s.OnRumorReceived += (_, e) => logger.LogWarning( "Rumor {0} received at service {1}!", e.Payload.GetHashCode(), idx); return(null); }); await Task.WhenAll(services.Select(s => s.StartAsync())); services[0].Broadcast(new Any()); await Task.Delay(1000 * 10); await Task.WhenAll(services.Select(s => s.StopAsync())); await Task.WhenAll(servers.Select(s => s.ShutdownAsync())); }
public async Task TestObservePullService() { const int BINDING_PORT = 38888; var logger = loggerFactory.CreateLogger(nameof(TestObserveService)); var options = Options.Create(new EpidemicBroadcastServiceOption { MinimalSleepIntervalMilliseconds = 10, RoundIntervalMilliseconds = 100, EstimatedNodeCountMin = 5, FanInNodeCountMax = 1, FanOutNodeCountMax = 2, PushRoundsFactor = 1, PullRoundsFactor = 1, DeadRoundsFactor = 1 }); var stopwatchProvider = new SystemStopwatchProvider(); var service = new EpidemicBroadcastService( loggerFactory.CreateLogger <EpidemicBroadcastService>(), options, stopwatchProvider) { GossiperClientFactory = target => new Gossiper.GossiperClient(new Channel(target, ChannelCredentials.Insecure)), MemberListProvider = () => Enumerable.Range(BINDING_PORT, 5).Select(p => $"127.0.0.1:{p}").ToArray() }; var server = new Server { Ports = { new ServerPort("127.0.0.1", BINDING_PORT, ServerCredentials.Insecure) }, Services = { Gossiper.BindService(service.GossiperImpl) } }; service.OnRumorReceived += (_, e) => logger.LogInformation("Rumor {0} received.", e.Payload.GetHashCode()); server.Start(); await service.StartAsync(); service.Broadcast(new Any()); await Task.Delay(500); var client = new Gossiper.GossiperClient(new Channel($"127.0.0.1:{BINDING_PORT}", ChannelCredentials.Insecure)); using (var pushCall = client.Push(deadline: DateTime.UtcNow.AddMilliseconds(200))) { await pushCall.RequestStream.WriteAsync(new Rumor { Counter = 3, Payload = new Any() }); await pushCall.RequestStream.CompleteAsync(); var pushResult = await pushCall; logger.LogInformation("Client push finished"); } await Task.Delay(110); using (var pullResult = client.Pull(new PullRequest(), deadline: DateTime.UtcNow.AddMilliseconds(200))) { while (await pullResult.ResponseStream.MoveNext()) { logger.LogInformation("Pulled rumor {0}", pullResult.ResponseStream.Current.GetHashCode()); } logger.LogInformation("Client pull finished"); } await service.StopAsync(); await server.ShutdownAsync(); await Task.Delay(1000); }