Skip to content

Commit

Permalink
Fix race condition in MockServer
Browse files Browse the repository at this point in the history
  • Loading branch information
achilleasa committed Feb 17, 2016
1 parent 277d708 commit f757ca6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions test/lib/mocks/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class MockServer {
List<String> _replayAuthDumpFileList;
String _pathToDumps;
Duration responseDelay;
Future _replayFuture = new Future.value();

MockServer() {
List<String> pathSegments = Platform.script.pathSegments.getRange(0, Platform.script.pathSegments.length - 1).toList();
Expand All @@ -112,6 +113,7 @@ class MockServer {
mockLogger.info("Shutting down server [${_server.address}:${_server.port}]");

List<Future> cleanupFutures = []
..add(_replayFuture)
..addAll(clients.map((Socket client) => new Future.value(client.destroy())))
..add(_server.close().then((_) => new Future.delayed(new Duration(milliseconds:20), () => true)));

Expand Down Expand Up @@ -210,9 +212,11 @@ class MockServer {

}

return responseDelay != null
return _replayFuture.then((_){
return responseDelay != null
? new Future.delayed(responseDelay, onReplay)
: onReplay();
});
}

Future listen(String host, int port) {
Expand Down Expand Up @@ -253,10 +257,10 @@ class MockServer {
writeMessage(client, Opcode.READY.value, streamId : frame.header.streamId);
} else if (_replayAuthDumpFileList != null && !_replayAuthDumpFileList.isEmpty) {
// Respond with the next payload in replay list
replayFile(clients.indexOf(client), _replayAuthDumpFileList.removeAt(0), frame.header.streamId);
_replayFuture = replayFile(clients.indexOf(client), _replayAuthDumpFileList.removeAt(0), frame.header.streamId);
} else if (_replayDumpFileList != null && !_replayDumpFileList.isEmpty) {
// Respond with the next payload in replay list
replayFile(clients.indexOf(client), _replayDumpFileList.removeAt(0), frame.header.streamId);
_replayFuture = replayFile(clients.indexOf(client), _replayDumpFileList.removeAt(0), frame.header.streamId);
}
}

Expand Down Expand Up @@ -298,4 +302,4 @@ class MockAuthenticator extends Authenticator {
return null;
}

}
}

0 comments on commit f757ca6

Please sign in to comment.