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.

297 lines
8.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/asyncudpsocket.h"
  28. #include "talk/base/event.h"
  29. #include "talk/base/gunit.h"
  30. #include "talk/base/host.h"
  31. #include "talk/base/physicalsocketserver.h"
  32. #include "talk/base/socketaddress.h"
  33. #include "talk/base/thread.h"
  34. #ifdef WIN32
  35. #include <comdef.h> // NOLINT
  36. #endif
  37. using namespace talk_base;
  38. const int MAX = 65536;
  39. // Generates a sequence of numbers (collaboratively).
  40. class TestGenerator {
  41. public:
  42. TestGenerator() : last(0), count(0) {}
  43. int Next(int prev) {
  44. int result = prev + last;
  45. last = result;
  46. count += 1;
  47. return result;
  48. }
  49. int last;
  50. int count;
  51. };
  52. struct TestMessage : public MessageData {
  53. explicit TestMessage(int v) : value(v) {}
  54. virtual ~TestMessage() {}
  55. int value;
  56. };
  57. // Receives on a socket and sends by posting messages.
  58. class SocketClient : public TestGenerator, public sigslot::has_slots<> {
  59. public:
  60. SocketClient(AsyncSocket* socket, const SocketAddress& addr,
  61. Thread* post_thread, MessageHandler* phandler)
  62. : socket_(AsyncUDPSocket::Create(socket, addr)),
  63. post_thread_(post_thread),
  64. post_handler_(phandler) {
  65. socket_->SignalReadPacket.connect(this, &SocketClient::OnPacket);
  66. }
  67. ~SocketClient() {
  68. delete socket_;
  69. }
  70. SocketAddress address() const { return socket_->GetLocalAddress(); }
  71. void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t size,
  72. const SocketAddress& remote_addr) {
  73. EXPECT_EQ(size, sizeof(uint32));
  74. uint32 prev = reinterpret_cast<const uint32*>(buf)[0];
  75. uint32 result = Next(prev);
  76. //socket_->set_readable(last < MAX);
  77. post_thread_->PostDelayed(200, post_handler_, 0, new TestMessage(result));
  78. }
  79. private:
  80. AsyncUDPSocket* socket_;
  81. Thread* post_thread_;
  82. MessageHandler* post_handler_;
  83. };
  84. // Receives messages and sends on a socket.
  85. class MessageClient : public MessageHandler, public TestGenerator {
  86. public:
  87. MessageClient(Thread* pth, Socket* socket)
  88. : thread_(pth), socket_(socket) {
  89. }
  90. virtual ~MessageClient() {
  91. delete socket_;
  92. }
  93. virtual void OnMessage(Message *pmsg) {
  94. TestMessage* msg = static_cast<TestMessage*>(pmsg->pdata);
  95. int result = Next(msg->value);
  96. EXPECT_GE(socket_->Send(&result, sizeof(result)), 0);
  97. delete msg;
  98. }
  99. private:
  100. Thread* thread_;
  101. Socket* socket_;
  102. };
  103. class CustomThread : public talk_base::Thread {
  104. public:
  105. CustomThread() {}
  106. virtual ~CustomThread() {}
  107. bool Start() { return false; }
  108. };
  109. // A thread that does nothing when it runs and signals an event
  110. // when it is destroyed.
  111. class SignalWhenDestroyedThread : public Thread {
  112. public:
  113. SignalWhenDestroyedThread(Event* event)
  114. : event_(event) {
  115. }
  116. virtual ~SignalWhenDestroyedThread() {
  117. event_->Set();
  118. }
  119. virtual void Run() {
  120. // Do nothing.
  121. }
  122. private:
  123. Event* event_;
  124. };
  125. TEST(ThreadTest, Main) {
  126. const SocketAddress addr("127.0.0.1", 0);
  127. // Create the messaging client on its own thread.
  128. Thread th1;
  129. Socket* socket = th1.socketserver()->CreateAsyncSocket(addr.family(),
  130. SOCK_DGRAM);
  131. MessageClient msg_client(&th1, socket);
  132. // Create the socket client on its own thread.
  133. Thread th2;
  134. AsyncSocket* asocket =
  135. th2.socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM);
  136. SocketClient sock_client(asocket, addr, &th1, &msg_client);
  137. socket->Connect(sock_client.address());
  138. th1.Start();
  139. th2.Start();
  140. // Get the messages started.
  141. th1.PostDelayed(100, &msg_client, 0, new TestMessage(1));
  142. // Give the clients a little while to run.
  143. // Messages will be processed at 100, 300, 500, 700, 900.
  144. Thread* th_main = Thread::Current();
  145. th_main->ProcessMessages(1000);
  146. // Stop the sending client. Give the receiver a bit longer to run, in case
  147. // it is running on a machine that is under load (e.g. the build machine).
  148. th1.Stop();
  149. th_main->ProcessMessages(200);
  150. th2.Stop();
  151. // Make sure the results were correct
  152. EXPECT_EQ(5, msg_client.count);
  153. EXPECT_EQ(34, msg_client.last);
  154. EXPECT_EQ(5, sock_client.count);
  155. EXPECT_EQ(55, sock_client.last);
  156. }
  157. // Test that setting thread names doesn't cause a malfunction.
  158. // There's no easy way to verify the name was set properly at this time.
  159. TEST(ThreadTest, Names) {
  160. // Default name
  161. Thread *thread;
  162. thread = new Thread();
  163. EXPECT_TRUE(thread->Start());
  164. thread->Stop();
  165. delete thread;
  166. thread = new Thread();
  167. // Name with no object parameter
  168. EXPECT_TRUE(thread->SetName("No object", NULL));
  169. EXPECT_TRUE(thread->Start());
  170. thread->Stop();
  171. delete thread;
  172. // Really long name
  173. thread = new Thread();
  174. EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this));
  175. EXPECT_TRUE(thread->Start());
  176. thread->Stop();
  177. delete thread;
  178. }
  179. // Test that setting thread priorities doesn't cause a malfunction.
  180. // There's no easy way to verify the priority was set properly at this time.
  181. TEST(ThreadTest, Priorities) {
  182. Thread *thread;
  183. thread = new Thread();
  184. EXPECT_TRUE(thread->SetPriority(PRIORITY_HIGH));
  185. EXPECT_TRUE(thread->Start());
  186. thread->Stop();
  187. delete thread;
  188. thread = new Thread();
  189. EXPECT_TRUE(thread->SetPriority(PRIORITY_ABOVE_NORMAL));
  190. EXPECT_TRUE(thread->Start());
  191. thread->Stop();
  192. delete thread;
  193. thread = new Thread();
  194. EXPECT_TRUE(thread->Start());
  195. #ifdef WIN32
  196. EXPECT_TRUE(thread->SetPriority(PRIORITY_ABOVE_NORMAL));
  197. #else
  198. EXPECT_FALSE(thread->SetPriority(PRIORITY_ABOVE_NORMAL));
  199. #endif
  200. thread->Stop();
  201. delete thread;
  202. }
  203. TEST(ThreadTest, Wrap) {
  204. Thread* current_thread = Thread::Current();
  205. current_thread->UnwrapCurrent();
  206. CustomThread* cthread = new CustomThread();
  207. EXPECT_TRUE(cthread->WrapCurrent());
  208. EXPECT_TRUE(cthread->started());
  209. EXPECT_FALSE(cthread->IsOwned());
  210. cthread->UnwrapCurrent();
  211. EXPECT_FALSE(cthread->started());
  212. delete cthread;
  213. current_thread->WrapCurrent();
  214. }
  215. // Test that calling Release on a thread causes it to self-destruct when
  216. // it's finished running
  217. TEST(ThreadTest, Release) {
  218. scoped_ptr<Event> event(new Event(true, false));
  219. // Ensure the event is initialized.
  220. event->Reset();
  221. Thread* thread = new SignalWhenDestroyedThread(event.get());
  222. thread->Start();
  223. thread->Release();
  224. // The event should get signaled when the thread completes, which should
  225. // be nearly instantaneous, since it doesn't do anything. For safety,
  226. // give it 3 seconds in case the machine is under load.
  227. bool signaled = event->Wait(3000);
  228. EXPECT_TRUE(signaled);
  229. }
  230. #ifdef WIN32
  231. class ComThreadTest : public testing::Test, public MessageHandler {
  232. public:
  233. ComThreadTest() : done_(false) {}
  234. protected:
  235. virtual void OnMessage(Message* message) {
  236. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  237. // S_FALSE means the thread was already inited for a multithread apartment.
  238. EXPECT_EQ(S_FALSE, hr);
  239. if (SUCCEEDED(hr)) {
  240. CoUninitialize();
  241. }
  242. done_ = true;
  243. }
  244. bool done_;
  245. };
  246. TEST_F(ComThreadTest, ComInited) {
  247. Thread* thread = new ComThread();
  248. EXPECT_TRUE(thread->Start());
  249. thread->Post(this, 0);
  250. EXPECT_TRUE_WAIT(done_, 1000);
  251. delete thread;
  252. }
  253. #endif