Пример #1
0
        public async Task TestGrpc()
        {
            const int BINDING_PORT = 28888;

            var logger = loggerFactory.CreateLogger(nameof(TestGrpc));

            var gossiperImpl = new GossiperImpl();

            gossiperImpl.OnPull += (_, e) => { logger.LogInformation("OnPull"); e.Rumors = Enumerable.Empty <Rumor>(); };
            gossiperImpl.OnPush += (_, e) => logger.LogInformation("Receive rumors: [{0}]", string.Join(", ", e.Rumors.Select(r => r.GetHashCode())));

            var server = new Server
            {
                Ports    = { new ServerPort("127.0.0.1", BINDING_PORT, ServerCredentials.Insecure) },
                Services = { Gossiper.BindService(gossiperImpl) }
            };

            server.Start();

            var client     = new Gossiper.GossiperClient(new Channel($"127.0.0.1:{BINDING_PORT}", ChannelCredentials.Insecure));
            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());
            }

            var pushCall = client.Push(deadline: DateTime.UtcNow.AddMilliseconds(200));
            await pushCall.RequestStream.WriteAsync(new Rumor
            {
                Counter = 1,
                Payload = new Any()
            });

            await pushCall.RequestStream.CompleteAsync();

            var pushResult = await pushCall;

            logger.LogInformation("Client push finished");

            await server.ShutdownAsync();
        }
Пример #2
0
        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);
        }