Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1116)

Side by Side Diff: runtime/vm/port_test.cc

Issue 9182001: OOB messages and general message refactor. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/port.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 "platform/assert.h" 5 #include "platform/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
(...skipping 12 matching lines...) Expand all
23 intptr_t index = PortMap::FindPort(port); 23 intptr_t index = PortMap::FindPort(port);
24 if (index < 0) { 24 if (index < 0) {
25 return false; 25 return false;
26 } 26 }
27 return PortMap::map_[index].live; 27 return PortMap::map_[index].live;
28 } 28 }
29 }; 29 };
30 30
31 31
32 // 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.
33 static const int kMaxSavedMsg = 80; 33 static int notify_count = 0;
34 static char saved_msg[kMaxSavedMsg]; 34 static void MyMessageNotifyCallback(Dart_Isolate dest_isolate) {
35 static bool MyPostMessageCallback(Dart_Isolate dest_isolate, 35 notify_count++;
36 Dart_Port dest_port,
37 Dart_Port reply_port,
38 Dart_Message dart_message) {
39 const char* msg = reinterpret_cast<char*>(dart_message);
40 OS::SNPrint(saved_msg, kMaxSavedMsg, "%s", msg);
41 bool result = (strcmp(msg, "fail") != 0);
42 free(dart_message);
43 return result;
44 }
45
46
47 // Intercept the close port callback and remember which port was closed.
48 static Dart_Port saved_port = 0;
49 static void MyClosePortCallback(Dart_Isolate dart_isolate,
50 Dart_Port port) {
51 saved_port = port;
52 } 36 }
53 37
54 38
55 static void InitPortMapTest() { 39 static void InitPortMapTest() {
56 Dart_SetMessageCallbacks(&MyPostMessageCallback, &MyClosePortCallback); 40 Dart_SetMessageNotifyCallback(&MyMessageNotifyCallback);
57 saved_port = 0; 41 notify_count = 0;
58 saved_msg[0] = '\0';
59 } 42 }
60 43
61 44
62 TEST_CASE(PortMap_CreateAndCloseOnePort) { 45 TEST_CASE(PortMap_CreateAndCloseOnePort) {
63 InitPortMapTest(); 46 InitPortMapTest();
64 intptr_t port = PortMap::CreatePort(); 47 intptr_t port = PortMap::CreatePort();
65 EXPECT_NE(0, port); 48 EXPECT_NE(0, port);
66 EXPECT(PortMapTestPeer::IsActivePort(port)); 49 EXPECT(PortMapTestPeer::IsActivePort(port));
67 50
68 PortMap::ClosePort(port); 51 PortMap::ClosePort(port);
69 EXPECT(!PortMapTestPeer::IsActivePort(port)); 52 EXPECT(!PortMapTestPeer::IsActivePort(port));
70
71 // Embedder was notified of port closure.
72 EXPECT_EQ(port, saved_port);
73 } 53 }
74 54
75 55
76 TEST_CASE(PortMap_CreateAndCloseTwoPorts) { 56 TEST_CASE(PortMap_CreateAndCloseTwoPorts) {
77 InitPortMapTest(); 57 InitPortMapTest();
78 Dart_Port port1 = PortMap::CreatePort(); 58 Dart_Port port1 = PortMap::CreatePort();
79 Dart_Port port2 = PortMap::CreatePort(); 59 Dart_Port port2 = PortMap::CreatePort();
80 EXPECT(PortMapTestPeer::IsActivePort(port1)); 60 EXPECT(PortMapTestPeer::IsActivePort(port1));
81 EXPECT(PortMapTestPeer::IsActivePort(port2)); 61 EXPECT(PortMapTestPeer::IsActivePort(port2));
82 62
83 // Uniqueness. 63 // Uniqueness.
84 EXPECT_NE(port1, port2); 64 EXPECT_NE(port1, port2);
85 65
86 PortMap::ClosePort(port1); 66 PortMap::ClosePort(port1);
87 EXPECT(!PortMapTestPeer::IsActivePort(port1)); 67 EXPECT(!PortMapTestPeer::IsActivePort(port1));
88 EXPECT(PortMapTestPeer::IsActivePort(port2)); 68 EXPECT(PortMapTestPeer::IsActivePort(port2));
89 EXPECT_EQ(port1, saved_port);
90 69
91 PortMap::ClosePort(port2); 70 PortMap::ClosePort(port2);
92 EXPECT(!PortMapTestPeer::IsActivePort(port1)); 71 EXPECT(!PortMapTestPeer::IsActivePort(port1));
93 EXPECT(!PortMapTestPeer::IsActivePort(port2)); 72 EXPECT(!PortMapTestPeer::IsActivePort(port2));
94 EXPECT_EQ(port2, saved_port);
95 } 73 }
96 74
97 75
98 TEST_CASE(PortMap_ClosePorts) { 76 TEST_CASE(PortMap_ClosePorts) {
99 InitPortMapTest(); 77 InitPortMapTest();
100 Dart_Port port1 = PortMap::CreatePort(); 78 Dart_Port port1 = PortMap::CreatePort();
101 Dart_Port port2 = PortMap::CreatePort(); 79 Dart_Port port2 = PortMap::CreatePort();
102 EXPECT(PortMapTestPeer::IsActivePort(port1)); 80 EXPECT(PortMapTestPeer::IsActivePort(port1));
103 EXPECT(PortMapTestPeer::IsActivePort(port2)); 81 EXPECT(PortMapTestPeer::IsActivePort(port2));
104 82
105 // Close all ports at once. 83 // Close all ports at once.
106 PortMap::ClosePorts(); 84 PortMap::ClosePorts();
107 EXPECT(!PortMapTestPeer::IsActivePort(port1)); 85 EXPECT(!PortMapTestPeer::IsActivePort(port1));
108 EXPECT(!PortMapTestPeer::IsActivePort(port2)); 86 EXPECT(!PortMapTestPeer::IsActivePort(port2));
109
110 // Embedder is notified to close all ports as well.
111 EXPECT_EQ(kCloseAllPorts, saved_port);
112 } 87 }
113 88
114 89
115 TEST_CASE(PortMap_CreateManyPorts) { 90 TEST_CASE(PortMap_CreateManyPorts) {
116 InitPortMapTest(); 91 InitPortMapTest();
117 for (int i = 0; i < 32; i++) { 92 for (int i = 0; i < 32; i++) {
118 Dart_Port port = PortMap::CreatePort(); 93 Dart_Port port = PortMap::CreatePort();
119 EXPECT(PortMapTestPeer::IsActivePort(port)); 94 EXPECT(PortMapTestPeer::IsActivePort(port));
120 PortMap::ClosePort(port); 95 PortMap::ClosePort(port);
121 EXPECT(!PortMapTestPeer::IsActivePort(port)); 96 EXPECT(!PortMapTestPeer::IsActivePort(port));
122 } 97 }
123 } 98 }
124 99
125 100
126 TEST_CASE(PortMap_SetLive) { 101 TEST_CASE(PortMap_SetLive) {
127 InitPortMapTest(); 102 InitPortMapTest();
128 intptr_t port = PortMap::CreatePort(); 103 intptr_t port = PortMap::CreatePort();
129 EXPECT_NE(0, port); 104 EXPECT_NE(0, port);
130 EXPECT(PortMapTestPeer::IsActivePort(port)); 105 EXPECT(PortMapTestPeer::IsActivePort(port));
131 EXPECT(!PortMapTestPeer::IsLivePort(port)); 106 EXPECT(!PortMapTestPeer::IsLivePort(port));
132 107
133 PortMap::SetLive(port); 108 PortMap::SetLive(port);
134 EXPECT(PortMapTestPeer::IsActivePort(port)); 109 EXPECT(PortMapTestPeer::IsActivePort(port));
135 EXPECT(PortMapTestPeer::IsLivePort(port)); 110 EXPECT(PortMapTestPeer::IsLivePort(port));
136 111
137 PortMap::ClosePort(port); 112 PortMap::ClosePort(port);
138 EXPECT(!PortMapTestPeer::IsActivePort(port)); 113 EXPECT(!PortMapTestPeer::IsActivePort(port));
139 EXPECT(!PortMapTestPeer::IsLivePort(port)); 114 EXPECT(!PortMapTestPeer::IsLivePort(port));
140
141 // Embedder was notified of port closure.
142 EXPECT_EQ(port, saved_port);
143 } 115 }
144 116
145 117
146 TEST_CASE(PortMap_PostMessage) { 118 TEST_CASE(PortMap_PostMessage) {
147 InitPortMapTest(); 119 InitPortMapTest();
148 Dart_Port port = PortMap::CreatePort(); 120 Dart_Port port = PortMap::CreatePort();
149 EXPECT(PortMap::PostMessage( 121 EXPECT(PortMap::PostMessage(new Message(
150 port, 0, reinterpret_cast<Dart_Message>(strdup("msg")))); 122 port, 0, reinterpret_cast<uint8_t*>(strdup("msg")),
123 Message::kNormalPriority)));
151 124
152 // Check that the post message callback was called. 125 // Check that the message notify callback was called.
153 EXPECT_STREQ("msg", saved_msg); 126 EXPECT_EQ(1, notify_count);
154 PortMap::ClosePorts(); 127 PortMap::ClosePorts();
155 } 128 }
156 129
157 130
158 TEST_CASE(PortMap_PostMessageInvalidPort) { 131 TEST_CASE(PortMap_PostMessageInvalidPort) {
159 InitPortMapTest(); 132 InitPortMapTest();
160 EXPECT(!PortMap::PostMessage( 133 EXPECT(!PortMap::PostMessage(new Message(
161 0, 0, reinterpret_cast<Dart_Message>(strdup("msg")))); 134 0, 0, reinterpret_cast<uint8_t*>(strdup("msg")),
135 Message::kNormalPriority)));
162 136
163 // Check that the post message callback was not called. 137 // Check that the message notifycallback was not called.
164 EXPECT_STREQ("", saved_msg); 138 EXPECT_STREQ(0, notify_count);
165 }
166
167
168 TEST_CASE(PortMap_PostMessageFailureInCallback) {
169 InitPortMapTest();
170 Dart_Port port = PortMap::CreatePort();
171
172 // Our callback is rigged to return false when it sees the message
173 // "fail". This return value is propagated out of PostMessage.
174 EXPECT(!PortMap::PostMessage(
175 port, 0, reinterpret_cast<Dart_Message>(strdup("fail"))));
176
177 // Check that the post message callback was called.
178 EXPECT_STREQ("fail", saved_msg);
179 PortMap::ClosePorts();
180 } 139 }
181 140
182 141
183 // End-of-test marker. 142 // End-of-test marker.
184 static const intptr_t kEOT = 0xFFFF; 143 static const intptr_t kEOT = 0xFFFF;
185 144
186 Dart_Message AllocIntData(intptr_t payload) { 145
146 uint8_t* AllocIntData(intptr_t payload) {
187 intptr_t* result = reinterpret_cast<intptr_t*>(malloc(sizeof(payload))); 147 intptr_t* result = reinterpret_cast<intptr_t*>(malloc(sizeof(payload)));
188 *result = payload; 148 *result = payload;
189 return reinterpret_cast<Dart_Message>(result); 149 return reinterpret_cast<uint8_t*>(result);
190 } 150 }
191 151
192 152
193 intptr_t GetIntData(Dart_Message data) { 153 intptr_t GetIntData(uint8_t* data) {
194 return *reinterpret_cast<intptr_t*>(data); 154 return *reinterpret_cast<intptr_t*>(data);
195 } 155 }
196 156
197 157
198 static PortMessage* NextMessage() { 158 static Message* NextMessage() {
199 Isolate* isolate = Isolate::Current(); 159 Isolate* isolate = Isolate::Current();
200 PortMessage* result = isolate->message_queue()->Dequeue(0); 160 Message* result = isolate->message_queue()->Dequeue(0);
201 return result; 161 return result;
202 } 162 }
203 163
204 164
205 void ThreadedPort_start(uword parameter) { 165 void ThreadedPort_start(uword parameter) {
206 // We only need an isolate here because the MutexLocker in 166 // We only need an isolate here because the MutexLocker in
207 // PortMap::CreatePort expects it, we don't need to initialize 167 // PortMap::CreatePort expects it, we don't need to initialize
208 // the isolate as it does not run any dart code. 168 // the isolate as it does not run any dart code.
209 Dart::CreateIsolate(NULL); 169 Dart::CreateIsolate(NULL);
210 170
211 intptr_t remote = parameter; 171 intptr_t remote = parameter;
212 intptr_t local = PortMap::CreatePort(); 172 intptr_t local = PortMap::CreatePort();
213 173
214 PortMap::PostMessage(remote, 0, AllocIntData(local)); 174 PortMap::PostMessage(new Message(
215 175 remote, 0, AllocIntData(local), Message::kNormalPriority));
216 intptr_t count = 0; 176 intptr_t count = 0;
217 while (true) { 177 while (true) {
218 PortMessage* msg = NextMessage(); 178 Message* msg = NextMessage();
219 EXPECT_EQ(local, msg->dest_port()); 179 EXPECT_EQ(local, msg->dest_port());
220 EXPECT(msg != NULL); 180 EXPECT(msg != NULL);
221 if (GetIntData(msg->data()) == kEOT) { 181 if (GetIntData(msg->data()) == kEOT) {
222 break; 182 break;
223 } 183 }
224 EXPECT(GetIntData(msg->data()) == count); 184 EXPECT(GetIntData(msg->data()) == count);
225 delete msg; 185 delete msg;
226 PortMap::PostMessage(remote, 0, AllocIntData(count * 2)); 186 PortMap::PostMessage(new Message(
187 remote, 0, AllocIntData(count * 2), Message::kNormalPriority));
227 count++; 188 count++;
228 } 189 }
229 PortMap::PostMessage(remote, 0, AllocIntData(kEOT)); 190 PortMap::PostMessage(new Message(
230 191 remote, 0, AllocIntData(kEOT), Message::kNormalPriority));
231 Dart::ShutdownIsolate(); 192 Dart::ShutdownIsolate();
232 } 193 }
233 194
234 195
235 TEST_CASE(ThreadedPort) { 196 TEST_CASE(ThreadedPort) {
236 intptr_t local = PortMap::CreatePort(); 197 intptr_t local = PortMap::CreatePort();
237 198
238 Thread* thr = new Thread(ThreadedPort_start, local); 199 Thread* thr = new Thread(ThreadedPort_start, local);
239 EXPECT(thr != NULL); 200 EXPECT(thr != NULL);
240 201
241 PortMessage* msg = NextMessage(); 202 Message* msg = NextMessage();
242 EXPECT_EQ(local, msg->dest_port()); 203 EXPECT_EQ(local, msg->dest_port());
243 EXPECT(msg != NULL); 204 EXPECT(msg != NULL);
244 intptr_t remote = GetIntData(msg->data()); // Get the remote port. 205 intptr_t remote = GetIntData(msg->data()); // Get the remote port.
245 delete msg; 206 delete msg;
246 207
247 for (intptr_t i = 0; i < 10; i++) { 208 for (intptr_t i = 0; i < 10; i++) {
248 PortMap::PostMessage(remote, 0, AllocIntData(i)); 209 PortMap::PostMessage(
249 PortMessage* msg = NextMessage(); 210 new Message(remote, 0, AllocIntData(i), Message::kNormalPriority));
211 Message* msg = NextMessage();
250 EXPECT_EQ(local, msg->dest_port()); 212 EXPECT_EQ(local, msg->dest_port());
251 EXPECT(msg != NULL); 213 EXPECT(msg != NULL);
252 EXPECT_EQ(i * 2, GetIntData(msg->data())); 214 EXPECT_EQ(i * 2, GetIntData(msg->data()));
253 delete msg; 215 delete msg;
254 } 216 }
255 217
256 PortMap::PostMessage(remote, 0, AllocIntData(kEOT)); 218 PortMap::PostMessage(
219 new Message(remote, 0, AllocIntData(kEOT), Message::kNormalPriority));
257 msg = NextMessage(); 220 msg = NextMessage();
258 EXPECT_EQ(local, msg->dest_port()); 221 EXPECT_EQ(local, msg->dest_port());
259 EXPECT(msg != NULL); 222 EXPECT(msg != NULL);
260 EXPECT_EQ(kEOT, GetIntData(msg->data())); 223 EXPECT_EQ(kEOT, GetIntData(msg->data()));
261 delete msg; 224 delete msg;
262 225
263 // Give the spawned thread enough time to properly exit. 226 // Give the spawned thread enough time to properly exit.
264 Monitor* waiter = new Monitor(); 227 Monitor* waiter = new Monitor();
265 { 228 {
266 MonitorLocker ml(waiter); 229 MonitorLocker ml(waiter);
267 ml.Wait(20); 230 ml.Wait(20);
268 } 231 }
269 delete waiter; 232 delete waiter;
270 } 233 }
271 234
272 } // namespace dart 235 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/port.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698