public void SendAndReceiveAndHandleTest() { Sender.AddPeer(Receiver.Address, Receiver.Crypto.Keys.Item1); var receiver = new ReceiverListener(Receiver.Communicator.ReceiveAndHandle); //Test whether the system is able to send and receive a basic command var receiverResult = receiver.BeginInvoke(null, null); //Waste some CPU time while the thread hopefully starts... var c = 0; while (c < 500000) { c++; } Console.WriteLine(c); Assert.That(Sender.StationActive(Receiver.Address)); receiver.EndInvoke(receiverResult); receiverResult = receiver.BeginInvoke(null, null); Sender.Communicator.Send(new EndElectionCommand(Sender.Address), Receiver.Address); var result = receiverResult; Assert.Throws <TheOnlyException>(() => receiver.EndInvoke(result)); receiverResult = receiver.BeginInvoke(null, null); using (var client = new TcpClient()) { client.Connect(Receiver.Address); using (var stream = client.GetStream()) { var msg = Bytes.From(new EndElectionCommand(Sender.Address)); stream.Write(msg, 0, msg.Length); } } Assert.Throws <TheOnlyException>(() => receiver.EndInvoke(receiverResult)); //Send a command that will be wrapped in a CryptoCommand that wont be received, and that the receiver is removed from the peer-list Assert.That(Sender.Peers.ContainsKey(Receiver.Address)); Sender.Communicator.Send(new ElectNewManagerCommand(Sender.Address), Receiver.Address); Assert.That(!Sender.Peers.ContainsKey(Receiver.Address)); //Test bad endpoint Assert.That(!Sender.StationActive(BadEndPoint)); }
public void BigCommandSendAndReceiveTest() { var voters = Enumerable.Range(0, 500).Select(x => new Tuple <string, string, uint, uint>("Bob Bobbersen nummer " + x, "Køge Sportshal", (uint)x + 250000, (uint)x + 2312881234)); var crypto = Sender.Crypto; var key = crypto.VoterDataEncryptionKey; var encryptedVoters = voters.Select(v => new EncryptedVoterData( new CipherText(crypto.AsymmetricEncrypt(Bytes.From(v.Item3), key)), new CipherText(crypto.AsymmetricEncrypt(Bytes.From(v.Item4), key)), new CipherText(crypto.AsymmetricEncrypt(Bytes.From((uint)BallotStatus.NotReceived), key)))); Sender.Database.Import(encryptedVoters); using (var receiver = new Station(new TestUi(), 62347, "BigCommandSendAndReceiveTestVoters.sqlite") { Manager = Sender.Address }) { receiver.StopListening(); Sender.AddPeer(receiver.Address, receiver.Crypto.Keys.Item1); receiver.AddPeer(Sender.Address, Sender.Crypto.Keys.Item1); var receiverListener = new ReceiverListener(receiver.Communicator.ReceiveAndHandle); var receiverResult = receiverListener.BeginInvoke(null, null); Assert.That(!receiver.Database.AllData.Any()); var sync = new SyncCommand(Sender); Console.WriteLine(Bytes.From(sync).Length); Sender.Communicator.Send(sync, receiver.Address); receiverListener.EndInvoke(receiverResult); Assert.That(receiver.Database.AllData.Count() == 500); } File.Delete("BigCommandSendAndReceiveTestVoters.sqlite"); }