httpmockserver
port_searcher.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <memory>
9 #include <stdexcept>
10 
11 namespace httpmock {
12 
13 
21 template <typename HTTPMock>
22 std::unique_ptr<MockServer> getFirstRunningMockServer(
23  unsigned port = 8080, unsigned tryCount = 1000)
24 {
25  for (unsigned p = 0; p < tryCount; p++) {
26  try {
27  std::unique_ptr<MockServer> server(new HTTPMock(port + p));
28  // try to run the server on current port
29  server->start();
30  return server;
31  } catch (const std::runtime_error &) {
32  // error occured, try next port number
33  continue;
34  }
35  }
36  throw std::runtime_error("MockServer did not come up!");
37 }
38 
39 
40 }
httpmock
Definition: mock_server.cc:20
httpmock::getFirstRunningMockServer
std::unique_ptr< MockServer > getFirstRunningMockServer(unsigned port=8080, unsigned tryCount=1000)
Definition: port_searcher.h:22