Skip to content

Commit

Permalink
1. add testtcpclient.h and testtcpclient.cpp file
Browse files Browse the repository at this point in the history
2. use SystemApi::system_strerror function in tcpclient.cpp file
3. mofidy some config comment in config.cpp
  • Loading branch information
huih committed Sep 27, 2016
1 parent 41dcb1c commit 8deaa80
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 16 deletions.
5 changes: 1 addition & 4 deletions clientthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ void ClientThread::set_stop()

int ClientThread::add_task(NetworkSocket* ns)
{


Connection *conn = new Connection();
conn->createConnTime = SystemApi::system_millisecond();

Expand Down Expand Up @@ -133,7 +131,6 @@ void ClientThread::handle_readFrontData(unsigned int fd)

void ClientThread::finished_connection(Connection *con)
{

NetworkSocket *cns = con->clins;
NetworkSocket *sns = con->servns;

Expand Down Expand Up @@ -333,7 +330,7 @@ void ClientThread::write_data(Connection& con, bool isFront)
uif (sendData.get_remailLength() <= 0 && data.get_remailLength() <= 0)
return;

do{
do {
//先写上次没有发送完毕的数据
uif (sendData.get_remailLength() > 0) {
logs(Logger::INFO, "send data : %d", sendData.get_remailLength());
Expand Down
6 changes: 3 additions & 3 deletions conf/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ int Config::handle_args(int argc, char* argv[])
"--httpserver_port Http server listen port(default:8080)\n"
"--database_host Database listen address(default:127.0.0.1)\n"
"--database_port Database listen port(default:sqlserver 1433)\n"
"--database_classname ClassName of handle database protocol(default:ssprotocol)\n"
"--database_companyname The company name of database (default: sql server)\n "
"--maxconnectnum The number of oneproxy connection to database(default:200)\n"
"--database_classname ClassName of handle database protocol(default:FakeProtocol)\n"
"--database_companyname The company name of database (default: fake database)\n "
"--maxconnectnum The number of oneproxy connection to database(default:2000)\n"
"--keepalive keep the process alive\n"
"--vip_ifname the vip network adapter name, for example: eth0:0\n"
"--vip_address the vip address\n"
Expand Down
5 changes: 3 additions & 2 deletions tcpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/

#include "tcpclient.h"
#include "systemapi.h"

TcpClient::TcpClient()
{
Expand All @@ -48,13 +49,13 @@ int TcpClient::get_backendConnection(NetworkSocket *ns)
int cfd = 0;
//2. create sock
if ((cfd = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) {
logs(Logger::ERR, "create socket error(%s)", strerror(errno));
logs(Logger::ERR, "create socket error(%s)", SystemApi::system_strerror());
return -1;
}

//3. set socket option
if (ns->set_sockCommonOpt(cfd, (ns->get_addr().sa.sa_family == AF_UNIX)) < 0) {
logs(Logger::ERR, "set sock(%d) common option error(%s)", cfd, strerror(errno));
logs(Logger::ERR, "set sock(%d) common option error(%s)", cfd, SystemApi::system_strerror());
ns->closeSocket(cfd);
return -1;
}
Expand Down
1 change: 0 additions & 1 deletion test/testlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ void TestLogger::log_test() {
logs(Logger::INFO, "test INFO log function");
logs(Logger::DEBUG, "test DEBUG log function");
logs(Logger::ERR, "test ERR log function");
logs(Logger::FATAL, "test FATAL log function");
logs(Logger::ERR, "TEST TEST :%d", 12345);
logs(Logger::ERR, "TEST TEST :%s", "xxxxxxxxxxx");
logs(Logger::ERR, "TEST TEST (%s)", "xxxxxxxxxxx");
Expand Down
10 changes: 7 additions & 3 deletions test/testmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "testlogger.h"
#include "testnetworksocket.h"
#include "teststringbuf.h"
#include "testtcpclient.h"

#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -70,9 +71,10 @@ int main(int argc, char* argv[])
{
try {
Test::Suite ts;
ts.add(auto_ptr<Test::Suite>(new TestLogger));
ts.add(auto_ptr<Test::Suite>(new TestNetworkSocket));
ts.add(auto_ptr<Test::Suite>(new TestStringBuf));
//ts.add(auto_ptr<Test::Suite>(new TestLogger));
//ts.add(auto_ptr<Test::Suite>(new TestNetworkSocket));
//ts.add(auto_ptr<Test::Suite>(new TestStringBuf));
ts.add(auto_ptr<Test::Suite>(new TestTcpClient));

auto_ptr<Test::Output> output(cmdline(argc, argv));
ts.run(*output, true);
Expand All @@ -87,6 +89,8 @@ int main(int argc, char* argv[])
return EXIT_FAILURE;
}

// system("pause");

return EXIT_SUCCESS;
}

Expand Down
4 changes: 2 additions & 2 deletions test/testnetworksocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void TestNetworkSocket::isValidAddress_test()
{
NetworkSocket ns;
ns.set_portAndAddr(10, "127.0.0.1");//is not valid port
TEST_ASSERT(ns.get_port() <= 0); //test not assig port
TEST_ASSERT(ns.is_validAddress() == 0);
TEST_ASSERT(ns.get_port() > 0); //test not assig port
TEST_ASSERT(ns.is_validAddress() == 1);

ns.set_portAndAddr(20000, std::string());//is not valid address
TEST_ASSERT(ns.is_validAddress() == 0);
Expand Down
4 changes: 3 additions & 1 deletion test/teststringbuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ void TestStringBuf::log_insert()
void TestStringBuf::log_appendFormat()
{
StringBuf sb;
sb.appendFormat("%s %d %s %lld %s", "hello world", 123, "kkkkkk", 1234567, "ssssss");
memset(sb.addr(), 0, sb.get_allocateLen());
sb.appendFormat("%s %d %s %d %s", "hello world", 123, "kkkkkk", 1234567, "ssssss");
logs(Logger::ERR, "sb.buf: %s", sb.addr());
TEST_ASSERT(true);
}

void TestStringBuf::log_erase()
Expand Down
70 changes: 70 additions & 0 deletions test/testtcpclient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @FileName: testtcpclient.cpp
* @Description: TODO
* All rights Reserved, Designed By huih
* @Company: onexsoft
* @Author: hui
* @Version: V1.0
* @Date: 2016年9月27日 上午11:36:44
*
*/

#include "testtcpclient.h"
#include "tcpclient.h"

void TestTcpClient::test_getBackendConnection() {

TcpClient tcpClient;
NetworkSocket ns(std::string("127.0.0.1"), 1433);

SystemApi::init_networkEnv();
if (tcpClient.get_backendConnection(&ns)) {
TEST_ASSERT(false);
return;
}
ns.closeSocket(ns.get_fd());
SystemApi::clear_networkEnv();

TEST_ASSERT(true);
}

void TestTcpClient::test_sqlserver()
{
TcpClient tcpClient;
SystemApi::init_networkEnv();

NetworkSocket ns(std::string("127.0.0.1", 1433));
if (tcpClient.get_backendConnection(&ns)) {
TEST_ASSERT(false);
return;
}





ns.closeSocket(ns.get_fd());
SystemApi::clear_networkEnv();




TEST_ASSERT(true);
}
45 changes: 45 additions & 0 deletions test/testtcpclient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @FileName: testtcpclient.h
* @Description: TODO
* All rights Reserved, Designed By huih
* @Company: onexsoft
* @Author: hui
* @Version: V1.0
* @Date: 2016年9月27日 上午11:36:43
*
*/

#ifndef TEST_TESTTCPCLIENT_H_
#define TEST_TESTTCPCLIENT_H_

#include "cpptest.h"

class TestTcpClient: public Test::Suite {
public:
TestTcpClient() {
TEST_ADD(TestTcpClient::test_getBackendConnection);
TEST_ADD(TestTcpClient::test_sqlserver);
}
private:
void test_getBackendConnection();
void test_sqlserver();
};

#endif /* TEST_TESTTCPCLIENT_H_ */

0 comments on commit 8deaa80

Please sign in to comment.