public void TestProcess() { var configSource = StartBootstrap(DefaultServerConfig); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { string welcomeString = reader.ReadLine(); Console.WriteLine("Welcome: " + welcomeString); writer.WriteLine("PROC"); writer.Flush(); var line = reader.ReadLine(); Assert.AreEqual("SuperSocket.Agent", line); } } }
public void TestListenersConfig() { var configSource = StartBootstrap("Listeners.config"); var serverConfig = configSource.Servers.FirstOrDefault(); foreach (var listener in serverConfig.Listeners) { EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), listener.Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); for (var i = 0; i < 5; i++) { var message = Guid.NewGuid().ToString(); writer.WriteLine("ECHO {0}", message); writer.Flush(); Assert.AreEqual(message, reader.ReadLine()); } } } } }
public void TestProcess() { var configSource = StartBootstrap(DefaultServerConfig); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { string welcomeString = reader.ReadLine(); Console.WriteLine("Welcome: " + welcomeString); writer.WriteLine("PROC"); writer.Flush(); var line = reader.ReadLine(); var pars = line.Split(','); var appDomainName = pars[0]; var appDomainRoot = pars[1]; Assert.AreEqual("SuperSocket.Agent.exe", appDomainName); Assert.AreEqual(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppRoot", serverConfig.Name), appDomainRoot); } } }
public void TestBrokenCommandBlock() { var configSource = StartBootstrap(DefaultServerConfig); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { string welcomeString = reader.ReadLine(); Console.WriteLine("Welcome: " + welcomeString); char[] chars = new char[] { 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H' }; Random rd = new Random(1); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 50; i++) { sb.Append(chars[rd.Next(0, chars.Length - 1)]); } string command = sb.ToString(); var commandSource = ("ECHO " + command).ToList(); while (commandSource.Count > 0) { int readLen = rd.Next(1, commandSource.Count); writer.Write(commandSource.Take(readLen).ToArray()); Console.WriteLine(commandSource.Take(readLen).ToArray()); writer.Flush(); commandSource.RemoveRange(0, readLen); Thread.Sleep(200); } writer.WriteLine(); writer.Flush(); string echoMessage = reader.ReadLine(); Console.WriteLine("C:" + echoMessage); Assert.AreEqual(command, echoMessage); } } }
public void TestTerminatorRequestFilter() { var appServer = new TestServer(); using (appServer as IDisposable) { var setupResult = appServer.Setup("127.0.0.1", 2012, null, new TerminatorReceiveFilterFactory("##", m_Encoding), new ConsoleLogFactory(), null, null); Assert.IsTrue(setupResult); Assert.IsTrue(appServer.Start()); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2012); using (var socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); var socketStream = new NetworkStream(socket); using (var reader = new StreamReader(socketStream, m_Encoding, true)) using (var writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { var welcomeString = reader.ReadLine(); Assert.AreEqual(string.Format(TestSession.WelcomeMessageFormat, appServer.Name), welcomeString); var chars = new char[] { 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H' }; var rd = new Random(1); var sb = new StringBuilder(); for (var i = 0; i < 100; i++) { sb.Append(chars[rd.Next(0, chars.Length - 1)]); var command = sb.ToString(); writer.Write("ECHO " + command); writer.Write("##"); writer.Flush(); var echoMessage = reader.ReadLine(); Console.WriteLine("C:" + echoMessage); Assert.AreEqual(command, echoMessage); } } } } }
public void TestCommands() { var configSource = StartBootstrap("DLR.config"); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); Random rd = new Random(); for (int i = 0; i < 10; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "ADD", x, y); Console.WriteLine(command); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Console.WriteLine(line); Assert.AreEqual(x + y, int.Parse(line)); } for (int i = 0; i < 10; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "MULT", x, y); Console.WriteLine(command); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Console.WriteLine(line); Assert.AreEqual(x * y, int.Parse(line)); } } } }
private void ValidateCulture(int port, string culture) { EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); writer.WriteLine("CULT"); writer.Flush(); var realCult = reader.ReadLine(); Console.WriteLine(realCult); Assert.AreEqual(culture, realCult); } } }
public void TestAppDomainConfig() { var configSource = StartBootstrap("AppDomain.config"); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); writer.WriteLine("DOMAIN"); writer.Flush(); Assert.AreEqual(serverConfig.Name, reader.ReadLine()); } } }
void TestAddServerInRuntimeImplement() { StartBootstrap(DefaultServerConfig); var bootstrap = BootStrap as IDynamicBootstrap; var options = new NameValueCollection(); options["testAtt1"] = "1"; options["testAtt2"] = "2"; Assert.IsTrue(bootstrap.AddAndStart(new ServerConfig { Name = "TestDynamicServer", ServerType = "SuperSocket.Test.TestServer, SuperSocket.Test", Port = 2013, Options = options })); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2013); using (Socket socket = CreateClientSocket()) { try { socket.Connect(serverAddress); } catch { Assert.Fail("Failed to connect to the dynamic created server."); } Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { string welcomeString = reader.ReadLine(); Assert.AreEqual(string.Format(TestSession.WelcomeMessageFormat, "TestDynamicServer"), welcomeString); var line = Guid.NewGuid().ToString(); writer.WriteLine("ECHO " + line); writer.Flush(); Assert.AreEqual(line, reader.ReadLine()); } } BootStrap.GetServerByName("TestDynamicServer").Stop(); bootstrap.Remove("TestDynamicServer"); }
private void SendRequestC(ConsoleWriter writer, string line) { writer.Write("ECHO " + line.Substring(0, line.Length - 1)); writer.Flush(); Thread.Sleep(100); writer.Write(line.Substring(line.Length - 1) + "#"); writer.Flush(); Thread.Sleep(100); writer.Write("#"); writer.Flush(); }
private void SendRequestB(ConsoleWriter writer, string line) { writer.Write("ECHO " + line); writer.Flush(); Thread.Sleep(100); writer.Write("#"); writer.Flush(); Thread.Sleep(100); writer.Write("#"); writer.Flush(); }
public void TestSessionConnectedState() { byte[] data = new byte[] { 0xff, 0xff, 0xff, 0xf0 }; Console.WriteLine(BitConverter.ToInt32(data, 0)); var configSource = SetupBootstrap(DefaultServerConfig); if (configSource.Isolation != IsolationMode.None) return; var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); BootStrap.Start(); var server = BootStrap.AppServers.FirstOrDefault() as IAppServer; using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); writer.WriteLine("SESS"); writer.Flush(); var sessionID = reader.ReadLine(); var session = server.GetSessionByID(sessionID); if (session == null) Assert.Fail("Failed to get session by sessionID"); Assert.IsTrue(session.Connected); try { socket.Shutdown(SocketShutdown.Both); } catch { } finally { try { socket.Close(); } catch { } } while (true) { Thread.Sleep(5000); if (!session.Connected) break; } } } }
public void TestFastSending() { var configSource = StartBootstrap(DefaultServerConfig, c => new ServerConfig(c) { LogBasicSessionActivity = false, SendingQueueSize = 100 }); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); string sendLine = string.Empty; int i = 0; int testRound = 5000; while (i < testRound) { sendLine = Guid.NewGuid().ToString(); writer.Write("ECHO " + sendLine + "\r\n"); writer.Flush(); var line = reader.ReadLine(); //Console.WriteLine("C: {0}, S: {1}", sendLine, line); if (line == null) { //CustomThreadPoolTaskScheduler.Reset(); } Assert.AreEqual(sendLine, line); i++; } Console.WriteLine("Client sent: {0}", i); Console.WriteLine("Server sent: {0}", ECHO.Sent); ECHO.Reset(); Assert.AreEqual(testRound, i); } } }
public void TestUnknownCommand() { var configSource = StartBootstrap(DefaultServerConfig); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); for (int i = 0; i < 10; i++) { string commandName = Guid.NewGuid().ToString().Substring(i, 3); if(commandName.Equals("325")) continue; string command = commandName + " " + DateTime.Now; writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Console.WriteLine(line); Assert.AreEqual(string.Format(TestSession.UnknownCommandMessageFormat, commandName), line); } } } }
public void TestCommandParser() { var configSource = StartBootstrap(DefaultServerConfig, c => new ServerConfig(c) { ServerType = "SuperSocket.Test.TestServerWithCustomRequestFilter, SuperSocket.Test" }); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); string command = string.Format("Hello World ({0})!", Guid.NewGuid().ToString()); writer.WriteLine("ECHO:" + command); writer.Flush(); string echoMessage = reader.ReadLine(); Assert.AreEqual(command, echoMessage); } } }
public void TestCloseFromServer() { var configSource = StartBootstrap(DefaultServerConfig, c => new ServerConfig(c) { LogBasicSessionActivity = false, ClearIdleSession = false, MaxConnectionNumber = 1000, }); var n = 100; var sockets = new List<Socket>(n); var streams = new List<Stream>(n); var server = BootStrap.AppServers.FirstOrDefault(); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); for (var i = 0; i < n; i++) { Socket socket = CreateClientSocket(); socket.Connect(serverAddress); sockets.Add(socket); streams.Add(GetSocketStream(socket)); } Console.WriteLine(sockets.Count); Thread.Sleep(500); Assert.AreEqual(n, server.SessionCount); for (var i = n - 1; i >= 0; i--) { Stream socketStream = streams[i]; using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { writer.WriteLine("CLOSE"); writer.Flush(); } Thread.Sleep(100); if (i != server.SessionCount) { Thread.Sleep(500); Assert.AreEqual(i, server.SessionCount); } Console.WriteLine("Conn: {0}", i); } }
public virtual void TestClearTimeoutSession() { var configSource = StartBootstrap(DefaultServerConfig, c => new ServerConfig(c) { ClearIdleSession = true }); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); var server = BootStrap.AppServers.FirstOrDefault(); Socket socket = CreateClientSocket(); socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); } Assert.AreEqual(1, server.SessionCount); Thread.Sleep(2000); Assert.AreEqual(1, server.SessionCount); Thread.Sleep(5000); Assert.AreEqual(0, server.SessionCount); }
public void TestProcessIsolationConfig() { var configSource = StartBootstrap("ProcessIsolation.config"); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); writer.WriteLine("PROC"); writer.Flush(); var line = reader.ReadLine(); var pars = line.Split(','); var appDomainName = pars[0]; Assert.AreEqual("SuperSocket.Agent.exe", appDomainName); } } }
private bool RunEchoMessage(int port, int runIndex) { EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); Socket socket = CreateClientSocket(); socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { string welcomeString = reader.ReadLine(); Console.WriteLine(welcomeString); char[] chars = new char[] { 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H' }; Random rd = new Random(1); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(chars[rd.Next(0, chars.Length - 1)]); string command = sb.ToString(); writer.WriteLine("ECHO " + command); writer.Flush(); string echoMessage = reader.ReadLine(); if (!string.Equals(command, echoMessage)) { Console.WriteLine("{0} : {1}", command, echoMessage); return false; } Thread.Sleep(50); } } return true; }
public void TestCommandCombining() { var configSource = StartBootstrap(DefaultServerConfig); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { string welcomeString = reader.ReadLine(); Console.WriteLine("Welcome: " + welcomeString); char[] chars = new char[] { 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H' }; Random rd = new Random(1); for (int j = 0; j < 10; j++) { StringBuilder sb = new StringBuilder(); List<string> source = new List<string>(5); for (int i = 0; i < 5; i++) { sb.Append(chars[rd.Next(0, chars.Length - 1)]); string command = sb.ToString(); source.Add(command); writer.WriteLine("ECHO " + command); } writer.Flush(); for (int i = 0; i < 5; i++) { string line = reader.ReadLine(); Assert.AreEqual(source[i], line); } } } } }
public void TestSendBeforeClose() { var configSource = StartBootstrap(DefaultServerConfig); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); var line = Guid.NewGuid().ToString(); writer.WriteLine("CLOSE " + line); writer.Flush(); string echoMessage = reader.ReadLine(); Console.WriteLine("C:" + echoMessage); Assert.AreEqual(line, echoMessage); } } }
public void TestTerminatorRequestFilterA() { var appServer = new TestServer(); using (appServer as IDisposable) { var setupResult = appServer.Setup("127.0.0.1", 2012, null, new TerminatorReceiveFilterFactory("##", m_Encoding), new ConsoleLogFactory(), null, null); Assert.IsTrue(setupResult); Assert.IsTrue(appServer.Start()); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2012); using (var socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); var socketStream = new NetworkStream(socket); using (var reader = new StreamReader(socketStream, m_Encoding, true)) using (var writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { var welcomeString = reader.ReadLine(); Assert.AreEqual(string.Format(TestSession.WelcomeMessageFormat, appServer.Name), welcomeString); var actions = new Action<ConsoleWriter, string>[] { (w, r) => SendRequestA(w, r), (w, r) => SendRequestB(w, r), (w, r) => SendRequestC(w, r) }; var rd = new Random(); for(var i = 0; i < 50; i++) { var command = Guid.NewGuid().ToString(); var act = actions[rd.Next(0, 100) % actions.Length]; act(writer, command); var echoMessage = reader.ReadLine(); Console.WriteLine("C:" + echoMessage); Assert.AreEqual(command, echoMessage); } } } } }
[Test]//[Test, Repeat(5)] public void TestConcurrentSending() { var configSource = StartBootstrap(DefaultServerConfig, c => new ServerConfig(c) { SendingQueueSize = 100, ClearIdleSession = false }); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); string[] source = SEND.GetStringSource(); string[] received = new string[source.Length]; using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); for (var j = 0; j < 1000; j++) { writer.WriteLine("SEND"); writer.Flush(); Console.WriteLine (j); int i; for (i = 0; i < received.Length; i++) { var line = reader.ReadLine(); if (line == null) break; //Console.WriteLine (i); received[i] = line; } Assert.AreEqual(received.Length, i); Console.WriteLine("ROUND"); } } } if (received.Distinct().Count() != received.Length) Assert.Fail("Duplicated record!"); var dict = source.ToDictionary(i => i); for (var i = 0; i < received.Length; i++) { if (!dict.Remove(received[i])) Assert.Fail(received[i]); } if (dict.Count > 0) Assert.Fail(); }
private void TestBasicConfig(string configFile) { var configSource = SetupBootstrap(configFile); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), configSource.Servers.FirstOrDefault().Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); for (var i = 0; i < 10; i++) { var message = Guid.NewGuid().ToString(); writer.WriteLine("ECHO {0}", message); writer.Flush(); Assert.AreEqual(message, reader.ReadLine()); } } } }
public void TestWelcomeMessage() { var configSource = StartBootstrap(DefaultServerConfig); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { string welcomeString = reader.ReadLine(); Assert.AreEqual(string.Format(TestSession.WelcomeMessageFormat, serverConfig.Name), welcomeString); } } }
public void TestCustomCommandName() { var configSource = StartBootstrap(DefaultServerConfig); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = CreateClientSocket()) { socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); string param = Guid.NewGuid().ToString(); writer.WriteLine("325 " + param); writer.Flush(); string echoMessage = reader.ReadLine(); Console.WriteLine("C:" + echoMessage); Assert.AreEqual(string.Format(SuperSocket.Test.Command.NUM.ReplyFormat, param), echoMessage); } } }
public void TestPerformance() { var configSource = StartBootstrap("DLR.config"); var serverConfig = configSource.Servers.FirstOrDefault(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), serverConfig.Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (ConsoleWriter writer = new ConsoleWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); Random rd = new Random(); for (int i = 0; i < 10; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "ADD", x, y); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Assert.AreEqual(x + y, int.Parse(line)); } for (int i = 0; i < 10; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "MULT", x, y); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Assert.AreEqual(x * y, int.Parse(line)); } var watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 500; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "ADD", x, y); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Assert.AreEqual(x + y, int.Parse(line)); } for (int i = 0; i < 500; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "MULT", x, y); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Assert.AreEqual(x * y, int.Parse(line)); } watch.Stop(); var dynamicTime = watch.ElapsedMilliseconds; watch.Reset(); watch.Start(); for (int i = 0; i < 500; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "ADDCS", x, y); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Assert.AreEqual(x + y, int.Parse(line)); } for (int i = 0; i < 500; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "MULTCS", x, y); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Assert.AreEqual(x * y, int.Parse(line)); } watch.Stop(); var staticTime = watch.ElapsedMilliseconds; Console.WriteLine("{0}/{1} = {2}", dynamicTime, staticTime, ((decimal)dynamicTime / (decimal)staticTime).ToString("0.0")); } } }