Skip to content

Commit

Permalink
modify testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
yedf committed Jun 15, 2015
1 parent 3ea91fe commit 1f95f0e
Show file tree
Hide file tree
Showing 22 changed files with 501 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build_config
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ $CXX -x c++ - -o $TMPDIR/handy_build_config.out >/dev/null 2>&1 <<EOF
#include <sys/epoll.h>
int main(){}
EOF
[ x$? == 0 ] && COMMON_FLAGS="$COMMON_FLAGS -DUSE_EPOLL"
[ $? = 0 ] && COMMON_FLAGS="$COMMON_FLAGS -DUSE_EPOLL"

COMMON_FLAGS="$COMMON_FLAGS -DPLATFORM_IS_LITTLE_ENDIAN=$PLATFORM_IS_LITTLE_ENDIAN -std=c++11"
PLATFORM_CCFLAGS="$PLATFORM_CCFLAGS $COMMON_FLAGS"
Expand Down
2 changes: 1 addition & 1 deletion handy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SOURCES = $(shell ls *.cc)

OBJECTS = $(SOURCES:.cc=.o)

LIBRARY = lib$(LIBNAME).a
LIBRARY = libhandy.a

default: $(LIBRARY)

Expand Down
4 changes: 2 additions & 2 deletions handy/conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void TcpConn::handleRead(const TcpConnPtr& con) {
int rd = 0;
if (channel_->fd() >= 0) {
rd = readImp(channel_->fd(), input_.end(), input_.space());
trace("channel %lld fd %d readed %d bytes", channel_->id(), channel_->fd(), rd);
trace("channel %lld fd %d readed %d bytes", (long long)channel_->id(), channel_->fd(), rd);
}
if (rd == -1 && errno == EINTR) {
continue;
Expand Down Expand Up @@ -176,7 +176,7 @@ ssize_t TcpConn::isend(const char* buf, size_t len) {
size_t sended = 0;
while (len > sended) {
ssize_t wd = writeImp(channel_->fd(), buf + sended, len - sended);
trace("channel %lld fd %d write %ld bytes", channel_->id(), channel_->fd(), wd);
trace("channel %lld fd %d write %ld bytes", (long long)channel_->id(), channel_->fd(), wd);
if (wd > 0) {
sended += wd;
continue;
Expand Down
2 changes: 1 addition & 1 deletion handy/handy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void Channel::enableReadWrite(bool readable, bool writable) {

void Channel::close() {
if (fd_ >= 0) {
trace("close channel %lld fd %d", id_, fd_);
trace("close channel %ld fd %d", (long)id_, fd_);
::close(fd_);
fd_ = -1;
}
Expand Down
30 changes: 15 additions & 15 deletions handy/poller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void PollerPoll::addChannel(Channel* ch) {
liveChannels_[ch] = fds_.size();
fdChannels_[ch->fd()] = ch;
fds_.push_back(ev);
trace("adding channel %lld fd %d events %d poll %lld", ch->id(), ch->fd(), ev.events, id_);
trace("adding channel %lld fd %d events %d poll %lld", (long long)ch->id(), ch->fd(), ev.events, (long long)id_);
}

void PollerPoll::updateChannel(Channel* ch) {
Expand All @@ -27,11 +27,11 @@ void PollerPoll::updateChannel(Channel* ch) {
struct pollfd& ev = fds_[iter->second];
ev.events = ch->events();
trace("modifying channel %lld fd %d events read %d write %d epoll %lld",
ch->id(), ch->fd(), ev.events & POLLIN, ev.events & POLLOUT, id_);
(long long)ch->id(), ch->fd(), ev.events & POLLIN, ev.events & POLLOUT, (long long)id_);
}

void PollerPoll::removeChannel(Channel* ch) {
trace("deleting channel %lld fd %d epoll %lld", ch->id(), ch->fd(), id_);
trace("deleting channel %lld fd %d epoll %lld", (long long)ch->id(), ch->fd(), (long long)id_);
auto iter = liveChannels_.find(ch);
assert(iter != liveChannels_.end());
struct pollfd& ev = fds_[iter->second];
Expand All @@ -53,10 +53,10 @@ void PollerPoll::loop_once(int waitMs) {
assert(iter != fdChannels_.end());
Channel* ch = iter->second;
if (events & (kReadEvent | POLLERR)) {
trace("channel %lld fd %d handle read", ch->id(), ch->fd());
trace("channel %lld fd %d handle read", (long long)ch->id(), ch->fd());
ch->handleRead();
} else if (events & kWriteEvent) {
trace("channel %lld fd %d handle write", ch->id(), ch->fd());
trace("channel %lld fd %d handle write", (long long)ch->id(), ch->fd());
ch->handleWrite();
} else {
fatal("unexpected poll events");
Expand All @@ -66,7 +66,7 @@ void PollerPoll::loop_once(int waitMs) {
}

PollerPoll::~PollerPoll() {
info("destroying PollerPoll %lld", id_);
info("destroying PollerPoll %lld", (long long)id_);
for (auto ch: liveChannels_) {
ch.first->close();
}
Expand All @@ -83,7 +83,7 @@ void PollerEpoll::init() {

PollerEpoll::PollerEpoll(){
assert(POLLIN == EPOLLIN);
assert(POLLIN == EPOLLOUT);
assert(POLLOUT == EPOLLOUT);
assert(POLLERR == EPOLLERR);
epollfd_ = epoll_create1(EPOLL_CLOEXEC);
fatalif(epollfd_<0, "epoll_create error %d %s", errno, strerror(errno));
Expand All @@ -100,10 +100,10 @@ void PollerEpoll::addChannel(Channel* ch) {
memset(&ev, 0, sizeof(ev));
ev.events = ch->events();
ev.data.ptr = ch;
trace("adding channel %ld fd %d events %d epoll %d", ch->id(), ch->fd(), ev.events, epollfd_);
trace("adding channel %lld fd %d events %d epoll %d", (long long)ch->id(), ch->fd(), ev.events, epollfd_);
int r = epoll_ctl(epollfd_, EPOLL_CTL_ADD, ch->fd(), &ev);
fatalif(r, "epoll_ctl add failed %d %s", errno, strerror(errno));
liveChannels_.push_front(ch);
liveChannels_.insert(ch);
}

void PollerEpoll::updateChannel(Channel* ch) {
Expand All @@ -114,8 +114,8 @@ void PollerEpoll::updateChannel(Channel* ch) {
memset(&ev, 0, sizeof(ev));
ev.events = ch->events();
ev.data.ptr = ch;
trace("modifying channel %ld fd %d events read %d write %d epoll %d",
ch->id(), ch->fd(), ev.events & POLLIN, ev.events & POLLOUT, epollfd_);
trace("modifying channel %lld fd %d events read %d write %d epoll %d",
(long long)ch->id(), ch->fd(), ev.events & POLLIN, ev.events & POLLOUT, epollfd_);
int r = epoll_ctl(epollfd_, EPOLL_CTL_MOD, ch->fd(), &ev);
fatalif(r, "epoll_ctl mod failed %d %s", errno, strerror(errno));
}
Expand All @@ -128,7 +128,7 @@ void PollerEpoll::removeChannel(Channel* ch) {
break;
}
}
trace("deleting channel %ld fd %d epoll %d", ch->id(), ch->fd(), epollfd_);
trace("deleting channel %lld fd %d epoll %d", (long long)ch->id(), ch->fd(), epollfd_);
if (ch->fd() < 0) {
return;
}
Expand All @@ -137,17 +137,17 @@ void PollerEpoll::removeChannel(Channel* ch) {
}

void PollerEpoll::loop_once(int waitMs) {
lastActive_ = epoll_wait(epollfd_, activeEvs_, kMaxEvents, wait);
lastActive_ = epoll_wait(epollfd_, activeEvs_, kMaxEvents, waitMs);
while (--lastActive_ >= 0) {
int i = lastActive_;
Channel* ch = (Channel*)activeEvs_[i].data.ptr;
int events = activeEvs_[i].events;
if (ch) {
if (events & (kReadEvent | POLLERR)) {
trace("channel %ld fd %d handle read", ch->id(), ch->fd());
trace("channel %lld fd %d handle read", (long long)ch->id(), ch->fd());
ch->handleRead();
} else if (events & kWriteEvent) {
trace("channel %ld fd %d handle write", ch->id(), ch->fd());
trace("channel %lld fd %d handle write", (long long)ch->id(), ch->fd());
ch->handleWrite();
} else {
fatal("unexpected epoll events");
Expand Down
2 changes: 1 addition & 1 deletion handy/poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct PollerBase {
virtual void removeChannel(Channel* ch) = 0;
virtual void updateChannel(Channel* ch) = 0;
virtual void loop_once(int waitMs) = 0;
virtual ~PollerBase() = 0;
virtual ~PollerBase(){};
};

struct PollerPoll : PollerBase {
Expand Down
1 change: 1 addition & 0 deletions handy/port_posix.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "port_posix.h"
#include <netdb.h>
#include <string.h>

namespace handy {
namespace port{
Expand Down
12 changes: 6 additions & 6 deletions handy/port_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
namespace handy {
namespace port {
static const int kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
uint16_t htobe(uint16_t v) {
inline uint16_t htobe(uint16_t v) {
if (!kLittleEndian) {
return v;
}
unsigned char* pv = (unsigned char*)&v;
return uint16_t(pv[0])<<8 | uint16_t(pv[1]);
}
uint32_t htobe(uint32_t v) {
inline uint32_t htobe(uint32_t v) {
if (!kLittleEndian) {
return v;
}
unsigned char* pv = (unsigned char*)&v;
return uint32_t(pv[0])<<24 | uint32_t(pv[1])<<16 | uint32_t(pv[2])<<8 | uint32_t(pv[3]);
}
uint64_t htobe(uint64_t v) {
inline uint64_t htobe(uint64_t v) {
if (!kLittleEndian) {
return v;
}
unsigned char* pv = (unsigned char*)&v;
return uint64_t(pv[0])<<56 | uint64_t(pv[1])<<48 | uint64_t(pv[2])<<40 | uint64_t(pv[3])<<32
| uint64_t(pv[4])<<24 | uint64_t(pv[5])<<16 | uint64_t(pv[6])<<8 | uint64_t(pv[7]);
}
int16_t htobe(int16_t v) { return (int16_t)htobe((uint16_t)v); }
int32_t htobe(int32_t v) { return (int32_t)htobe((uint32_t)v); }
int64_t htobe(int64_t v) { return (int64_t)htobe((uint64_t)v); }
inline int16_t htobe(int16_t v) { return (int16_t)htobe((uint16_t)v); }
inline int32_t htobe(int32_t v) { return (int32_t)htobe((uint32_t)v); }
inline int64_t htobe(int64_t v) { return (int64_t)htobe((uint64_t)v); }
struct in_addr getHostByName(const std::string& host);
}
}
35 changes: 35 additions & 0 deletions test/conf.ut.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <handy/conf.h>
#include <string.h>
#include "test_harness.h"

using namespace std;
using namespace handy;

int dump_ini(const char* dir, const char* inifile) {
Conf conf;
char buf[4096];
snprintf(buf, sizeof buf, "%s/%s", dir, inifile);
int err = conf.parse(buf);
if (err) {
//printf("parse error in %s err: %d\n", inifile, err);
return err;
}
//printf("file %s:\n", inifile);
//for (auto& kv : conf.values_) {
// for(auto& v : kv.second) {
// printf("%s=%s\n", kv.first.c_str(), v.c_str());
// }
//}
return 0;
}

TEST(test::TestBase, allFiles) {
const char* dir = "./";
ASSERT_EQ(1, dump_ini(dir, "files/bad_comment.ini"));
ASSERT_EQ(1, dump_ini(dir, "files/bad_multi.ini"));
ASSERT_EQ(3, dump_ini(dir, "files/bad_section.ini"));
ASSERT_EQ(0, dump_ini(dir, "files/multi_line.ini"));
ASSERT_EQ(0, dump_ini(dir, "files/normal.ini"));
ASSERT_EQ(0, dump_ini(dir, "files/user_error.ini"));
}

1 change: 1 addition & 0 deletions test/files/bad_comment.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is an error
1 change: 1 addition & 0 deletions test/files/bad_multi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
indented
5 changes: 5 additions & 0 deletions test/files/bad_section.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[section1]
name1=value1
[section2
[section3 ; comment ]
name2=value2
15 changes: 15 additions & 0 deletions test/files/multi_line.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[section1]
single1 = abc
multi = this is a
multi-line value
single2 = xyz
[section2]
multi = a
b
c
[section3]
single: ghi
multi: the quick
brown fox
name = bob smith ; comment line 1
; comment line 2
25 changes: 25 additions & 0 deletions test/files/normal.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; This is an INI file
[section1] ; section comment
one=This is a test ; name=value comment
two = 1234
; x=y

[ section 2 ]
happy = 4
sad =

[empty]
; do nothing

[comment_test]
test1 = 1;2;3 ; only this will be a comment
test2 = 2;3;4;this won't be a comment, needs whitespace before ';'
test;3 = 345 ; key should be "test;3"
test4 = 4#5#6 ; '#' only starts a comment at start of line
#test5 = 567 ; entire line commented
# test6 = 678 ; entire line commented, except in MULTILINE mode

[colon_tests]
Content-Type: text/html
foo:bar
adams : 42
4 changes: 4 additions & 0 deletions test/files/user_error.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[section]
a = b
user = parse_error
c = d
71 changes: 71 additions & 0 deletions test/handy.ut.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <handy/conn.h>
#include <handy/logging.h>
#include "test_harness.h"
#include <thread>

using namespace std;
using namespace handy;

TEST(test::TestBase, Ip4Addr) {
ASSERT_EQ("127.0.0.1:80", Ip4Addr("localhost", 80).toString());
ASSERT_EQ(true, Ip4Addr("www.baidu.com", 80).isIpValid());
ASSERT_EQ(Ip4Addr("127...", 80).isIpValid(), false);
ASSERT_EQ(true, Ip4Addr("127.0.0.1", 80).isIpValid());
}

TEST(test::TestBase, EventBase) {
Logger::getLogger().setLogLevel(Logger::LDEBUG);
EventBase base;
base.safeCall([] {
info("task by base.addTask");
});
thread th([&]{ usleep(50000); info("base exit"); base.exit(); });
base.loop();
th.join();
}

TEST(test::TestBase, Timer) {
EventBase base;
long now = util::timeMilli();
info("adding timers ");
TimerId tid1 = base.runAt(now + 100, []{info("timer at 100");});
TimerId tid2 = base.runAfter(50, []{ info("timer after 50"); });
TimerId tid3 = base.runAfter(20, [] { info("timer interval 10");}, 10);
base.runAfter(120, [&] {
info("after 120 then cancel above");
base.cancel(tid1);
base.cancel(tid2);
base.cancel(tid3);
base.exit();
});
base.loop();
}

TEST(test::TestBase, TcpServer1) {
Logger::getLogger().setLogLevel(Logger::LDEBUG);
EventBase base;
ThreadPool th(2);
TcpServer delayEcho(&base, "", 99);
delayEcho.onConnRead(
[&th, &base](const TcpConnPtr& con) {
th.addTask([&base,con] {
usleep(200*1000);
info("in pool");
base.safeCall([con, &base]{
con->send(con->getInput());
base.exit();
});
});
con->close();
}
);
TcpConnPtr con = TcpConn::createConnection(&base, "localhost", 99);
con->onState([](const TcpConnPtr& con) {
if (con->getState() == TcpConn::Connected)
con->send("hello");
});
base.loop();
th.exit();
th.join();
Logger::getLogger().setLogLevel(Logger::LINFO);
}
Loading

0 comments on commit 1f95f0e

Please sign in to comment.