| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/assert.h" | 5 #include "vm/assert.h" |
| 6 #include "vm/message_queue.h" | 6 #include "vm/message_queue.h" |
| 7 #include "vm/os.h" | 7 #include "vm/os.h" |
| 8 #include "vm/port.h" | 8 #include "vm/port.h" |
| 9 #include "vm/unit_test.h" | 9 #include "vm/unit_test.h" |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 | 12 |
| 13 // Provides private access to PortMap for testing. |
| 14 class PortMapTestPeer { |
| 15 public: |
| 16 static bool IsActivePort(Dart_Port port) { |
| 17 MutexLocker ml(PortMap::mutex_); |
| 18 return (PortMap::FindPort(port) >= 0); |
| 19 } |
| 20 |
| 21 static bool IsLivePort(Dart_Port port) { |
| 22 MutexLocker ml(PortMap::mutex_); |
| 23 intptr_t index = PortMap::FindPort(port); |
| 24 if (index < 0) { |
| 25 return false; |
| 26 } |
| 27 return PortMap::map_[index].live; |
| 28 } |
| 29 }; |
| 30 |
| 13 | 31 |
| 14 // Intercept the post message callback and just store a copy of the message. | 32 // Intercept the post message callback and just store a copy of the message. |
| 15 static const int kMaxSavedMsg = 80; | 33 static const int kMaxSavedMsg = 80; |
| 16 static char saved_msg[kMaxSavedMsg]; | 34 static char saved_msg[kMaxSavedMsg]; |
| 17 static bool MyPostMessageCallback(Dart_Isolate dest_isolate, | 35 static bool MyPostMessageCallback(Dart_Isolate dest_isolate, |
| 18 Dart_Port dest_port, | 36 Dart_Port dest_port, |
| 19 Dart_Port reply_port, | 37 Dart_Port reply_port, |
| 20 Dart_Message dart_message) { | 38 Dart_Message dart_message) { |
| 21 const char* msg = reinterpret_cast<char*>(dart_message); | 39 const char* msg = reinterpret_cast<char*>(dart_message); |
| 22 OS::SNPrint(saved_msg, kMaxSavedMsg, "%s", msg); | 40 OS::SNPrint(saved_msg, kMaxSavedMsg, "%s", msg); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 38 Dart_SetMessageCallbacks(&MyPostMessageCallback, &MyClosePortCallback); | 56 Dart_SetMessageCallbacks(&MyPostMessageCallback, &MyClosePortCallback); |
| 39 saved_port = 0; | 57 saved_port = 0; |
| 40 saved_msg[0] = '\0'; | 58 saved_msg[0] = '\0'; |
| 41 } | 59 } |
| 42 | 60 |
| 43 | 61 |
| 44 TEST_CASE(PortMap_CreateAndCloseOnePort) { | 62 TEST_CASE(PortMap_CreateAndCloseOnePort) { |
| 45 InitPortMapTest(); | 63 InitPortMapTest(); |
| 46 intptr_t port = PortMap::CreatePort(); | 64 intptr_t port = PortMap::CreatePort(); |
| 47 EXPECT_NE(0, port); | 65 EXPECT_NE(0, port); |
| 48 EXPECT(PortMap::IsActivePort(port)); | 66 EXPECT(PortMapTestPeer::IsActivePort(port)); |
| 49 | 67 |
| 50 PortMap::ClosePort(port); | 68 PortMap::ClosePort(port); |
| 51 EXPECT(!PortMap::IsActivePort(port)); | 69 EXPECT(!PortMapTestPeer::IsActivePort(port)); |
| 52 | 70 |
| 53 // Embedder was notified of port closure. | 71 // Embedder was notified of port closure. |
| 54 EXPECT_EQ(port, saved_port); | 72 EXPECT_EQ(port, saved_port); |
| 55 } | 73 } |
| 56 | 74 |
| 57 | 75 |
| 58 TEST_CASE(PortMap_CreateAndCloseTwoPorts) { | 76 TEST_CASE(PortMap_CreateAndCloseTwoPorts) { |
| 59 InitPortMapTest(); | 77 InitPortMapTest(); |
| 60 Dart_Port port1 = PortMap::CreatePort(); | 78 Dart_Port port1 = PortMap::CreatePort(); |
| 61 Dart_Port port2 = PortMap::CreatePort(); | 79 Dart_Port port2 = PortMap::CreatePort(); |
| 62 EXPECT(PortMap::IsActivePort(port1)); | 80 EXPECT(PortMapTestPeer::IsActivePort(port1)); |
| 63 EXPECT(PortMap::IsActivePort(port2)); | 81 EXPECT(PortMapTestPeer::IsActivePort(port2)); |
| 64 | 82 |
| 65 // Uniqueness. | 83 // Uniqueness. |
| 66 EXPECT_NE(port1, port2); | 84 EXPECT_NE(port1, port2); |
| 67 | 85 |
| 68 PortMap::ClosePort(port1); | 86 PortMap::ClosePort(port1); |
| 69 EXPECT(!PortMap::IsActivePort(port1)); | 87 EXPECT(!PortMapTestPeer::IsActivePort(port1)); |
| 70 EXPECT(PortMap::IsActivePort(port2)); | 88 EXPECT(PortMapTestPeer::IsActivePort(port2)); |
| 71 EXPECT_EQ(port1, saved_port); | 89 EXPECT_EQ(port1, saved_port); |
| 72 | 90 |
| 73 PortMap::ClosePort(port2); | 91 PortMap::ClosePort(port2); |
| 74 EXPECT(!PortMap::IsActivePort(port1)); | 92 EXPECT(!PortMapTestPeer::IsActivePort(port1)); |
| 75 EXPECT(!PortMap::IsActivePort(port2)); | 93 EXPECT(!PortMapTestPeer::IsActivePort(port2)); |
| 76 EXPECT_EQ(port2, saved_port); | 94 EXPECT_EQ(port2, saved_port); |
| 77 } | 95 } |
| 78 | 96 |
| 79 | 97 |
| 80 TEST_CASE(PortMap_ClosePorts) { | 98 TEST_CASE(PortMap_ClosePorts) { |
| 81 InitPortMapTest(); | 99 InitPortMapTest(); |
| 82 Dart_Port port1 = PortMap::CreatePort(); | 100 Dart_Port port1 = PortMap::CreatePort(); |
| 83 Dart_Port port2 = PortMap::CreatePort(); | 101 Dart_Port port2 = PortMap::CreatePort(); |
| 84 EXPECT(PortMap::IsActivePort(port1)); | 102 EXPECT(PortMapTestPeer::IsActivePort(port1)); |
| 85 EXPECT(PortMap::IsActivePort(port2)); | 103 EXPECT(PortMapTestPeer::IsActivePort(port2)); |
| 86 | 104 |
| 87 // Close all ports at once. | 105 // Close all ports at once. |
| 88 PortMap::ClosePorts(); | 106 PortMap::ClosePorts(); |
| 89 EXPECT(!PortMap::IsActivePort(port1)); | 107 EXPECT(!PortMapTestPeer::IsActivePort(port1)); |
| 90 EXPECT(!PortMap::IsActivePort(port2)); | 108 EXPECT(!PortMapTestPeer::IsActivePort(port2)); |
| 91 | 109 |
| 92 // Embedder is notified to close all ports as well. | 110 // Embedder is notified to close all ports as well. |
| 93 EXPECT_EQ(kCloseAllPorts, saved_port); | 111 EXPECT_EQ(kCloseAllPorts, saved_port); |
| 94 } | 112 } |
| 95 | 113 |
| 96 | 114 |
| 97 TEST_CASE(PortMap_CreateManyPorts) { | 115 TEST_CASE(PortMap_CreateManyPorts) { |
| 98 InitPortMapTest(); | 116 InitPortMapTest(); |
| 99 for (int i = 0; i < 32; i++) { | 117 for (int i = 0; i < 32; i++) { |
| 100 Dart_Port port = PortMap::CreatePort(); | 118 Dart_Port port = PortMap::CreatePort(); |
| 101 EXPECT(PortMap::IsActivePort(port)); | 119 EXPECT(PortMapTestPeer::IsActivePort(port)); |
| 102 PortMap::ClosePort(port); | 120 PortMap::ClosePort(port); |
| 103 EXPECT(!PortMap::IsActivePort(port)); | 121 EXPECT(!PortMapTestPeer::IsActivePort(port)); |
| 104 } | 122 } |
| 105 } | 123 } |
| 106 | 124 |
| 107 | 125 |
| 126 TEST_CASE(PortMap_SetLive) { |
| 127 InitPortMapTest(); |
| 128 intptr_t port = PortMap::CreatePort(); |
| 129 EXPECT_NE(0, port); |
| 130 EXPECT(PortMapTestPeer::IsActivePort(port)); |
| 131 EXPECT(!PortMapTestPeer::IsLivePort(port)); |
| 132 |
| 133 PortMap::SetLive(port); |
| 134 EXPECT(PortMapTestPeer::IsActivePort(port)); |
| 135 EXPECT(PortMapTestPeer::IsLivePort(port)); |
| 136 |
| 137 PortMap::ClosePort(port); |
| 138 EXPECT(!PortMapTestPeer::IsActivePort(port)); |
| 139 EXPECT(!PortMapTestPeer::IsLivePort(port)); |
| 140 |
| 141 // Embedder was notified of port closure. |
| 142 EXPECT_EQ(port, saved_port); |
| 143 } |
| 144 |
| 145 |
| 108 TEST_CASE(PortMap_PostMessage) { | 146 TEST_CASE(PortMap_PostMessage) { |
| 109 InitPortMapTest(); | 147 InitPortMapTest(); |
| 110 Dart_Port port = PortMap::CreatePort(); | 148 Dart_Port port = PortMap::CreatePort(); |
| 111 EXPECT(PortMap::PostMessage( | 149 EXPECT(PortMap::PostMessage( |
| 112 port, 0, reinterpret_cast<Dart_Message>(strdup("msg")))); | 150 port, 0, reinterpret_cast<Dart_Message>(strdup("msg")))); |
| 113 | 151 |
| 114 // Check that the post message callback was called. | 152 // Check that the post message callback was called. |
| 115 EXPECT_STREQ("msg", saved_msg); | 153 EXPECT_STREQ("msg", saved_msg); |
| 116 PortMap::ClosePorts(); | 154 PortMap::ClosePorts(); |
| 117 } | 155 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 // Give the spawned thread enough time to properly exit. | 263 // Give the spawned thread enough time to properly exit. |
| 226 Monitor* waiter = new Monitor(); | 264 Monitor* waiter = new Monitor(); |
| 227 { | 265 { |
| 228 MonitorLocker ml(waiter); | 266 MonitorLocker ml(waiter); |
| 229 ml.Wait(20); | 267 ml.Wait(20); |
| 230 } | 268 } |
| 231 delete waiter; | 269 delete waiter; |
| 232 } | 270 } |
| 233 | 271 |
| 234 } // namespace dart | 272 } // namespace dart |
| OLD | NEW |