add CacheMap test

This commit is contained in:
an-tao 2018-05-04 09:51:12 +08:00
parent 87592fe7da
commit f42da49a80
4 changed files with 95 additions and 1 deletions

View File

@ -66,4 +66,8 @@ install(FILES ${trantor_net_headers} DESTINATION include/drogon)
add_dependencies(drogon trantor)
if (MAKETEST STREQUAL YES)
ADD_SUBDIRECTORY(tests)
endif ()
#target_link_libraries(drogon trantor pthread)

View File

@ -13,6 +13,10 @@
#include <vector>
#include <set>
namespace drogon
{
class CallbackEntry
{
public:
@ -196,4 +200,5 @@ protected:
}
}
}
};
};
}

4
tests/CMakeLists.txt Executable file
View File

@ -0,0 +1,4 @@
link_libraries(drogon trantor pthread)
add_executable(cache_map_test CacheMapTest.cc)

81
tests/CacheMapTest.cc Executable file
View File

@ -0,0 +1,81 @@
#include <drogon/CacheMap.h>
#include <trantor/net/EventLoopThread.h>
#include <unistd.h>
#include <string>
#include <thread>
#include <iostream>
int main()
{
trantor::EventLoopThread loopThread;
loopThread.run();
drogon::CacheMap<std::string,std::string> cache(loopThread.getLoop(),1,120);
cache.insert("1","first",20,[=]{
std::cout<<"first item in cache timeout,erase!"<<std::endl;
});
cache.insert("2","second",5);
cache.insert("3","third",5);
std::thread thread1([&]{
sleep(1);
cache.erase("3");
cache.insert("3","THIRD");
sleep(10);
if(cache.find("1"))
{
std::cout<<"sleep 10,1 item:"<<cache["1"]<<std::endl;
}
else
{
std::cout<<"can't find 1 item"<<std::endl;
}
sleep(15);
if(cache.find("1"))
{
std::cout<<"sleep 15,1 item:"<<cache["1"]<<std::endl;
}
else
{
std::cout<<"can't find 1 item"<<std::endl;
}
sleep(20);
if(cache.find("1"))
{
std::cout<<"sleep 20,1 item:"<<cache["1"]<<std::endl;
}
else
{
std::cout<<"can't find 1 item"<<std::endl;
}
sleep(22);
if(cache.find("1"))
{
std::cout<<"sleep22,1 item:"<<cache["1"]<<std::endl;
}
else
{
std::cout<<"can't find 1 item"<<std::endl;
}
if(cache.find("2"))
{
std::cout<<"sleep22,2 item:"<<cache["2"]<<std::endl;
}
else
{
std::cout<<"can't find 2 item"<<std::endl;
}
if(cache.find("3"))
{
std::cout<<"sleep22,3 item:"<<cache["3"]<<std::endl;
}
else
{
std::cout<<"can't find 3 item"<<std::endl;
}
});
thread1.join();
}