]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/testsuite.cpp
Forgot to initialize critical section. somehow, the new logging code exposed this...
[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.h"
17 #include "testsuite.h"
18 #include "threadengine.h"
19 #include <iostream>
20
21 using namespace std;
22
23 class TestSuiteThread : public Thread
24 {
25  public:
26         TestSuiteThread() : Thread()
27         {
28         }
29
30         virtual ~TestSuiteThread()
31         {
32         }
33
34         virtual void Run()
35         {
36                 while (GetExitFlag() == false)
37                 {
38                         cout << "Test suite thread run...\n";
39                         sleep(5);
40                 }
41         }
42 };
43
44 TestSuite::TestSuite(InspIRCd* Instance) : ServerInstance(Instance)
45 {
46         cout << "\n\n*** STARTING TESTSUITE ***\n";
47
48         std::string modname;
49         std::string choice;
50
51         ServerInstance->SE->Blocking(fileno(stdin));
52
53         while (1)
54         {
55                 cout << "(1) Call all module OnRunTestSuite() methods\n";
56                 cout << "(2) Load a module\n";
57                 cout << "(3) Unload a module\n";
58                 cout << "(4) Threading tests\n";
59
60                 cout << endl << "(X) Exit test suite\n";
61
62                 cout << "\nChoice: ";
63                 cin >> choice;
64
65                 switch (*choice.begin())
66                 {
67                         case '1':
68                                 FOREACH_MOD(I_OnRunTestSuite, OnRunTestSuite());
69                         break;
70                         case '2':
71                                 cout << "Enter module filename to load: ";
72                                 cin >> modname;
73                                 cout << (Instance->Modules->Load(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
74                         break;
75                         case '3':
76                                 cout << "Enter module filename to unload: ";
77                                 cin >> modname;
78                                 cout << (Instance->Modules->Unload(modname.c_str()) ? "\nSUCCESS!\n" : "\nFAILURE\n");
79                         break;
80                         case '4':
81                                 cout << (DoThreadTests() ? "\nSUCCESS!\n" : "\nFAILURE\n");
82                         break;
83                         case 'X':
84                                 return;
85                         break;
86                         default:
87                                 cout << "Invalid option\n";
88                         break;
89                 }
90                 cout << endl;
91         }
92 }
93
94 bool TestSuite::DoThreadTests()
95 {
96         std::string anything;
97         ThreadEngine* te = NULL;
98
99         cout << "Creating new ThreadEngine class...\n";
100         try
101         {
102                 ThreadEngineFactory* tef = new ThreadEngineFactory();
103                 te = tef->Create(ServerInstance);
104                 delete tef;
105         }
106         catch (...)
107         {
108                 cout << "Creation failed, test failure.\n";
109                 return false;
110         }
111         cout << "Creation success, type " << te->GetName() << "\n";
112
113         cout << "Allocate: new TestSuiteThread...\n";
114         TestSuiteThread* tst = new TestSuiteThread();
115
116         cout << "ThreadEngine::Create on TestSuiteThread...\n";
117         try
118         {
119                 try
120                 {
121                         te->Create(tst);
122                 }
123                 catch (CoreException &ce)
124                 {
125                         cout << "Failure: " << ce.GetReason() << endl;
126                 }
127         }
128         catch (...)
129         {
130                 cout << "Failure, unhandled exception\n";
131         }
132
133         cout << "Type any line and press enter to end test.\n";
134         cin >> anything;
135
136         /* Thread engine auto frees thread on delete */
137         cout << "Waiting for thread to exit... " << flush;
138         delete tst;
139         cout << "Done!\n";
140
141         cout << "Delete ThreadEngine... ";
142         delete te;
143         cout << "Done!\n";
144
145         return true;
146 }
147
148 TestSuite::~TestSuite()
149 {
150         cout << "\n\n*** END OF TEST SUITE ***\n";
151 }
152