Exemplo n.º 1
0
        private protected async Task RequestResponseTest(ServerFactory serverFactory, ClientFactory clientFactory)
        {
            var timeout = TimeSpan.FromSeconds(20);
            //prepare server
            var serverAddr = new IPEndPoint(IPAddress.Loopback, 3789);

            using var server = serverFactory(new LocalMember(), serverAddr, timeout);
            server.Start();
            //prepare client
            using var client = clientFactory(serverAddr);
            //Vote request
            CancellationTokenSource timeoutTokenSource;
            Result <bool>           result;

            using (timeoutTokenSource = new CancellationTokenSource(timeout))
            {
                var exchange = new VoteExchange(42L, 1L, 56L);
                client.Enqueue(exchange, timeoutTokenSource.Token);
                result = await exchange.Task;
                True(result.Value);
                Equal(43L, result.Term);
            }
            // PreVote reqyest
            using (timeoutTokenSource = new CancellationTokenSource(timeout))
            {
                var exchange = new PreVoteExchange(10L, 2L, 99L);
                client.Enqueue(exchange, timeoutTokenSource.Token);
                result = await exchange.Task;
                True(result.Value);
                Equal(44L, result.Term);
            }
            //Resign request
            using (timeoutTokenSource = new CancellationTokenSource(timeout))
            {
                var exchange = new ResignExchange();
                client.Enqueue(exchange, timeoutTokenSource.Token);
                True(await exchange.Task);
            }
            //Heartbeat request
            using (timeoutTokenSource = new CancellationTokenSource(timeout))
            {
                var exchange = new HeartbeatExchange(42L, 1L, 56L, 10L);
                client.Enqueue(exchange, timeoutTokenSource.Token);
                result = await exchange.Task;
                True(result.Value);
                Equal(43L, result.Term);
            }
        }
 private void BeginProcessHeartbeat(ReadOnlyMemory <byte> payload, EndPoint sender, CancellationToken token)
 {
     HeartbeatExchange.Parse(payload.Span, out var remotePort, out var term, out var prevLogIndex, out var prevLogTerm, out var commitIndex);
     ChangePort(ref sender, remotePort);
     task = server.ReceiveEntriesAsync(sender, term, EmptyProducer, prevLogIndex, prevLogTerm, commitIndex, token);
 }