📄 test_ncbi_pipe.cpp
字号:
args.clear(); args.push_back("1"); assert(pipe.Open(app.c_str(), args, CPipe::fStdOut_Close) == eIO_Success); assert(s_ReadPipe(pipe, buf, kBufferSize, &n_read) == eIO_Unknown); assert(n_read == 0); message = "Child, are you ready?"; assert(s_WritePipe(pipe, message.c_str(), message.length(), &n_written) == eIO_Success); assert(n_written == message.length()); assert(pipe.Close(&exitcode) == eIO_Success); assert(exitcode == kTestResult); // Bidirectional pipe (direct from pipe) args.clear(); args.push_back("2"); assert(pipe.Open(app.c_str(), args) == eIO_Success); assert(s_ReadPipe(pipe, buf, kBufferSize, &n_read) == eIO_Timeout); assert(n_read == 0); message = "Child, are you ready again?"; assert(s_WritePipe(pipe, message.c_str(), message.length(), &n_written) == eIO_Success); assert(n_written == message.length()); message = "Ok. Test 2 running."; assert(s_ReadPipe(pipe, buf, kBufferSize, &n_read) == eIO_Closed); assert(n_read == message.length()); assert(memcmp(buf, message.c_str(), n_read) == 0); assert(s_ReadPipe(pipe, buf, kBufferSize, &n_read) == eIO_Closed); assert(n_read == 0); assert(pipe.Close(&exitcode) == eIO_Success); assert(exitcode == kTestResult); assert(s_ReadPipe(pipe, buf, kBufferSize, &n_read) == eIO_Closed); assert(n_read == 0); assert(s_WritePipe(pipe, buf, kBufferSize, &n_written) == eIO_Closed); assert(n_written == 0); // Bidirectional pipe (iostream) args.clear(); args.push_back("3"); CConn_PipeStream ps(app.c_str(), args, CPipe::fStdErr_Open); cout << endl; for (int i = 5; i<=10; i++) { int value; cout << "How much is " << i << "*" << i << "?\t"; ps << i << endl; ps.flush(); ps >> value; cout << value << endl; assert(ps.good()); assert(value == i*i); } ps.GetPipe().SetReadHandle(CPipe::eStdErr); ps >> str; cout << str << endl; assert(str == "Done"); assert(ps.GetPipe().Close(&exitcode) == eIO_Success); assert(exitcode == kTestResult); // f*OnClose flags test args.clear(); args.push_back("4"); assert(pipe.Open(app.c_str(), args, CPipe::fKeepOnClose) == eIO_Success); handle = pipe.GetProcessHandle(); assert(handle > 0); assert(pipe.Close(&exitcode) == eIO_Timeout); assert(exitcode == -1 ); {{ CProcess process(handle, CProcess::eHandle); assert(process.IsAlive()); assert(process.Kill()); assert(!process.IsAlive()); }} args.clear(); args.push_back("4"); assert(pipe.Open(app.c_str(), args, CPipe::fKillOnClose) == eIO_Success); handle = pipe.GetProcessHandle(); assert(handle > 0); assert(pipe.Close(&exitcode) == eIO_Success); assert(exitcode == -1 ); {{ CProcess process(handle, CProcess::eHandle); assert(!process.IsAlive()); }} assert(pipe.Open(app.c_str(), args, CPipe::fKeepOnClose) == eIO_Success); handle = pipe.GetProcessHandle(); assert(handle > 0); assert(pipe.Close(&exitcode) == eIO_Timeout); assert(exitcode == -1); {{ CProcess process(handle, CProcess::eHandle); assert(process.IsAlive()); assert(process.Wait(4000) == kTestResult); assert(!process.IsAlive()); }} // Done cout << "\nTEST execution completed successfully!\n"; return 0;}///////////////////////////////////// APPLICATION OBJECT and MAIN//int main(int argc, const char* argv[]){ string command; // Check arguments if (argc > 2) { // Invalid arguments exit(1); } if (argc == 1) { // Execute main application function return CTest().AppMain(argc, argv, 0, eDS_Default, 0); } // Internal tests int test_num = NStr::StringToInt(argv[1]); switch ( test_num ) { // Spawned process for unidirectional test case 1: { cerr << endl << "--- CPipe unidirectional test ---" << endl; command = s_ReadFile(stdin); _TRACE("read back >>" << command << "<<"); assert(command == "Child, are you ready?"); cerr << "Ok. Test 1 running." << endl; exit(kTestResult); } // Spawned process for bidirectional test (direct from pipe) case 2: { cerr << endl << "--- CPipe bidirectional test (pipe) ---" << endl; command = s_ReadFile(stdin); assert(command == "Child, are you ready again?"); s_WriteFile(stdout, "Ok. Test 2 running."); exit(kTestResult); } // Spawned process for bidirectional test (iostream) case 3: { //cerr << endl << "--- CPipe bidirectional test (iostream) ---"<<endl; for (int i = 5; i<=10; i++) { int value; cin >> value; assert(value == i); cout << value*value << endl; cout.flush(); } cerr << "Done" << endl; exit(kTestResult); } // Test for fKeepOnClose && fKillOnClose flags case 4: { SleepSec(3); exit(kTestResult); }} return -1;}/* * =========================================================================== * $Log: test_ncbi_pipe.cpp,v $ * Revision 1000.3 2004/06/01 18:46:18 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.22 * * Revision 6.22 2004/05/17 20:58:22 gorelenk * Added include of PCH ncbi_pch.hpp * * Revision 6.21 2003/12/04 16:29:22 ivanov * Added f*OnClose flags test * * Revision 6.20 2003/11/15 15:35:36 ucko * +<stdio.h> (no longer included by ncbi_pipe.hpp) * * Revision 6.19 2003/11/12 16:43:36 ivanov * Using SetReadHandle() through GetPipe() * * Revision 6.18 2003/09/30 21:00:34 lavr * Formatting * * Revision 6.17 2003/09/23 21:13:32 lavr * Adapt test for new stream and pipe API * * Revision 6.16 2003/09/10 15:54:47 ivanov * Rewritten s_ReadPipe/s_WritePipe with using I/O loops. * Removed unused Delay(). * * Revision 6.15 2003/09/09 19:42:19 ivanov * Added more checks * * Revision 6.14 2003/09/02 20:38:10 ivanov * Changes concerned moving ncbipipe to CONNECT library from CORELIB and * rewritting CPipe class using I/O timeouts. * * Revision 6.13 2003/04/23 20:57:45 ivanov * Slightly changed static read/write functions. Minor cosmetics. * * Revision 6.12 2003/03/07 16:24:44 ivanov * Simplify Delay(). Added a delays before checking exit code of a * pipe'd application. * * Revision 6.11 2003/03/06 21:19:16 ivanov * Added comment for R6.10: * Run the "dir" command for MS Windows via default command interpreter. * * Revision 6.10 2003/03/06 21:12:35 ivanov * *** empty log message *** * * Revision 6.9 2003/03/03 14:47:21 dicuccio * Remplemented CPipe using private platform specific classes. Remplemented * Win32 pipes using CreatePipe() / CreateProcess() - enabled CPipe in windows * subsystem * * Revision 6.8 2002/08/14 14:33:29 ivanov * Changed allcalls _exit() to exit() back -- non crossplatform function * * Revision 6.7 2002/08/13 14:09:48 ivanov * Changed exit() to _exit() in the child's branch of the test * * Revision 6.6 2002/06/24 21:44:36 ivanov * Fixed s_ReadFile(), c_ReadStream() * * Revision 6.5 2002/06/12 14:01:38 ivanov * Increased size of read buffer. Fixed s_ReadStream(). * * Revision 6.4 2002/06/11 19:25:56 ivanov * Added tests for CPipeIOStream * * Revision 6.3 2002/06/10 18:49:08 ivanov * Fixed argument passed to child process * * Revision 6.2 2002/06/10 18:35:29 ivanov * Changed argument's type of a running child program from char*[] * to vector<string> * * Revision 6.1 2002/06/10 17:00:30 ivanov * Initial revision * * =========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -