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

Side by Side Diff: tests/standalone/io/http_read_test.dart

Issue 10837070: Remove old isolate API and update all code in the repository to use (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix namespace comment. Created 8 years, 4 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
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 // VMOptions= 5 // VMOptions=
6 // VMOptions=--short_socket_read 6 // VMOptions=--short_socket_read
7 // VMOptions=--short_socket_write 7 // VMOptions=--short_socket_write
8 // VMOptions=--short_socket_read --short_socket_write 8 // VMOptions=--short_socket_read --short_socket_write
9 9
10 #import("dart:isolate"); 10 #import("dart:isolate");
11 #import("dart:io"); 11 #import("dart:io");
12 12
13 class TestServerMain { 13 class TestServerMain {
14 TestServerMain() 14 TestServerMain()
15 : _statusPort = new ReceivePort(), 15 : _statusPort = new ReceivePort(),
16 _serverPort = null { 16 _serverPort = null {
17 new TestServer().spawn().then((SendPort port) { 17 _serverPort = spawnFunction(startTestServer);
18 _serverPort = port;
19 });
20 } 18 }
21 19
22 void setServerStartedHandler(void startedCallback(int port)) { 20 void setServerStartedHandler(void startedCallback(int port)) {
23 _startedCallback = startedCallback; 21 _startedCallback = startedCallback;
24 } 22 }
25 23
26 void start() { 24 void start() {
27 // Handle status messages from the server. 25 // Handle status messages from the server.
28 _statusPort.receive((var status, SendPort replyTo) { 26 _statusPort.receive((var status, SendPort replyTo) {
29 if (status.isStarted) { 27 if (status.isStarted) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 bool get isStopped() => _state == STOPPED; 82 bool get isStopped() => _state == STOPPED;
85 bool get isError() => _state == ERROR; 83 bool get isError() => _state == ERROR;
86 84
87 int get port() => _port; 85 int get port() => _port;
88 86
89 int _state; 87 int _state;
90 int _port; 88 int _port;
91 } 89 }
92 90
93 91
94 class TestServer extends Isolate { 92 void startTestServer() {
93 var server = new TestServer();
94 server.init();
95 port.receive((message, replyTo) {
kasperl 2012/08/03 05:20:27 port.receive(server.dispatch)? Maybe this is a pat
96 server.dispatch(message, replyTo);
97 });
98 }
99
100 class TestServer {
95 // Echo the request content back to the response. 101 // Echo the request content back to the response.
96 void _echoHandler(HttpRequest request, HttpResponse response) { 102 void _echoHandler(HttpRequest request, HttpResponse response) {
97 Expect.equals("POST", request.method); 103 Expect.equals("POST", request.method);
98 response.contentLength = request.contentLength; 104 response.contentLength = request.contentLength;
99 request.inputStream.pipe(response.outputStream); 105 request.inputStream.pipe(response.outputStream);
100 } 106 }
101 107
102 // Return a 404. 108 // Return a 404.
103 void _notFoundHandler(HttpRequest request, HttpResponse response) { 109 void _notFoundHandler(HttpRequest request, HttpResponse response) {
104 response.statusCode = HttpStatus.NOT_FOUND; 110 response.statusCode = HttpStatus.NOT_FOUND;
105 response.headers.set("Content-Type", "text/html; charset=UTF-8"); 111 response.headers.set("Content-Type", "text/html; charset=UTF-8");
106 response.outputStream.writeString("Page not found"); 112 response.outputStream.writeString("Page not found");
107 response.outputStream.close(); 113 response.outputStream.close();
108 } 114 }
109 115
110 116
111 void main() { 117 void init() {
112 // Setup request handlers. 118 // Setup request handlers.
113 _requestHandlers = new Map(); 119 _requestHandlers = new Map();
114 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { 120 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) {
115 _echoHandler(request, response); 121 _echoHandler(request, response);
116 }; 122 };
123 }
117 124
118 this.port.receive((var message, SendPort replyTo) { 125 void dispatch(message, SendPort replyTo) {
119 if (message.isStart) { 126 if (message.isStart) {
120 _server = new HttpServer(); 127 _server = new HttpServer();
121 try { 128 try {
122 _server.listen("127.0.0.1", 0); 129 _server.listen("127.0.0.1", 0);
123 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { 130 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) {
124 _requestReceivedHandler(req, rsp); 131 _requestReceivedHandler(req, rsp);
125 }; 132 };
126 replyTo.send(new TestServerStatus.started(_server.port), null); 133 replyTo.send(new TestServerStatus.started(_server.port), null);
127 } catch (var e) { 134 } catch (var e) {
128 replyTo.send(new TestServerStatus.error(), null); 135 replyTo.send(new TestServerStatus.error(), null);
129 }
130 } else if (message.isStop) {
131 _server.close();
132 this.port.close();
133 replyTo.send(new TestServerStatus.stopped(), null);
134 } else if (message.isChunkedEncoding) {
135 _chunkedEncoding = true;
136 } 136 }
137 }); 137 } else if (message.isStop) {
138 _server.close();
139 port.close();
140 replyTo.send(new TestServerStatus.stopped(), null);
141 } else if (message.isChunkedEncoding) {
142 _chunkedEncoding = true;
143 }
138 } 144 }
139 145
140 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { 146 void _requestReceivedHandler(HttpRequest request, HttpResponse response) {
141 var requestHandler =_requestHandlers[request.path]; 147 var requestHandler =_requestHandlers[request.path];
142 if (requestHandler != null) { 148 if (requestHandler != null) {
143 requestHandler(request, response); 149 requestHandler(request, response);
144 } else { 150 } else {
145 _notFoundHandler(request, response); 151 _notFoundHandler(request, response);
146 } 152 }
147 } 153 }
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 } 263 }
258 testServerMain.start(); 264 testServerMain.start();
259 } 265 }
260 266
261 void main() { 267 void main() {
262 testReadInto(true); 268 testReadInto(true);
263 testReadInto(false); 269 testReadInto(false);
264 testReadShort(true); 270 testReadShort(true);
265 testReadShort(false); 271 testReadShort(false);
266 } 272 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698