Ejemplo 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);
            }
        }
Ejemplo n.º 2
0
 private void BeginPreVote(ReadOnlyMemory <byte> payload, EndPoint sender, CancellationToken token)
 {
     PreVoteExchange.Parse(payload.Span, out var remotePort, out var term, out var lastLogIndex, out var lastLogTerm);
     ChangePort(ref sender, remotePort);
     task = server.ReceivePreVoteAsync(sender, term, lastLogIndex, lastLogTerm, token);
 }