|
From: <mik...@us...> - 2003-12-24 14:10:36
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/test
In directory sc8-pr-cvs1:/tmp/cvs-serv9609/src/test
Added Files:
TestSemaphore.h TestSemaphore.cpp
Log Message:
24/12/2003 Barbeaux
* Added simple test program for Semaphores.
--- NEW FILE: TestSemaphore.h ---
/*
* This file is part of webInterface.
* Copyright (C) 2003 Mikael Barbeaux <mik...@us...>
*
* 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
*/
#ifndef _TEST_SEMAPHORE_H_
#define _TEST_SEMAPHORE_H_
#include "TestHandler.h"
#include "../thread/Semaphore.h"
#include "../thread/Thread.h"
#include "../exceptions/ThreadException.h"
#include <iostream>
using namespace std;
/**
* Defines a thread class that will be handled by the semaphore.
*/
class MyThread : public Thread {
private:
// the semaphore
Semaphore *semaphore;
// thread id
int id;
public:
/**
* Creates a test thread for the semaphore
*/
MyThread(Semaphore *sem, int nb) : Thread() {
semaphore = sem;
id = nb;
}
/**
* Destructor for MyThread
*/
~MyThread() throw (ThreadException) {}
/**
* Runs the Thread
*/
virtual void run() {
/* The thread will wait to be signaled by the tester by the semaphore
* that it can enters its processing mode. */
cout << "Thread n" << id << " says : I am waiting for the semaphore" << endl;
semaphore->wait();
cout << "Thread n" << id << "says : Thanks, Tester !";
cout << " I can process now" << endl;
for(int i=0;i<9;i++) {
cout << "Thread n" << id << " says : I am processing" << endl;
Thread::sleep(1);
}
cout << "Thread n" << id << " has finished !" << endl;
}
};
/**
* Defines a tester for threads synchronized with semaphores.
*/
class TestSemaphore : public TestHandler {
private:
// semaphore for synchronizing threads.
Semaphore *sync;
public:
/**
* Creates a TestSemaphore object.
*/
TestSemaphore();
/**
* Destructor for TestSemaphore
*/
~TestSemaphore();
/**
* Runs the tester.
*/
virtual void test();
};
#endif
--- NEW FILE: TestSemaphore.cpp ---
/*
* This file is part of webInterface.
* Copyright (C) 2003 Mikael Barbeaux <mik...@us...>
*
* 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
*/
#include "TestSemaphore.h"
/**
* Creates a TestSemaphore object.
*/
TestSemaphore::TestSemaphore() : TestHandler() {
// initialize the semaphore
sync = new Semaphore(0);
}
/**
* Destructor for TestSemaphore
*/
TestSemaphore::~TestSemaphore() {
// delete the semaphore
delete sync;
}
/**
* Runs the tester.
*/
void TestSemaphore::test() {
/** This test tries to launch two threads and to
* synchronize them with the semaphore. */
cout << endl;
cout << "ShareDaemon Web Interface - Test" << endl << endl;
cout << "Test for semaphores and threads... " << endl;
cout << "Two threads wanting to process their work wants to get " << endl;
cout << "the semaphore before continuing. The program will " <<endl;
cout << "will test synchronization between threads thanks to the" << endl;
cout << "semaphore." << endl;
cout << "Press enter to continue" << endl;
getchar();
cout << endl << endl;
try {
// Creates the threads
MyThread t1(sync, 1);
MyThread t2(sync, 2);
// Launches the threads
t1.start();
t2.start();
// Goes on working
for(int i=1;i<15;i++) {
cout << "Tester is working..." << endl;
if((i %5) == 0) {
cout << "I need help, releasing a thread !" << endl;
sync->post();
}
Thread::sleep(1);
}
cout << "Tester has finished !" << endl;
cout << "Tester says : Waiting for threads to end" << endl;
t1.wait();
t2.wait();
cout << "Tester says : Threads ended, thanks for the help !" << endl << endl;
}
catch(Exception& e) {
// display the exception message
cout << e.getMessage() << endl;
}
cout << "Did you see the threads being synchronized by the semaphore ?" << endl;
cout << " If no => VERY BAD :(" << endl;
cout << " If yes => Test worked !! :D" << endl << endl;
cout << "Press enter to continue" << endl;
getchar();
}
|