Make network tests concurrent

This commit is contained in:
Allan Odgaard
2013-03-10 16:04:46 +01:00
parent 090a6ac698
commit 4dada34bde
2 changed files with 60 additions and 69 deletions

View File

@@ -1,5 +1,5 @@
SOURCES = src/*.cc
TEST_SOURCES = tests/*.cc
TESTS = tests/*.cc
LINK += text cf io plist OakSystem regexp
LIBS = curl crypto
EXPORT = src/{constants,download{,_tbz},filter_*,key_chain,network,post,proxy,tbz}.h

View File

@@ -5,80 +5,71 @@
#define WEB_SERVER_PORT 61706
static class WebServerFixture : public CxxTest::GlobalFixture
__attribute__((constructor)) static void setup_fixtures ()
{
public:
bool setUpWorld()
if(!web::setup_server(WEB_SERVER_PORT))
{
if(web::setup_server(WEB_SERVER_PORT))
fprintf(stderr, "*** unable to setup server for http://localhost:%d/\n", WEB_SERVER_PORT);
abort();
}
std::thread([]{ web::run_server(path::join(__FILE__, "..")); }).detach();
}
void test_download ()
{
struct my_filter : filter_t
{
my_filter (std::string& status, std::map<std::string, std::string>& headers, std::string& body) : _status(status), _headers(headers), _body(body) { }
bool receive_status (std::string const& status)
{
std::thread([]{ web::run_server(path::join(__FILE__, "..")); }).detach();
_status = status;
return true;
}
return false;
}
} fixture;
class DownloadTests : public CxxTest::TestSuite
{
public:
void test_download ()
{
struct my_filter : filter_t
bool receive_header (std::string const& header, std::string const& value)
{
my_filter (std::string& status, std::map<std::string, std::string>& headers, std::string& body) : _status(status), _headers(headers), _body(body) { }
bool receive_status (std::string const& status)
{
_status = status;
return true;
}
bool receive_header (std::string const& header, std::string const& value)
{
_headers.insert(std::make_pair(header, value));
return true;
}
bool receive_data (char const* bytes, size_t len)
{
_body.insert(_body.end(), bytes, bytes + len);
return true;
}
bool receive_end (std::string& error)
{
return true;
}
private:
std::string& _status;
std::map<std::string, std::string>& _headers;
std::string& _body;
};
std::string status, body, error;
std::map<std::string, std::string> headers;
my_filter myFilter(status, headers, body);
static std::string const url = "http://localhost:" STRINGIFY(WEB_SERVER_PORT) "/t_download.cc";
TS_ASSERT_EQUALS(network::download(network::request_t(url, &myFilter, NULL), &error), 200);
struct stat buf;
int fd = open(__FILE__, O_RDONLY);
if(fd != -1 && fstat(fd, &buf) != -1)
{
size_t fileSize = buf.st_size;
char fileContent[fileSize];
if(read(fd, fileContent, fileSize) == fileSize)
{
TS_ASSERT_EQUALS(status, "HTTP/1.0 200 OK");
TS_ASSERT(headers.find("content-length") != headers.end());
TS_ASSERT_EQUALS(headers.find("content-length")->second, std::to_string(fileSize));
TS_ASSERT_EQUALS(body, std::string(fileContent, fileContent + fileSize));
}
close(fd);
_headers.insert(std::make_pair(header, value));
return true;
}
bool receive_data (char const* bytes, size_t len)
{
_body.insert(_body.end(), bytes, bytes + len);
return true;
}
bool receive_end (std::string& error)
{
return true;
}
private:
std::string& _status;
std::map<std::string, std::string>& _headers;
std::string& _body;
};
std::string status, body, error;
std::map<std::string, std::string> headers;
my_filter myFilter(status, headers, body);
static std::string const url = "http://localhost:" STRINGIFY(WEB_SERVER_PORT) "/t_download.cc";
OAK_ASSERT_EQ(network::download(network::request_t(url, &myFilter, NULL), &error), 200);
struct stat buf;
int fd = open(__FILE__, O_RDONLY|O_CLOEXEC);
if(fd != -1 && fstat(fd, &buf) != -1)
{
size_t fileSize = buf.st_size;
char fileContent[fileSize];
if(read(fd, fileContent, fileSize) == fileSize)
{
OAK_ASSERT_EQ(status, "HTTP/1.0 200 OK");
OAK_ASSERT(headers.find("content-length") != headers.end());
OAK_ASSERT_EQ(headers.find("content-length")->second, std::to_string(fileSize));
OAK_ASSERT_EQ(body, std::string(fileContent, fileContent + fileSize));
}
close(fd);
}
};
}