TestDriver.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <string>
  19. #include <cppunit/TestRunner.h>
  20. #include <cppunit/CompilerOutputter.h>
  21. #include <cppunit/TestResult.h>
  22. #include <cppunit/TestResultCollector.h>
  23. #include <cppunit/TextTestProgressListener.h>
  24. #include <cppunit/BriefTestProgressListener.h>
  25. #include <cppunit/extensions/TestFactoryRegistry.h>
  26. #include <signal.h>
  27. #include <stdlib.h>
  28. #include <stdexcept>
  29. #include <unistd.h>
  30. #include <sys/select.h>
  31. #include <cppunit/Exception.h>
  32. #include <cppunit/TestFailure.h>
  33. #include <cppunit/XmlOutputter.h>
  34. #include <cppunit/TestAssert.h>
  35. #include <fstream>
  36. #include <time.h>
  37. #include "Util.h"
  38. #include "zookeeper_log.h"
  39. using namespace std;
  40. CPPUNIT_NS_BEGIN
  41. class EclipseOutputter: public CompilerOutputter
  42. {
  43. public:
  44. EclipseOutputter(TestResultCollector *result,ostream &stream):
  45. CompilerOutputter(result,stream,"%p:%l: "),stream_(stream)
  46. {
  47. }
  48. virtual void printFailedTestName( TestFailure *failure ){}
  49. virtual void printFailureMessage( TestFailure *failure )
  50. {
  51. stream_<<": ";
  52. Message msg = failure->thrownException()->message();
  53. stream_<< msg.shortDescription();
  54. string text;
  55. for(int i=0; i<msg.detailCount();i++){
  56. text+=msg.detailAt(i);
  57. if(i+1!=msg.detailCount())
  58. text+=", ";
  59. }
  60. if(text.length()!=0)
  61. stream_ <<" ["<<text<<"]";
  62. stream_<<"\n";
  63. }
  64. ostream& stream_;
  65. };
  66. CPPUNIT_NS_END
  67. class TimingListener : public CPPUNIT_NS::BriefTestProgressListener {
  68. public:
  69. void startTest( CPPUNIT_NS::Test *test )
  70. {
  71. gettimeofday(&_start_time, NULL);
  72. CPPUNIT_NS::BriefTestProgressListener::startTest(test);
  73. }
  74. void endTest( CPPUNIT_NS::Test *test )
  75. {
  76. struct timeval end;
  77. gettimeofday(&end, NULL);
  78. long seconds = end.tv_sec - _start_time.tv_sec;
  79. long useconds = end.tv_usec - _start_time.tv_usec;
  80. long mtime = seconds * 1000 + useconds/1000.0;
  81. CPPUNIT_NS::stdCOut() << " : elapsed " << mtime;
  82. CPPUNIT_NS::BriefTestProgressListener::endTest(test);
  83. }
  84. private:
  85. struct timeval _start_time;
  86. };
  87. class ZKServer {
  88. public:
  89. ZKServer() {
  90. char cmd[1024];
  91. sprintf(cmd, "%s startClean %s", ZKSERVER_CMD, "127.0.0.1:22181");
  92. CPPUNIT_ASSERT(system(cmd) == 0);
  93. struct sigaction act;
  94. act.sa_handler = SIG_IGN;
  95. sigemptyset(&act.sa_mask);
  96. act.sa_flags = 0;
  97. CPPUNIT_ASSERT(sigaction(SIGPIPE, &act, NULL) == 0);
  98. }
  99. virtual ~ZKServer(){
  100. char cmd[1024];
  101. sprintf(cmd, "%s stop %s", ZKSERVER_CMD, "127.0.0.1:22181");
  102. CPPUNIT_ASSERT(system(cmd) == 0);
  103. }
  104. };
  105. int main( int argc, char* argv[] ) {
  106. // if command line contains "-ide" then this is the post build check
  107. // => the output must be in the compiler error format.
  108. //bool selfTest = (argc > 1) && (std::string("-ide") == argv[1]);
  109. globalTestConfig.addConfigFromCmdLine(argc,argv);
  110. ZKServer zkserver;
  111. // Create the event manager and test controller
  112. CPPUNIT_NS::TestResult controller;
  113. // Add a listener that colllects test result
  114. CPPUNIT_NS::TestResultCollector result;
  115. controller.addListener( &result );
  116. // A listener that print dots as tests run.
  117. // CPPUNIT_NS::TextTestProgressListener progress;
  118. // CPPUNIT_NS::BriefTestProgressListener progress;
  119. // brief + elapsed time
  120. TimingListener progress;
  121. controller.addListener( &progress );
  122. CPPUNIT_NS::TestRunner runner;
  123. runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
  124. try {
  125. CPPUNIT_NS::stdCOut() << endl << "Running " << endl;
  126. zoo_set_debug_level(ZOO_LOG_LEVEL_INFO);
  127. //zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
  128. runner.run( controller, globalTestConfig.getTestName());
  129. // Print test in a compiler compatible format.
  130. CPPUNIT_NS::EclipseOutputter outputter( &result,cout);
  131. outputter.write();
  132. // Uncomment this for XML output
  133. #ifdef ENABLE_XML_OUTPUT
  134. std::ofstream file( "tests.xml" );
  135. CPPUNIT_NS::XmlOutputter xml( &result, file );
  136. xml.setStyleSheet( "report.xsl" );
  137. xml.write();
  138. file.close();
  139. #endif
  140. } catch ( std::invalid_argument &e ) {
  141. // Test path not resolved
  142. cout<<"\nERROR: "<<e.what()<<endl;
  143. return 0;
  144. }
  145. return result.wasSuccessful() ? 0 : 1;
  146. }