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 | 13 |
| 14 // Intercept the create port callback and remember which port was created. |
| 15 static Dart_Port saved_create_port = 0; |
| 16 static void MyCreatePortCallback(Dart_Isolate dart_isolate, |
| 17 Dart_Port port) { |
| 18 saved_create_port = port; |
| 19 } |
| 20 |
14 // Intercept the post message callback and just store a copy of the message. | 21 // Intercept the post message callback and just store a copy of the message. |
15 static const int kMaxSavedMsg = 80; | 22 static const int kMaxSavedMsg = 80; |
16 static char saved_msg[kMaxSavedMsg]; | 23 static char saved_msg[kMaxSavedMsg]; |
17 static bool MyPostMessageCallback(Dart_Isolate dest_isolate, | 24 static bool MyPostMessageCallback(Dart_Isolate dest_isolate, |
18 Dart_Port dest_port, | 25 Dart_Port dest_port, |
19 Dart_Port reply_port, | 26 Dart_Port reply_port, |
20 Dart_Message dart_message) { | 27 Dart_Message dart_message) { |
21 const char* msg = reinterpret_cast<char*>(dart_message); | 28 const char* msg = reinterpret_cast<char*>(dart_message); |
22 OS::SNPrint(saved_msg, kMaxSavedMsg, "%s", msg); | 29 OS::SNPrint(saved_msg, kMaxSavedMsg, "%s", msg); |
23 bool result = (strcmp(msg, "fail") != 0); | 30 bool result = (strcmp(msg, "fail") != 0); |
24 free(dart_message); | 31 free(dart_message); |
25 return result; | 32 return result; |
26 } | 33 } |
27 | 34 |
28 | 35 |
29 // Intercept the close port callback and remember which port was closed. | 36 // Intercept the close port callback and remember which port was closed. |
30 static Dart_Port saved_port = 0; | 37 static Dart_Port saved_close_port = 0; |
31 static void MyClosePortCallback(Dart_Isolate dart_isolate, | 38 static void MyClosePortCallback(Dart_Isolate dart_isolate, |
32 Dart_Port port) { | 39 Dart_Port port) { |
33 saved_port = port; | 40 saved_close_port = port; |
34 } | 41 } |
35 | 42 |
36 | 43 |
37 static void InitPortMapTest() { | 44 static void InitPortMapTest() { |
38 Dart_SetMessageCallbacks(&MyPostMessageCallback, &MyClosePortCallback); | 45 Dart_SetMessageCallbacks( |
39 saved_port = 0; | 46 &MyCreatePortCallback, &MyPostMessageCallback, &MyClosePortCallback); |
| 47 saved_create_port = 0; |
| 48 saved_close_port = 0; |
40 saved_msg[0] = '\0'; | 49 saved_msg[0] = '\0'; |
41 } | 50 } |
42 | 51 |
43 | 52 |
44 TEST_CASE(PortMap_CreateAndCloseOnePort) { | 53 TEST_CASE(PortMap_CreateAndCloseOnePort) { |
45 InitPortMapTest(); | 54 InitPortMapTest(); |
46 intptr_t port = PortMap::CreatePort(); | 55 intptr_t port = PortMap::CreatePort(); |
47 EXPECT_NE(0, port); | 56 EXPECT_NE(0, port); |
48 EXPECT(PortMap::IsActivePort(port)); | 57 EXPECT(PortMap::IsActivePort(port)); |
49 | 58 |
| 59 // Embedder was notified of port creation. |
| 60 EXPECT_EQ(port, saved_create_port); |
| 61 |
50 PortMap::ClosePort(port); | 62 PortMap::ClosePort(port); |
51 EXPECT(!PortMap::IsActivePort(port)); | 63 EXPECT(!PortMap::IsActivePort(port)); |
52 | 64 |
53 // Embedder was notified of port closure. | 65 // Embedder was notified of port closure. |
54 EXPECT_EQ(port, saved_port); | 66 EXPECT_EQ(port, saved_close_port); |
55 } | 67 } |
56 | 68 |
57 | 69 |
58 TEST_CASE(PortMap_CreateAndCloseTwoPorts) { | 70 TEST_CASE(PortMap_CreateAndCloseTwoPorts) { |
59 InitPortMapTest(); | 71 InitPortMapTest(); |
60 Dart_Port port1 = PortMap::CreatePort(); | 72 Dart_Port port1 = PortMap::CreatePort(); |
61 Dart_Port port2 = PortMap::CreatePort(); | 73 Dart_Port port2 = PortMap::CreatePort(); |
62 EXPECT(PortMap::IsActivePort(port1)); | 74 EXPECT(PortMap::IsActivePort(port1)); |
63 EXPECT(PortMap::IsActivePort(port2)); | 75 EXPECT(PortMap::IsActivePort(port2)); |
64 | 76 |
65 // Uniqueness. | 77 // Uniqueness. |
66 EXPECT_NE(port1, port2); | 78 EXPECT_NE(port1, port2); |
67 | 79 |
68 PortMap::ClosePort(port1); | 80 PortMap::ClosePort(port1); |
69 EXPECT(!PortMap::IsActivePort(port1)); | 81 EXPECT(!PortMap::IsActivePort(port1)); |
70 EXPECT(PortMap::IsActivePort(port2)); | 82 EXPECT(PortMap::IsActivePort(port2)); |
71 EXPECT_EQ(port1, saved_port); | 83 EXPECT_EQ(port1, saved_close_port); |
72 | 84 |
73 PortMap::ClosePort(port2); | 85 PortMap::ClosePort(port2); |
74 EXPECT(!PortMap::IsActivePort(port1)); | 86 EXPECT(!PortMap::IsActivePort(port1)); |
75 EXPECT(!PortMap::IsActivePort(port2)); | 87 EXPECT(!PortMap::IsActivePort(port2)); |
76 EXPECT_EQ(port2, saved_port); | 88 EXPECT_EQ(port2, saved_close_port); |
77 } | 89 } |
78 | 90 |
79 | 91 |
80 TEST_CASE(PortMap_ClosePorts) { | 92 TEST_CASE(PortMap_ClosePorts) { |
81 InitPortMapTest(); | 93 InitPortMapTest(); |
82 Dart_Port port1 = PortMap::CreatePort(); | 94 Dart_Port port1 = PortMap::CreatePort(); |
83 Dart_Port port2 = PortMap::CreatePort(); | 95 Dart_Port port2 = PortMap::CreatePort(); |
84 EXPECT(PortMap::IsActivePort(port1)); | 96 EXPECT(PortMap::IsActivePort(port1)); |
85 EXPECT(PortMap::IsActivePort(port2)); | 97 EXPECT(PortMap::IsActivePort(port2)); |
86 | 98 |
87 // Close all ports at once. | 99 // Close all ports at once. |
88 PortMap::ClosePorts(); | 100 PortMap::ClosePorts(); |
89 EXPECT(!PortMap::IsActivePort(port1)); | 101 EXPECT(!PortMap::IsActivePort(port1)); |
90 EXPECT(!PortMap::IsActivePort(port2)); | 102 EXPECT(!PortMap::IsActivePort(port2)); |
91 | 103 |
92 // Embedder is notified to close all ports as well. | 104 // Embedder is notified to close all ports as well. |
93 EXPECT_EQ(kCloseAllPorts, saved_port); | 105 EXPECT_EQ(kCloseAllPorts, saved_close_port); |
94 } | 106 } |
95 | 107 |
96 | 108 |
97 TEST_CASE(PortMap_CreateManyPorts) { | 109 TEST_CASE(PortMap_CreateManyPorts) { |
98 InitPortMapTest(); | 110 InitPortMapTest(); |
99 for (int i = 0; i < 32; i++) { | 111 for (int i = 0; i < 32; i++) { |
100 Dart_Port port = PortMap::CreatePort(); | 112 Dart_Port port = PortMap::CreatePort(); |
101 EXPECT(PortMap::IsActivePort(port)); | 113 EXPECT(PortMap::IsActivePort(port)); |
102 PortMap::ClosePort(port); | 114 PortMap::ClosePort(port); |
103 EXPECT(!PortMap::IsActivePort(port)); | 115 EXPECT(!PortMap::IsActivePort(port)); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // Give the spawned thread enough time to properly exit. | 237 // Give the spawned thread enough time to properly exit. |
226 Monitor* waiter = new Monitor(); | 238 Monitor* waiter = new Monitor(); |
227 { | 239 { |
228 MonitorLocker ml(waiter); | 240 MonitorLocker ml(waiter); |
229 ml.Wait(20); | 241 ml.Wait(20); |
230 } | 242 } |
231 delete waiter; | 243 delete waiter; |
232 } | 244 } |
233 | 245 |
234 } // namespace dart | 246 } // namespace dart |
OLD | NEW |