//----< Load and Test is responsible for testing >--------------- ILoadAndTest installLoader(AppDomain ad) { ad.Load("LoadAndTest"); //showAssemblies(ad); //Console.WriteLine(); // create proxy for LoadAndTest object in child AppDomain ObjectHandle oh = ad.CreateInstance("LoadAndTest", "TestHarness.LoadAndTest"); object ob = oh.Unwrap(); // unwrap creates proxy to ChildDomain // Console.Write("\n {0}", ob); // set reference to LoadAndTest object in child ILoadAndTest landt = (ILoadAndTest)ob; // create Callback object in parent domain and pass reference // to LoadAndTest object in child landt.setCallback(cb_); lock (sync_) { filePath_ = TLS[Thread.CurrentThread.ManagedThreadId]; landt.loadPath(filePath_); // send file path to LoadAndTest } return(landt); }
void RunTestThrdProc(Message msg) // msg= testreqeust message { List <TestInfo> testInfoList = new List <TestInfo>(); XDocument xdoc = XParse(testInfoList, msg.body); xdoc.ShowXMLBody(); AppDomain ad = ChildDomain(); ILoadAndTest ilt = installLoader(ad); // LoadAndTest proxy foreach (var tstinfomsg in testInfoList) { ilt.LoadTests(tstinfomsg); // pass testInfo msg to proxy method } ITestResults trs = cb_.GetMessage(); // cb is callback object that returns testResults trs.testKey = msg.author + "_" + trs.dateTime.ToString("yyyy-mm-dd-hh-mm-ss-ffff") + "_ThreadID" + Thread.CurrentThread.ManagedThreadId + ".txt"; Message resultMsg = MakeTestRsultMessage(trs, msg); //------ logger to Repository ------------- streamproxy.writeLog(resultMsg); // ------reply testResult msg to client --------- this.comm.sndr.PostMessage(resultMsg); testInfoList.Clear(); AppDomain.Unload(ad); }
ITestResults runTests(Message testRequest) { RequestInfo rqi = processRequestAndLoadFiles(testRequest); AppDomain ad = createChildAppDomain(); ILoadAndTest ldandtst = installLoader(ad); ITestResults tr = null; if (ldandtst != null) { //ldandtst.setCallback(cb_); tr = ldandtst.test(rqi); tr.testKey = localDir_; DLog.flush(); //DLog.pause(true); RLog.putLine(); RLog.write("\n test results are:"); RLog.write("\n - test Identifier: " + tr.testKey); RLog.write("\n - test DateTime: " + tr.dateTime); foreach (ITestResult test in tr.testResults) { RLog.write("\n --------------------------------------"); RLog.write("\n test name: " + test.testName); RLog.write("\n test result: " + test.testResult); RLog.write("\n test log: " + test.testLog); } RLog.write("\n --------------------------------------"); } RLog.putLine(); RLog.flush(); //DLog.pause(false); if (saveResultsAndLogs(tr)) { RLog.write("\n saved test results and logs in Repository - Req #5, Req #7, Req #8\n"); } else { RLog.write("\n failed to save test results and logs in Repository\n"); } DLog.putLine(); DLog.write("\n removing test directory \"" + localDir_ + "\""); try { System.IO.Directory.Delete(localDir_, true); } catch (Exception ex) { DLog.write("\n could not remove directory"); DLog.write("\n " + ex.Message); } // unloading ChildDomain, and so unloading the library DLog.write("\n unloading: \"" + ad.FriendlyName + "\"\n"); AppDomain.Unload(ad); DLog.stop(); return(tr); }
//processes the test request public Message processTestRequest(Message msg, ICommunicator repoChannel) { Console.WriteLine(" thread {0} Datetime for msgs{1} {2}", Thread.CurrentThread.ManagedThreadId, msg.author, DateTime.Now); AppDomain ad = null; Message resultMsg = new Message(); resultMsg.type = "TestResult"; resultMsg.to = msg.from; resultMsg.from = msg.to; resultMsg.author = msg.author; resultMsg.time = DateTime.Now; ILoadAndTest ldandtst = null; TestRequest tr = msg.body.FromXml<TestRequest>(); Logger objLogger = new Logger(); TestResults objTestResults = new TestResults(); if (tr != null) { string tempDirectory = ProcessAndload(tr, msg.author, repoChannel); if (tempDirectory == null) { Console.WriteLine("**********Requirement 3: one or more libraries not found in the repository ********"); resultMsg.body = "ERROR : Dll's not found in Repository"; return resultMsg; } ad = createChildAppDomain(); ldandtst = installLoader(ad, tempDirectory); if (ldandtst != null) { objTestResults = ldandtst.test(tr, objLogger, msg.author); resultMsg.body = objTestResults != null ? objTestResults.ToXml() : "no result"; } Console.WriteLine("**********Requirement7 : sending logs to repository"); //uploading log to repo uploadFileToRepo(objLogger.fileName, objLogger.fileName.Substring(objLogger.fileName.LastIndexOf("\\") + 1), repoChannel); // write results to file and upload to repo string resultFileName = tempDirectory + "\\" + msg.author + "_" + tr.TestRequestName + "_" + "TestResult" + "_" + System.Guid.NewGuid().ToString() + "_" + DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt"; writeResultsToFile(resultFileName, resultMsg, repoChannel); Console.WriteLine("**********Requirement7 : Unloading Child App domain"); AppDomain.Unload(ad); try { System.IO.Directory.Delete(tempDirectory, true); Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": removed directory " + tempDirectory); } catch (Exception ex) { Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": could not remove directory " + tempDirectory + " exception " + ex.Message); } } return resultMsg; }
//----< run tests >---------------------------------------------- /* * In Project #4 this function is run by the thread proc for * each child AppDomain thread. */ ITestResults runTests(Message testRequest) { AppDomain ad = null; ILoadAndTest ldandtst = null; RequestInfo rqi = null; ITestResults tr = null; try { lock (sync_) { rqi = processRequestAndLoadFiles(testRequest); Thread.Sleep(15000); ad = createChildAppDomain(); "Created a Child AppDomain to run the test Request - Req#4".title(); ldandtst = installLoader(ad); } if (ldandtst != null) { tr = ldandtst.test(rqi); } // unloading ChildDomain, and so unloading the library Console.WriteLine("Before Saving the log to local dir"); Thread.Sleep(1000); saveResultsAndLogs(tr, rqi.tempDirName); lock (sync_) { Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": unloading: \"" + ad.FriendlyName + "\"\n"); AppDomain.Unload(ad); try { // System.IO.Directory.Delete(rqi.tempDirName, true); Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": removed directory " + rqi.tempDirName); } catch (Exception ex) { Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": could not remove directory " + rqi.tempDirName); Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": " + ex.Message); } } return(tr); } catch (Exception ex) { Console.Write("\n\n---- {0}\n\n", ex.Message); return(tr); } }
//----< run tests >---------------------------------------------- /* * In Project #4 this function becomes the thread proc for * each child AppDomain thread. */ ITestResults runTests(Message testRequest) { AppDomain ad = null; ILoadAndTest ldandtst = null; RequestInfo rqi = null; ITestResults tr = null; try { //lock (sync_) { rqi = processRequestAndLoadFiles(testRequest); ad = createChildAppDomain(); ldandtst = installLoader(ad); } if (ldandtst != null) { tr = ldandtst.test(rqi); tr.testKey = rqi.tempDirName; } // unloading ChildDomain, and so unloading the library saveResultsAndLogs(tr); //write the test log and send the test result lock (sync_) { Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": unloading: \"" + ad.FriendlyName + "\"\n"); AppDomain.Unload(ad); try { System.IO.Directory.Delete(rqi.tempDirName, true); Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": removed directory " + rqi.tempDirName); } catch (Exception ex) { Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": could not remove directory " + rqi.tempDirName); Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": " + ex.Message); } } return(tr); } catch (Exception ex) { Console.Write("\n\n---- {0}\n\n", ex.Message); return(tr); } }
//Run tests ITestResults runTests(Messages testRequest) { AppDomain ad = null; ILoadAndTest ldandtst = null; RequestInfo rqi = null; ITestResults tr = null; try { lock (sync_) { rqi = processRequestAndLoadFiles(testRequest); ad = createChildAppDomain(); ldandtst = installLoader(ad); } if (ldandtst != null) { tr = ldandtst.test(rqi); } // unloading ChildDomain, and so unloading the library saveResultsAndLogs(tr); //lock (sync_) // this lock scope is no longer needed due to use of "thread local storage Dictionary - TLS" { Console.WriteLine("\n------------------------Requirement 7 - Line 333 TestHarness.cs--------------------"); Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": unloading: \"" + ad.FriendlyName + "\"\n"); AppDomain.Unload(ad); try { System.IO.Directory.Delete(rqi.tempDirName, true); Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": removed directory " + rqi.tempDirName); } catch (Exception ex) { Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": could not remove directory " + rqi.tempDirName); Console.Write("\n TID" + Thread.CurrentThread.ManagedThreadId + ": " + ex.Message); } } return(tr); } catch (Exception ex) { Console.Write("\n\n---- {0}\n\n", ex.Message); return(tr); } }
//----< Load and Test is responsible for testing >--------------- ILoadAndTest installLoader(AppDomain ad) { ad.Load("LoadAndTest"); ILoadAndTest landt = null; try { landt = (ILoadAndTest)ad.CreateInstanceAndUnwrap("LoadAndTest", "TestHarnessServer.AssemLoadAndTest"); landt.setCallback(cb_); } catch (Exception ex) { Console.WriteLine(ex.Message); //return new global::TestHarnessServer.AssemLoadAndTest(); } return(landt); }
ILoadAndTest installLoader(AppDomain ad, string tempDir) { ad.Load("Loader"); //showAssemblies(ad); //Console.WriteLine(); // create proxy for LoadAndTest object in child AppDomain ObjectHandle oh = ad.CreateInstance("Loader", "LoaderTH.Loader"); object ob = oh.Unwrap(); // unwrap creates proxy to ChildDomain // Console.Write("\n {0}", ob); // set reference to LoadAndTest object in child ILoadAndTest landt = (ILoadAndTest)ob; // create Callback object in parent domain and pass reference // to LoadAndTest object in child landt.setCallback(cb_); landt.loadPath(tempDir); // send file path to LoadAndTest return landt; }
static void Main(string[] args) { ILoadAndTest ldandtst = null; //RequestInfo rqi = null; AppDomainSetup domaininfo = new AppDomainSetup(); domaininfo.ApplicationBase = "file:///" + System.Environment.CurrentDirectory; // defines search path for LoadAndTest library Evidence adevidence = AppDomain.CurrentDomain.Evidence; AppDomain ad = AppDomain.CreateDomain("ChildDomain", adevidence, domaininfo); ad.Load("LoadAndTest"); ObjectHandle oh = ad.CreateInstance("LoadAndTest", "TestHarness.LoadAndTest"); object ob = oh.Unwrap(); // unwrap creates proxy to ChildDomain ldandtst = (ILoadAndTest)ob; //ITestResults tr = ldandtst.test(rqi); }