| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of ServerTest; | 5 part of ServerTest; |
| 6 | 6 |
| 7 abstract class TestingServer { | 7 abstract class TestingServer { |
| 8 | 8 |
| 9 static const HOST = "127.0.0.1"; | 9 static const HOST = "127.0.0.1"; |
| 10 static const INIT = 0; | 10 static const INIT = 0; |
| 11 static const SHUTDOWN = -1; | 11 static const SHUTDOWN = -1; |
| 12 | 12 |
| 13 void onConnection(Socket connection); // Abstract. | 13 void onConnection(Socket connection); // Abstract. |
| 14 | 14 |
| 15 void errorHandlerServer(e) { | 15 void errorHandlerServer(e, trace) { |
| 16 String msg = "Server socket error $e"; | 16 String msg = "Server socket error $e"; |
| 17 var trace = getAttachedStackTrace(e); | |
| 18 if (trace != null) msg += "\nStackTrace: $trace"; | 17 if (trace != null) msg += "\nStackTrace: $trace"; |
| 19 Expect.fail(msg); | 18 Expect.fail(msg); |
| 20 } | 19 } |
| 21 | 20 |
| 22 SendPort get closeSendPort => _closePort.sendPort; | 21 SendPort get closeSendPort => _closePort.sendPort; |
| 23 | 22 |
| 24 Future<int> init() { | 23 Future<int> init() { |
| 25 _closePort = new ReceivePort(); | 24 _closePort = new ReceivePort(); |
| 26 _closePort.first.then((_) { close(); }); | 25 _closePort.first.then((_) { close(); }); |
| 27 return ServerSocket.bind(HOST, 0).then((server) { | 26 return ServerSocket.bind(HOST, 0).then((server) { |
| 28 _server = server; | 27 _server = server; |
| 29 _server.listen( | 28 _server.listen( |
| 30 onConnection, | 29 onConnection, |
| 31 onError: errorHandlerServer); | 30 onError: errorHandlerServer); |
| 32 return _server.port; | 31 return _server.port; |
| 33 }); | 32 }); |
| 34 } | 33 } |
| 35 | 34 |
| 36 void close() { | 35 void close() { |
| 37 _server.close(); | 36 _server.close(); |
| 38 } | 37 } |
| 39 | 38 |
| 40 ServerSocket _server; | 39 ServerSocket _server; |
| 41 ReceivePort _closePort; | 40 ReceivePort _closePort; |
| 42 } | 41 } |
| OLD | NEW |