aRts audio server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.4KB

  1. /*
  2. Copyright (C) 2001 Stefan Westerfeld
  3. stefan@space.twc.de
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include "notification.h"
  18. #include "dispatcher.h"
  19. #include "test.h"
  20. using namespace Arts;
  21. using namespace std;
  22. static string messagesReceived;
  23. static long messagesAlive;
  24. static void destroyString(const Notification& n)
  25. {
  26. messagesAlive--;
  27. delete (string *)n.data;
  28. }
  29. class Receiver : public NotificationClient {
  30. private:
  31. long alive;
  32. public:
  33. Receiver() { alive = 42; }
  34. virtual ~Receiver() {
  35. NotificationManager::the()->removeClient(this);
  36. alive = 0;
  37. }
  38. void notify(const Notification& n) {
  39. if(alive != 42) messagesReceived += "<dead>";
  40. string *data = (string *)n.data;
  41. messagesReceived += *data;
  42. destroyString(n);
  43. if(alive != 42) messagesReceived += "</dead>";
  44. }
  45. };
  46. struct TestNotification : public TestCase
  47. {
  48. TESTCASE(TestNotification);
  49. Dispatcher dispatcher;
  50. void setUp() {
  51. messagesReceived = "";
  52. messagesAlive = 0;
  53. }
  54. void tearDown() {
  55. }
  56. void sendNotification(Receiver& r, string text)
  57. {
  58. messagesAlive++;
  59. Notification n;
  60. n.receiver = &r;
  61. n.data = new string(text);
  62. n.internal = 0;
  63. n.setDestroy(destroyString);
  64. NotificationManager::the()->send(n);
  65. }
  66. /* sending a notification (should be asynchronous) */
  67. TEST(send) {
  68. Receiver r1;
  69. sendNotification(r1,"r1");
  70. testEquals(1,messagesAlive);
  71. testEquals("",messagesReceived);
  72. testEquals(true, NotificationManager::the()->pending());
  73. NotificationManager::the()->run();
  74. testEquals("r1",messagesReceived);
  75. testEquals(false, NotificationManager::the()->pending());
  76. testEquals(0,messagesAlive);
  77. }
  78. /* order of sending notifications (first-in-first-out) */
  79. TEST(sendOrder) {
  80. Receiver r1,r2;
  81. sendNotification(r1,"r1");
  82. sendNotification(r2,"r2");
  83. testEquals(2,messagesAlive);
  84. testEquals("",messagesReceived);
  85. testEquals(true, NotificationManager::the()->pending());
  86. NotificationManager::the()->run();
  87. testEquals(false, NotificationManager::the()->pending());
  88. testEquals(0,messagesAlive);
  89. testEquals("r1r2",messagesReceived);
  90. }
  91. /* deletion of unprocessed notifications once a client dies */
  92. TEST(clientDeletion) {
  93. Receiver *r1 = new Receiver,
  94. *r2 = new Receiver,
  95. *r3 = new Receiver,
  96. *r4 = new Receiver;
  97. sendNotification(*r1,"r1");
  98. sendNotification(*r2,"r2");
  99. sendNotification(*r3,"r3");
  100. sendNotification(*r4,"r4");
  101. testEquals(4,messagesAlive);
  102. testEquals("",messagesReceived);
  103. delete r2;
  104. delete r4;
  105. testEquals(2,messagesAlive);
  106. testEquals("",messagesReceived);
  107. NotificationManager::the()->run();
  108. testEquals("r1r3",messagesReceived);
  109. testEquals(0,messagesAlive);
  110. }
  111. };
  112. TESTMAIN(TestNotification);