xref: /haiku/src/tests/kits/app/bmessagequeue/MessageQueueTestCase.h (revision 571d840abfdf03de583b26fefd2066ee75b25cf4)
1 /*
2 	$Id: MessageQueueTestCase.h 383 2002-07-22 09:28:00Z tylerdauwalder $
3 
4 	This file defines a set of classes for testing BMessageQueue
5 	functionality.
6 
7 	*/
8 
9 
10 #ifndef MessageQueueTestCase_H
11 #define MessageQueueTestCase_H
12 
13 
14 #include <List.h>
15 #include <Locker.h>
16 #include <Message.h>
17 #include <MessageQueue.h>
18 #include "ThreadedTestCase.h"
19 
20 
21 //
22 // The SafetyLock class is a utility class for use in actual tests
23 // of the BMessageQueue interfaces.  It is used to make sure that if the
24 // test fails and an exception is thrown with the lock held on the message
25 // queue, that lock will be released.  Without this SafetyLock, there
26 // could be deadlocks if one thread in a test has a failure while holding
27 // the lock.  It should be used like so:
28 //
29 //  void myTestClass::myTestFunc(void)
30 // {
31 //   SafetyLock mySafetyLock(theMessageQueue);
32 //   ...perform tests without worrying about holding the lock on assert...
33 //
34 
35  class SafetyLock {
36 private:
37 	BMessageQueue *theMessageQueue;
38 
39 public:
SafetyLock(BMessageQueue * aMessageQueue)40 	SafetyLock(BMessageQueue *aMessageQueue) {theMessageQueue = aMessageQueue;}
~SafetyLock()41 	virtual ~SafetyLock() {if (theMessageQueue != NULL) theMessageQueue->Unlock(); };
42 	};
43 
44 
45 //
46 // The testMessageClass is used to count the number of messages that are
47 // deleted.  This is used to check that all messages on the message queue
48 // were properly free'ed.
49 //
50 
51 class testMessageClass : public BMessage
52 {
53 public:
54 	static int messageDestructorCount;
~testMessageClass()55 	virtual ~testMessageClass() { messageDestructorCount++; };
testMessageClass(int what)56 	testMessageClass(int what) : BMessage(what) { };
57 };
58 
59 
60  class MessageQueueTestCase : public BThreadedTestCase {
61 
62 private:
63 	BList messageList;
64 
65 protected:
66 	BMessageQueue *theMessageQueue;
67 
68 	void AddMessage(BMessage *message);
69 	void RemoveMessage(BMessage *message);
70 	BMessage *NextMessage(void);
71 	BMessage *FindMessage(uint32 what, int index);
72 
73 	void CheckQueueAgainstList(void);
74 
75 public:
76 	MessageQueueTestCase(std::string);
77 	virtual ~MessageQueueTestCase();
78 	};
79 
80 #endif
81 
82 
83 
84