]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
7f8a38d54eb3981226c6f60e0f4e4e212525c7ba
[user/henk/code/inspircd.git] / src / testsuite.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDtestsuite */
15
16 #include "inspircd_config.h"
17 #include "inspircd.h"
18 #include "testsuite.h"
19 #include "threadengine.h"
20 #include <iostream>
21
22 using namespace std;
23
24 class TestSuiteThread : public Thread
25 {
26  public:
27         TestSuiteThread() : Thread()
28         {
29         }
30
31         virtual ~TestSuiteThread()
32         {
33         }
34
35         virtual void Run()
36         {
37                 while (GetExitFlag() == false)
38                 {
39                         cout << "Test suite thread run...\n";
40                         sleep(5);
41                 }
42         }
43 };
44
45 TestSuite::TestSuite(InspIRCd* Instance) : ServerInstance(Instance)
46 {
47         cout << "\n\n*** STARTING TESTSUITE ***\n";
48
49         std::string modname;
50         std::string choice;
51
52         ServerInstance->SE->Blocking(fileno(stdin));
53
54         while (1)
55         {
56                 cout << "(1) Call all module OnRunTestSuite() methods\n";
57                 cout << "(2) Load a module\n";
58                 cout << "(3) Unload a module\n";
59                 cout << "(4) Threading tests\n";
60
61                 cout << endl << "(X) Exit test suite\n";
62
63                 cout << "\nChoice: ";
64                 cin >> choice;
65
66                 switch (*choice.begin())
67                 {
68                         case '1':
69                                 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
70                         break;
71                         case '2':
72                                 cout << "Enter module filename to load: ";
73                                 cin >> modname;
74                                 cout << (Instance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
75                         break;
76                         case '3':
77                                 cout << "Enter module filename to unload: ";
78                                 cin >> modname;
79                                 cout << (Instance->Modules->Unload(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
80                         break;
81                         case '4':
82                                 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
83                         break;
84                         case 'X':
85                                 return;
86                         break;
87                         default:
88                                 cout << "Invalid option\n";
89                         break;
90                 }
91                 cout << endl;
92         }
93 }
94
95 bool TestSuite::DoThreadTests()
96 {
97         std::string anything;
98         ThreadEngine* te = NULL;
99
100         cout << "Creating new ThreadEngine class...\n";
101         try
102         {
103                 ThreadEngineFactory* tef = new ThreadEngineFactory();
104                 te = tef->Create(ServerInstance);
105                 delete tef;
106         }
107         catch (...)
108         {
109                 cout << "Creation failed, test failure.\n";
110                 return false;
111         }
112         cout << "Creation success, type " << te->GetName() << "\n";
113
114         cout << "Allocate: new TestSuiteThread...\n";
115         TestSuiteThread* tst = new TestSuiteThread();
116
117         cout << "ThreadEngine::Create on TestSuiteThread...\n";
118         te->Create(tst);
119
120         cout << "Type any line and press enter to end test.\n";
121         cin >> anything;
122
123         /* Thread engine auto frees thread on delete */
124         cout << "Waiting for thread to exit... " << flush;
125         delete tst;
126         cout << "Done!\n";
127
128         cout << "Delete ThreadEngine... ";
129         delete te;
130         cout << "Done!\n";
131
132         return true;
133 }
134
135 TestSuite::~TestSuite()
136 {
137         cout << "\n\n*** END OF TEST SUITE ***\n";
138 }
139