📄 ndbportlibtest.cpp
字号:
/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//** * NdbPortLibTest.cpp * Test the functionality of portlib * TODO - Add tests for NdbMem */#include <ndb_global.h>#include "NdbOut.hpp"#include "NdbThread.h"#include "NdbMutex.h"#include "NdbCondition.h"#include "NdbSleep.h"#include "NdbTick.h"#include "NdbEnv.h"#include "NdbHost.h"#include "NdbMain.h"int TestHasFailed;int verbose = 0;static void fail(const char* test, const char* cause){ TestHasFailed = 1; ndbout << test << " failed, " << cause << endl;}// test 1 variables and funcsextern "C" void* thread1func(void* arg){ int arg1; int returnvalue = 8; arg1 = *(int*)arg; ndbout << "thread1: thread1func called with arg = " << arg1 << endl; // delay(1000); if (arg1 != 7) fail("TEST1", "Wrong arg"); return returnvalue;}// test 2 variables and funcsNdbMutex* test2mutex;extern "C" void* test2func(void* arg){ int arg1; arg1 = *(int*)arg; ndbout << "thread" << arg1 << " started in test2func" << endl; if (NdbMutex_Lock(test2mutex) != 0) fail("TEST2", "Failed to lock mutex"); ndbout << "thread" << arg1 << ", test2func " << endl; if (NdbMutex_Unlock(test2mutex) != 0) fail("TEST2", "Failed to unlock mutex"); int returnvalue = arg1; return returnvalue;}// test 3 and 7 variables and funcsNdbMutex* testmutex;NdbCondition* testcond;int testthreadsdone;extern "C" void* testfunc(void* arg){ int tmpVar; int threadno; int result; threadno = *(int*)arg; ndbout << "Thread" << threadno << " started in testfunc" << endl; do { if ((threadno % 2) == 0) result = NdbSleep_SecSleep(1); else result = NdbSleep_MilliSleep(100); if (result != 0) fail("TEST3", "Wrong result from sleep function"); if (NdbMutex_Lock(testmutex) != 0) fail("TEST3", "Wrong result from NdbMutex_Lock function"); ndbout << "thread" << threadno << ", testfunc " << endl; testthreadsdone++; tmpVar = testthreadsdone; if (NdbCondition_Signal(testcond) != 0) fail("TEST3", "Wrong result from NdbCondition_Signal function"); if (NdbMutex_Unlock(testmutex) != 0) fail("TEST3", "Wrong result from NdbMutex_Unlock function"); } while(tmpVar<100); return 0;}extern "C" void* testTryLockfunc(void* arg){ int tmpVar = 0; int threadno; int result; threadno = *(int*)arg; ndbout << "Thread" << threadno << " started" << endl; do { if ((threadno % 2) == 0) result = NdbSleep_SecSleep(1); else result = NdbSleep_MilliSleep(100); if (result != 0) fail("TEST3", "Wrong result from sleep function"); if (NdbMutex_Trylock(testmutex) == 0){ ndbout << "thread" << threadno << ", testTryLockfunc locked" << endl; testthreadsdone++; tmpVar = testthreadsdone; if (NdbCondition_Signal(testcond) != 0) fail("TEST3", "Wrong result from NdbCondition_Signal function"); if (NdbMutex_Unlock(testmutex) != 0) fail("TEST3", "Wrong result from NdbMutex_Unlock function"); } } while(tmpVar<100); return 0;}void testMicros(int count);Uint64 time_diff(Uint64 s1, Uint64 s2, Uint32 m1, Uint32 m2);NDB_COMMAND(PortLibTest, "portlibtest", "portlibtest", "Test the portable function layer", 4096){ ndbout << "= TESTING ARGUMENT PASSING ============" << endl; ndbout << "ARGC: " << argc << endl; for(int i = 1; i < argc; i++){ ndbout << " ARGV"<<i<<": " << (char*)argv[i] << endl; } ndbout << endl << endl; struct NdbThread* thread1var; void *status = 0; int arg = 7; TestHasFailed = 0; // create one thread and wait for it to return ndbout << "= TEST1 ===============================" << endl; thread1var = NdbThread_Create(thread1func, // Function (void**)&arg,// Arg 2048, // Stacksize (char*)"thread1", // Thread name NDB_THREAD_PRIO_MEAN); // Thread priority if(NdbThread_WaitFor(thread1var, &status) != 0) fail("TEST1", "NdbThread_WaitFor failed"); // NOTE! thread return value is not yet used in Ndb and thus not tested(does not work) //ndbout << "thread1 returned, status = " << status << endl; //if (status != 8) // fail("TEST1", "Wrong status"); ndbout << "TEST1 completed" << endl; NdbThread_Destroy(&thread1var); // Create 10 threads that will wait for a mutex before printing it's message to screen ndbout << "= TEST2 ===============================" << endl;#define T2_THREADS 10 NdbThread* threads[T2_THREADS]; int args[T2_THREADS]; void *status2 = 0; test2mutex = NdbMutex_Create(); NdbMutex_Lock(test2mutex); for (int i = 0; i < T2_THREADS; i++) { args[i] = i; threads[i] = NdbThread_Create(test2func, // Function (void**)&args[i],// Arg 2048, // Stacksize (char*)"test2thread", // Thread name NDB_THREAD_PRIO_MEAN); // Thread priority if (threads[i] == NULL) fail("TEST2", "NdbThread_Create failed"); } ndbout << "All threads created" << endl; NdbMutex_Unlock(test2mutex); for (int i = 0; i < T2_THREADS; i++) { if (NdbThread_WaitFor(threads[i], &status2)) fail("TEST2", "NdbThread_WaitFor failed"); NdbThread_Destroy(&threads[i]); // Don't test return values // ndbout << "thread" << i << " returned, status = " << status2 << endl; // if (status2 != i) // fail("TEST2", "Wrong status"); } if (NdbMutex_Lock(test2mutex) != 0) fail("TEST2", "NdbMutex_Lock failed"); if (NdbMutex_Unlock(test2mutex) != 0) fail("TEST2", "NdbMutex_Unlock failed"); if (NdbMutex_Destroy(test2mutex) != 0) fail("TEST2", "NdbMutex_Destroy failed"); ndbout << "TEST2 completed" << endl; ndbout << "= TEST3 ===============================" << endl; // Create 10 threads that will by synchronised by a condition // When they are awakened and have the mutex they will increment a global variable#define T3_THREADS 10 NdbThread* t3threads[T3_THREADS]; int t3args[T3_THREADS]; void *status3 = 0; testmutex = NdbMutex_Create(); testcond = NdbCondition_Create(); testthreadsdone = 0; for (int i = 0; i < T3_THREADS; i++) { t3args[i] = i; t3threads[i] = NdbThread_Create(testfunc, // Function (void**)&t3args[i],// Arg 2048, // Stacksize (char*)"test3thread", // Thread name NDB_THREAD_PRIO_MEAN); // Thread priority } ndbout << "All threads created" << endl; if (NdbMutex_Lock(testmutex) != 0) fail("TEST3", "NdbMutex_Lock failed"); while (testthreadsdone < T3_THREADS*10) { if(NdbCondition_Wait(testcond, testmutex) != 0) fail("TEST3", "NdbCondition_Wait failed"); ndbout << "Condition signaled, there are " << testthreadsdone << " completed threads" << endl; } if (NdbMutex_Unlock(testmutex) != 0) fail("TEST3", "NdbMutex_Unlock failed"); for (int i = 0; i < T3_THREADS; i++) { if (NdbThread_WaitFor(t3threads[i], &status3) != 0) fail("TEST3", "NdbThread_WaitFor failed"); NdbThread_Destroy(&t3threads[i]); //ndbout << "thread" << i << " returned, status = " << status3 << endl; //if (status3 != i) // fail("TEST3", "Wrong status"); } NdbMutex_Destroy(testmutex); NdbCondition_Destroy(testcond); ndbout << "TEST3 completed" << endl; ndbout << "= TEST4 ===============================" << endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -