update tests

pull/38/head^2
Jeff Becker 6 years ago
parent 73e6141023
commit f285a0ac3e
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -8,7 +8,7 @@
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"program": "${workspaceFolder}/lokinet",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

@ -6,7 +6,7 @@
{
"label": "build",
"type": "shell",
"command": "make",
"command": "make -j8 JSONRPC=ON",
"group": "build",
"problemMatcher": [
"$gcc"

@ -511,6 +511,7 @@ if(USE_LIBABYSS)
set(ALL_SRC ${ALL_SRC} ${ABYSS_SRC} ${ABYSS}/main.cpp)
add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp)
target_link_libraries(${ABYSS_EXE} ${PLATFORM_LIB})
set(TEST_SRC ${TEST_SRC} test/jsonrpc_unittest.cpp)
endif()
foreach(F ${ALL_SRC})

@ -31,9 +31,10 @@ TESTNET_CLIENTS ?= 50
TESTNET_SERVERS ?= 50
TESTNET_DEBUG ?= 0
JSONRPC = ON
JSONRPC = OFF
CXX17 = ON
BUILD_ROOT = $(REPO)/build
CONFIG_CMD = $(shell /bin/echo -n "cd '$(BUILD_ROOT)' && " ; /bin/echo -n "cmake -DUSE_CXX17=$(CXX17) -DUSE_LIBABYSS=$(JSONRPC) '$(REPO)'")

@ -20,7 +20,7 @@ namespace llarp
operator^(const Key_t& other) const
{
Key_t dist;
for(size_t idx = 0; idx < 4; ++idx)
for(size_t idx = 0; idx < (size() / sizeof(l[0])); ++idx)
dist.l[idx] = l[idx] ^ other.l[idx];
return dist;
}

@ -28,10 +28,7 @@ namespace abyss
ProcessHeaderLine(abyss::string_view line, bool& done);
virtual bool
ShouldProcessHeader(const abyss::string_view& line) const
{
return true;
}
ShouldProcessHeader(const abyss::string_view& line) const = 0;
};
} // namespace http

@ -50,6 +50,10 @@ namespace abyss
void
RemoveConn(IRPCHandler* handler);
/// close the handler and acceptor
void
Close();
llarp_time_t
now() const
{

@ -86,6 +86,12 @@ namespace abyss
return HandleStatusCode(codePart.substr(0, idx));
}
bool
ShouldProcessHeader(const abyss::string_view& name) const
{
return name == "content-length" || name == "content-type";
}
/// return true if we get a 200 status code
bool
HandleStatusCode(string_view code) const

@ -365,11 +365,16 @@ namespace abyss
}
}
BaseReqHandler::~BaseReqHandler()
void
BaseReqHandler::Close()
{
llarp_tcp_acceptor_close(&m_acceptor);
}
BaseReqHandler::~BaseReqHandler()
{
}
void
BaseReqHandler::OnAccept(llarp_tcp_acceptor* acceptor, llarp_tcp_conn* conn)
{

@ -77,7 +77,7 @@ TEST_F(KademliaDHTTest, TestBucketOperators)
ASSERT_TRUE((one ^ three) == (three ^ one));
};
TEST_F(KademliaDHTTest, TestBucketRandomzied)
TEST_F(KademliaDHTTest, TestBucketRandomzied_1000)
{
size_t moreNodes = 100;
while(moreNodes--)
@ -86,13 +86,35 @@ TEST_F(KademliaDHTTest, TestBucketRandomzied)
n.ID.Randomize();
nodes->PutNode(n);
}
llarp::dht::Key_t result;
llarp::dht::Key_t target;
llarp::dht::Key_t oldResult;
target.Randomize();
oldResult = result;
ASSERT_TRUE(nodes->FindClosest(target, result));
ASSERT_TRUE((result ^ target) < (oldResult ^ target));
ASSERT_TRUE((result ^ target) != (oldResult ^ target));
ASSERT_FALSE((result ^ target) == (oldResult ^ target));
const size_t count = 1000;
size_t left = count;
while(left--)
{
llarp::dht::Key_t result;
llarp::dht::Key_t target;
llarp::dht::Key_t expect;
target.Randomize();
expect = target;
ASSERT_TRUE(nodes->FindClosest(target, result));
if(target == result)
{
ASSERT_FALSE((result ^ target) < (expect ^ target));
ASSERT_FALSE((result ^ target) != (expect ^ target));
ASSERT_TRUE((result ^ target) == (expect ^ target));
}
else
{
Key_t dist = result ^ target;
Key_t oldDist = expect ^ target;
ASSERT_TRUE((result ^ target) != (expect ^ target));
if((result ^ target) < (expect ^ target))
{
std::cout << "result=" << result << "expect=" << expect << std::endl;
std::cout << dist << ">=" << oldDist << "iteration=" << (count - left)
<< std::endl;
ASSERT_TRUE(false);
}
ASSERT_FALSE((result ^ target) == (expect ^ target));
}
}
};

