public void Run() { Console.WriteLine("Running the Avro client server demo ..."); // Spawn server var responder = new SpecificResponder<HelloAvroProtocol>(new Server()); var server = new SocketServer(Hostname, 0, responder); server.Start(); Console.WriteLine("Server started on port {0}", server.Port); // Spawn client(s) var client = new Client(Hostname, server.Port); bool onGoing = true; do { client.Run(); Console.Write("Repeat client operations? (Y/N) "); var keyPress = Console.ReadKey().KeyChar; if (keyPress == 'n' || keyPress =='N') onGoing = false; } while (onGoing); // Close the server to free up the port server.Stop(); Console.WriteLine(Environment.NewLine + Helper.HorizontalLine); }
public void TestSocketTransceiverWhenServerStops() { Responder responder = new SpecificResponder<Mail>(new MailImpl()); var server = new SocketServer("localhost", 0, responder); server.Start(); var transceiver = new SocketTransceiver("localhost", server.Port); var mail = SpecificRequestor.CreateClient<Mail>(transceiver); int[] successes = {0}; int failures = 0; int[] quitOnFailure = {0}; var threads = new List<Thread>(); // Start a bunch of client threads that use the transceiver to send messages for (int i = 0; i < 100; i++) { var thread = new Thread( () => { while (true) { try { mail.send(CreateMessage()); Interlocked.Increment(ref successes[0]); } catch (Exception) { Interlocked.Increment(ref failures); if (Interlocked.Add(ref quitOnFailure[0], 0) == 1) { return; } } } }); thread.Name = "Thread" + i; threads.Add(thread); thread.Start(); } // Be sure the threads are running: wait until we get a good deal of successes while (Interlocked.Add(ref successes[0], 0) < 10000) { Thread.Sleep(50); } // Now stop the server server.Stop(); // Server is stopped: successes should not increase anymore: wait until we're in that situation while (true) { int previousSuccesses = Interlocked.Add(ref successes[0], 0); Thread.Sleep(500); if (previousSuccesses == Interlocked.Add(ref successes[0], 0)) { break; } } server.Start(); long now = CurrentTimeMillis(); int previousSuccesses2 = successes[0]; while (true) { Thread.Sleep(500); if (successes[0] > previousSuccesses2) { break; } if (CurrentTimeMillis() - now > 5000) { Console.WriteLine("FYI: requests don't continue immediately..."); break; } } // Stop our client, we would expect this to go on immediately Console.WriteLine("Stopping transceiver"); Interlocked.Add(ref quitOnFailure[0], 1); now = CurrentTimeMillis(); transceiver.Close(); // Wait for all threads to quit while (true) { threads.RemoveAll(x => !x.IsAlive); if (threads.Count > 0) Thread.Sleep(1000); else break; } if (CurrentTimeMillis() - now > 10000) { Assert.Fail("Stopping NettyTransceiver and waiting for client threads to quit took too long."); } else { Console.WriteLine("Stopping NettyTransceiver and waiting for client threads to quit took " + (CurrentTimeMillis() - now) + " ms"); } }