public void RunAsyncTest() { #if _LOG_TO_FILE using (sw = new System.IO.StreamWriter("C:\\Logs\\RunAsyncTestLog.Txt", true)) { LogFile.SetLoggingFunction(LogFn); #endif bool unicode = false; string serverAddr = "localhost:6666"; string user = "******"; string pass = string.Empty; string ws_client = "admin_space"; // turn off exceptions for this test ErrorSeverity oldExceptionLevel = P4Exception.MinThrowLevel; P4Exception.MinThrowLevel = ErrorSeverity.E_NOEXC; for (int i = 0; i < 1; i++) // run once for ascii, change < 1 to < 2 to also run once for unicode { Process p4d = Utilities.DeployP4TestServer(TestDir, unicode, TestContext.TestName); try { using (serverMT = new P4ServerMT(serverAddr, user, pass, ws_client)) { P4Server server = serverMT.getServer(); if (unicode) { Assert.IsTrue(server.UseUnicode, "Unicode server detected as not supporting Unicode"); } else { Assert.IsFalse(server.UseUnicode, "Non Unicode server detected as supporting Unicode"); } cmdCnt1 = 0; cmdCnt2 = 0; cmdCnt3 = 0; cmdCnt4 = 0; cmdCnt5 = 0; cmdCnt6 = 0; run = true; Thread t1 = new Thread(new ThreadStart(cmdThreadProc1)); t1.Name = "RunAsyncTest Thread t1"; Thread t2 = new Thread(new ThreadStart(cmdThreadProc2)); t2.Name = "RunAsyncTest Thread t2"; Thread t3 = new Thread(new ThreadStart(cmdThreadProc3)); t3.Name = "RunAsyncTest Thread t3"; Thread t4 = new Thread(new ThreadStart(cmdThreadProc4)); t4.Name = "RunAsyncTest Thread t4"; Thread t5 = new Thread(new ThreadStart(cmdThreadProc5)); t5.Name = "RunAsyncTest Thread t5"; Thread t6 = new Thread(new ThreadStart(cmdThreadProc6)); t6.Name = "RunAsyncTest Thread t6"; t1.Start(); Thread.Sleep(TimeSpan.FromSeconds(5)); // wait to start a 4th thread t2.Start(); t3.Start(); Thread.Sleep(TimeSpan.FromSeconds(5)); // run a bit run = false; //now stop running commands if (t1.Join(JoinTime) == false) { WriteLine("Thread 1 did not cleanly exit"); t1.Abort(); } if (t2.Join(JoinTime) == false) { WriteLine("Thread 2 did not cleanly exit"); t2.Abort(); } if (t3.Join(JoinTime) == false) { WriteLine("Thread 3 did not cleanly exit"); t3.Abort(); } if (runThreadExeptions.Count > 0) { // one or more run threads threw an excaption string msg = string.Empty; foreach (RunThreadException runThreadException in runThreadExeptions) { msg += string.Format("Thread {0} threw exception: {1}", runThreadException.ThreadNumber, runThreadException.ThreadException.Message); } Assert.Fail(msg); } Thread.Sleep(TimeSpan.FromSeconds(15)); // wait 15 seconds so will disconnect run = true;; t1 = new Thread(new ThreadStart(cmdThreadProc1)); t1.Name = "RunAsyncTest Thread t1b"; t2 = new Thread(new ThreadStart(cmdThreadProc2)); t2.Name = "RunAsyncTest Thread t2b"; t3 = new Thread(new ThreadStart(cmdThreadProc3)); t3.Name = "RunAsyncTest Thread t3b"; t1.Start(); t2.Start(); t3.Start(); Thread.Sleep(TimeSpan.FromSeconds(1)); // wait to start a 4th thread t4.Start(); Thread.Sleep(TimeSpan.FromSeconds(2)); // wait to start a 5th thread t5.Start(); Thread.Sleep(TimeSpan.FromSeconds(3)); // wait to start a 6th thread t6.Start(); #if SlowJoin Thread.Sleep(TimeSpan.FromMinutes(15)); // run all threads for 15 sseconds #else Thread.Sleep(TimeSpan.FromSeconds(15)); // run all threads for 15 sseconds #endif run = false; if (t1.Join(JoinTime) == false) { WriteLine("Thread 1 did not cleanly exit"); t1.Abort(); } if (t2.Join(JoinTime) == false) { WriteLine("Thread 2 did not cleanly exit"); t2.Abort(); } if (t3.Join(JoinTime) == false) { WriteLine("Thread 3 did not cleanly exit"); t3.Abort(); } if (t4.Join(JoinTime) == false) { WriteLine("Thread 4 did not cleanly exit"); t4.Abort(); } if (t5.Join(JoinTime) == false) { WriteLine("Thread 5 did not cleanly exit"); t5.Abort(); } if (t6.Join(JoinTime) == false) { WriteLine("Thread 6 did not cleanly exit"); t6.Abort(); } if (runThreadExeptions.Count > 0) { // one or more run threads threw an excaption string msg = string.Empty; foreach (RunThreadException runThreadException in runThreadExeptions) { msg += string.Format("Thread {0} threw exception: {1}\r\n{2}", runThreadException.ThreadNumber, runThreadException.ThreadException.Message, runThreadException.ThreadException.StackTrace); } Assert.Fail(msg); } } } catch (Exception ex) { Assert.Fail("Test threw an exception: {0}\r\n{1}", ex.Message, ex.StackTrace); } finally { Utilities.RemoveTestServer(p4d, TestDir); } unicode = !unicode; } // reset the exception level P4Exception.MinThrowLevel = oldExceptionLevel; #if _LOG_TO_FILE } #endif }
public void RunAsyncTest(bool unicode) { #if _LOG_TO_FILE using (sw = new System.IO.StreamWriter("C:\\Logs\\RunAsyncTestLog.Txt", true)) { LogFile.SetLoggingFunction(LogFn); #endif string serverAddr = "localhost:6666"; string user = "******"; string pass = string.Empty; string ws_client = "admin_space"; // turn off exceptions for this test ErrorSeverity oldExceptionLevel = P4Exception.MinThrowLevel; P4Exception.MinThrowLevel = ErrorSeverity.E_NOEXC; Process p4d = null; try { p4d = Utilities.DeployP4TestServer(TestDir, unicode, TestContext.TestName); using (serverMT = new P4ServerMT(serverAddr, user, pass, ws_client)) { P4Server server = serverMT.getServer(); if (unicode) { Assert.IsTrue(server.UseUnicode, "Unicode server detected as not supporting Unicode"); } else { Assert.IsFalse(server.UseUnicode, "Non Unicode server detected as supporting Unicode"); } run = true; Thread t1 = new Thread(new ThreadStart(cmdThreadProc1)); t1.Name = "RunAsyncTest Thread t1"; Thread t2 = new Thread(new ThreadStart(cmdThreadProc2)); t2.Name = "RunAsyncTest Thread t2"; Thread t3 = new Thread(new ThreadStart(cmdThreadProc3)); t3.Name = "RunAsyncTest Thread t3"; Thread t4 = new Thread(new ThreadStart(cmdThreadProc4)); t4.Name = "RunAsyncTest Thread t4"; Thread t5 = new Thread(new ThreadStart(cmdThreadProc5)); t5.Name = "RunAsyncTest Thread t5"; Thread t6 = new Thread(new ThreadStart(cmdThreadProc6)); t6.Name = "RunAsyncTest Thread t6"; t1.Start(); Thread.Sleep(TimeSpan.FromSeconds(5)); // wait to start a 4th thread t2.Start(); t3.Start(); Thread.Sleep(TimeSpan.FromSeconds(5)); // wait to start a 4th thread run = false; if (t1.Join(1000) == false) { WriteLine("Thread 1 did not cleanly exit"); t1.Abort(); } if (t2.Join(1000) == false) { WriteLine("Thread 2 did not cleanly exit"); t2.Abort(); } if (t3.Join(1000) == false) { WriteLine("Thread 3 did not cleanly exit"); t3.Abort(); } Thread.Sleep(TimeSpan.FromSeconds(15)); // wait 15 seconds so will disconnect run = true;; t1 = new Thread(new ThreadStart(cmdThreadProc1)); t1.Name = "RunAsyncTest Thread t1b"; t2 = new Thread(new ThreadStart(cmdThreadProc2)); t2.Name = "RunAsyncTest Thread t2b"; t3 = new Thread(new ThreadStart(cmdThreadProc3)); t3.Name = "RunAsyncTest Thread t3b"; t1.Start(); t2.Start(); t3.Start(); Thread.Sleep(TimeSpan.FromSeconds(1)); // wait to start a 4th thread t4.Start(); Thread.Sleep(TimeSpan.FromSeconds(2)); // wait to start a 5th thread t5.Start(); Thread.Sleep(TimeSpan.FromSeconds(3)); // wait to start a 6th thread t6.Start(); Thread.Sleep(TimeSpan.FromSeconds(15)); // run all threads for 15 seconds run = false; if (t1.Join(1000) == false) { WriteLine("Thread 1 did not cleanly exit"); t1.Abort(); } if (t2.Join(1000) == false) { WriteLine("Thread 2 did not cleanly exit"); t2.Abort(); } if (t3.Join(1000) == false) { WriteLine("Thread 3 did not cleanly exit"); t3.Abort(); } if (t4.Join(1000) == false) { WriteLine("Thread 4 did not cleanly exit"); t4.Abort(); } if (t5.Join(1000) == false) { WriteLine("Thread 5 did not cleanly exit"); t5.Abort(); } if (t6.Join(1000) == false) { WriteLine("Thread 6 did not cleanly exit"); t6.Abort(); } } } catch (Exception ex) { Assert.Fail("Test threw an exception: {0}\r\n{1}", ex.Message, ex.StackTrace); } finally { Utilities.RemoveTestServer(p4d, TestDir); } // reset the exception level P4Exception.MinThrowLevel = oldExceptionLevel; #if _LOG_TO_FILE } #endif }