@ -0,0 +1,169 @@
#include <gtest/gtest.h>
#include <libabyss.hpp>
#include <llarp/ev.h>
#include <llarp/threading.hpp>
#include <llarp/net.hpp>
struct AbyssTestBase : public ::testing::Test
{
llarp_threadpool* threadpool = nullptr;
llarp_ev_loop* loop = nullptr;
llarp_logic* logic = nullptr;
abyss::httpd::BaseReqHandler* server = nullptr;
abyss::http::JSONRPC* client = nullptr;
const std::string method = "test.method";
void
AssertMethod(const std::string& meth) const
{
ASSERT_TRUE(meth == method);
}
void
Start()
{
threadpool = llarp_init_same_process_threadpool();
llarp_ev_loop_alloc(&loop);
logic = llarp_init_single_process_logic(threadpool);
sockaddr_in addr;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons((llarp_randint() % 2000) + 2000);
addr.sin_family = AF_INET;
llarp::Addr a(addr);
while(true)
{
if(server->ServeAsync(loop, logic, a))
{
client->RunAsync(loop, a.ToString());
return;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void
Stop()
{
if(server)
server->Close();
llarp_logic_stop(logic);
llarp_ev_loop_stop(loop);
llarp_threadpool_stop(threadpool);
}
void
TearDown()
{
if(loop && threadpool && logic)
{
llarp_free_logic(&logic);
llarp_ev_loop_free(&loop);
llarp_free_threadpool(&threadpool);
}
}
};
struct ClientHandler : public abyss::http::IRPCClientHandler
{
AbyssTestBase* test;
ClientHandler(abyss::http::ConnImpl* impl, AbyssTestBase* parent)
: abyss::http::IRPCClientHandler(impl), test(parent)
{
}
void
HandleError()
{
ASSERT_TRUE(false);
}
void
PopulateReqHeaders(abyss::http::Headers_t& hdr)
{
}
bool
HandleResponse(const abyss::http::RPC_Response& response)
{
test->Stop();
return true;
}
};
struct ServerHandler : public abyss::httpd::IRPCHandler
{
const AbyssTestBase* test;
ServerHandler(abyss::httpd::ConnImpl* impl, const AbyssTestBase* parent)
: abyss::httpd::IRPCHandler(impl), test(parent)
{
}
bool
HandleJSONRPC(Method_t method, const Params& params, Response& response)
{
test->AssertMethod(method);
return true;
}
};
struct AbyssTest : public AbyssTestBase,
public abyss::http::JSONRPC,
public abyss::httpd::BaseReqHandler
{
AbyssTest()
: AbyssTestBase()
, abyss::http::JSONRPC()
, abyss::httpd::BaseReqHandler(1000)
{
}
abyss::http::IRPCClientHandler*
NewConn(abyss::http::ConnImpl* impl)
{
return new ClientHandler(impl, this);
}
abyss::httpd::IRPCHandler*
CreateHandler(abyss::httpd::ConnImpl* impl) const
{
return new ServerHandler(impl, this);
}
void
SetUp()
{
client = this;
server = this;
}
static void
FlushIt(void* u)
{
static_cast< AbyssTest* >(u)->Flush();
}
void
AsyncFlush()
{
llarp_logic_queue_job(logic, {this, &FlushIt});
}
void
RunLoop()
{
llarp_ev_loop_run_single_process(loop, threadpool, logic);
}
};
TEST_F(AbyssTest, TestClientAndServer)
{
Start();
abyss::json::Value params;
params.SetObject();
QueueRPC(method, std::move(params),
std::bind(&AbyssTest::NewConn, this, std::placeholders::_1));
AsyncFlush();
RunLoop();
};
Loading…
Cancel
Save