【发布时间】:2020-08-28 22:33:33
【问题描述】:
背景资料
我正在编写一个生成子进程(c# 控制台应用程序)的集成测试。测试在进程启动和进程关闭后计算数据库中的一些行。该进程通过 process.Kill() 关闭
当进程以这种方式被杀死时,它不会在进程中点击 Stop 方法。我需要调用此停止方法来停止线程并从数据库中删除条目以使测试通过。
原始码
我在测试中生成的控制台应用程序进程:static void Main(string[] args)
{
TaskManager tm = new TaskManagerProcess();
if (Environment.UserInteractive ||
(args.EmptyForNull().Any(a => a.Equals("-RunInteractive", StringComparison.OrdinalIgnoreCase) || a.Equals("/RunInteractive"))))
{
tm.ConsoleStart(args);
Console.WriteLine("Press [Enter] to shut down, any other key to mark");
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
break;
Console.WriteLine("========================================================");
Console.Out.Flush();
}
Console.WriteLine("Shutting down...");
tm.ConsoleStop();
}
else
{
ServiceBase.Run(tm);
}
}
}
测试代码:
//count before starting child proc
int preCount;
//count after process is spun up
int runningsCount;
//count after stopped
int postCount;
//Get an initial count of the logged in modules before svc host is started
user = ApiMethod.GetLoggedInUsers().Where(x => x.RecId == userRecID).FirstOrDefault();
preCount = user.LoggedInModules.Count;
Process proc = Helper.StartProcess(ConnectionBundle);
//Give process time to spin up leaders and workers
await Task.Delay(TimeSpan.FromSeconds(30));
//Get a count of modules after process is spun up
user = ApiMethod.GetLoggedInUsers().Where(x => x.RecId == userRecID).FirstOrDefault();
runningCount = user.LoggedInModules.Count;
//Write a line terminator to the child svc host process -
//this allows it to shutdown normally
Helper.ProcessInput.WriteLine();
Helper.ProcessInput.Close();
Helper.KillProcess(proc);
await Task.Delay(TimeSpan.FromSeconds(5));
//Get count of logged in modules after process is closed
user = ApiMethod.GetLoggedInUsers().Where(x => x.RecId == userRecID).FirstOrDefault();
postCount = user.LoggedInModules.Count;
Helper 是一个静态类,用于设置进程启动信息(包括 args)并启动进程。在助手中,我重定向了 StandardInput 并添加了一个属性 ProcessInput,该属性设置为已创建进程的 StandardInput。
我的目标是将“Enter”的输入从测试发送到生成的进程,以便它从循环中中断并调用 tm.ConsoleStop()
TaskManagerProcess 是控制流程的私有自定义类。它不继承自 System.Diagnostics.Process。作为一种替代方法,我的测试可以直接与 TaskManagerProcess 交互。但是,我不能公开 TaskManagerProcess,我需要在它自己的 AppDomain 中运行 TaskManagerProcess,因为调用 ConsoleStop 会在 API 中处理我需要完成测试的对象。
我尝试过的事情
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(CloseProcDelgate handler, bool add);
我尝试添加对 Kernel32.SetConsoleCtrlHandler (和必要的委托)的调用以在进程退出时调用 ConsoleStop。当通过 process.Kill() 杀死进程时,这似乎不起作用
使用原始流程代码,我在写到 StandardInput 时注意到了一个异常。异常消息告诉我使用 Console.Read 而不是 Console.ReadKey()。这实际上是间歇性的!我有时可以在 int cKey = Console.Read() 上获得一个断点(调试器附加到子进程),但其他时候它没有达到断点。
while (true)
{
//Changing this to Console.Read instead of Console.ReadKey
//Allows us to send redirected input to process?
int cKey = Console.Read();
if ((ConsoleKey)cKey == ConsoleKey.Enter)
break;
Console.WriteLine("========================================================");
Console.Out.Flush();
}
最后,我尝试直接与 TaskManagerProcess 交互。我将私有类设为内部,并将内部标记为对我的测试程序集可见。我不能公开课程。
当我走这条路时,调用 tm.ConsoleStop() 会吹走我的 API 中的一些对象,因此在调用此方法后我无法检查计数。出于这个原因,我想我会创建一个新的 AppDomain 并在 TaskManagerProcess 类上调用 AppDomain.CreateInstanceAndUnwrap()。但是,我在这里遇到了一个例外,我相信这是因为该类是内部的。
我真的被困在这一点上!感谢您提供任何帮助,并感谢您花时间阅读本文!
编辑
我创建了一个演示项目here 这显示了我正在尝试做的事情,并且在测试方法中有两种方法。
最初我以为我不能调用 AppDomain.CreateInstanceAndUnwrap() 因为 TaskManagerProcess 类是内部的。但是,在玩了我的演示项目之后,我想我就是无法加载程序集。
【问题讨论】:
-
您是否编写了这个其他应用程序?如果是,只需设置 IPC...无论如何我的感觉是您不应该为另一个应用程序编写测试,您应该测试其中的方法
-
嗨,Michael,我的公司编写了另一个应用程序。修复错误后,我正在为应用程序编写测试。我会研究IPC,谢谢
-
您应该为应用程序中的方法编写测试,而不是为控制台应用程序编写测试