Skip to content

Instantly share code, notes, and snippets.

@suma
Last active December 30, 2015 03:09
Show Gist options
  • Select an option

  • Save suma/7767099 to your computer and use it in GitHub Desktop.

Select an option

Save suma/7767099 to your computer and use it in GitHub Desktop.
gtest runner for jubatset
// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2013 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <cstdlib>
#include <iostream>
#include <gtest/gtest.h>
#include <jubatus/client/classifier_client.hpp>
#include "util.hpp"
using std::vector;
using jubatus::classifier::estimate_result;
using jubatus::classifier::labeled_datum;
using jubatus::classifier::client::classifier;
namespace {
vector<labeled_datum> make_labeled_data() {
jubatus::client::common::datum datum;
vector<labeled_datum> data;
data.push_back(labeled_datum("label", datum));
return data;
}
vector<jubatus::client::common::datum> make_data() {
vector<jubatus::client::common::datum> data;
return data;
}
}
TEST(classifier_test, get_config) {
classifier cli(host(), port(), cluster_name(), timeout());
ASSERT_NE("", cli.get_config());
}
TEST(classifier_test, get_status) {
classifier cli(host(), port(), cluster_name(), timeout());
std::map<std::string, std::map<std::string, std::string> >
res = cli.get_status();
}
TEST(classifier_test, save_load) {
classifier cli(host(), port(), cluster_name(), timeout());
ASSERT_TRUE(cli.save("classifier.save_test.model"));
ASSERT_TRUE(cli.load("classifier.save_test.model"));
}
TEST(classifier_test, train) {
classifier cli(host(), port(), cluster_name(), timeout());
cli.train(make_labeled_data());
}
TEST(classifier_test, classify) {
classifier cli(host(), port(), cluster_name(), timeout());
vector<vector<estimate_result> >
result = cli.classify(make_data());
ASSERT_EQ(0, result.size());
}
TEST(classifier_test, clear) {
classifier cli(host(), port(), cluster_name(), timeout());
ASSERT_TRUE(cli.clear());
}
# For gtest
env.param('TEST_PROGRAM', '/home/suma/repos/jubatest/cpp/build/classifier_test')
env.param('JUBATUS_SERVICE', 'classifier')
PYTHONPATH=lib bin/jubatest --config envdef.py --testcase gtest/
2013-12-04 15:17:44,235:jubatest[47356304427456]:main.py:46:main:INFO: starting test session
2013-12-04 15:17:44,381:jubatest[47356304427456]:unit.py:58:startTest:INFO: test started: test_gtest (test_gtest.ClientDistributedTest)
2013-12-04 15:17:45,317:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.get_config
2013-12-04 15:17:45,326:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.get_config
2013-12-04 15:17:45,326:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.get_status
2013-12-04 15:17:45,335:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.get_status
2013-12-04 15:17:45,335:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.save_load
2013-12-04 15:17:45,345:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.save_load
2013-12-04 15:17:45,345:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.train
2013-12-04 15:17:45,353:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.train
2013-12-04 15:17:45,353:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.classify
2013-12-04 15:17:45,359:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.classify
2013-12-04 15:17:45,360:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.clear
2013-12-04 15:17:45,367:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.clear
2013-12-04 15:17:46,194:jubatest[47356304427456]:unit.py:66:stopTest:INFO: test stopped: test_gtest (test_gtest.ClientDistributedTest)
2013-12-04 15:17:46,400:jubatest[47356304427456]:unit.py:58:startTest:INFO: test started: test_gtest (test_gtest.ClientStandaloneTest)
2013-12-04 15:17:46,549:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.get_config
2013-12-04 15:17:46,556:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.get_config
2013-12-04 15:17:46,556:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.get_status
2013-12-04 15:17:46,562:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.get_status
2013-12-04 15:17:46,562:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.save_load
2013-12-04 15:17:46,572:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.save_load
2013-12-04 15:17:46,573:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.train
2013-12-04 15:17:46,578:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.train
2013-12-04 15:17:46,578:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.classify
2013-12-04 15:17:46,583:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.classify
2013-12-04 15:17:46,584:jubatest[47356304427456]:test_gtest.py:48:test_gtest:INFO: gtest started: classifier_test.clear
2013-12-04 15:17:46,588:jubatest[47356304427456]:test_gtest.py:62:test_gtest:INFO: gtest finished [PASS] : classifier_test.clear
2013-12-04 15:17:47,503:jubatest[47356304427456]:unit.py:66:stopTest:INFO: test stopped: test_gtest (test_gtest.ClientStandaloneTest)
2013-12-04 15:17:47,504:jubatest[47356304427456]:main.py:68:main:INFO: completed test session
2013-12-04 15:17:47,504:jubatest[47356304427456]:main.py:77:main:INFO: generating report as text
===== TEST SUMMARY =====
Total: 2
( Error: 0 )
( Fail: 0 )
( Skip: 0 )
========================
----- Success -----
test_gtest (test_gtest.ClientDistributedTest)
test_gtest (test_gtest.ClientStandaloneTest)
----- Failure -----
----- Error -----
----- Skipped -----
2013-12-04 15:17:47,504:jubatest[47356304427456]:main.py:94:main:INFO: end
# -*- coding: utf-8 -*-
import os
import re
from cStringIO import StringIO
import time
from jubatest import *
from jubatest.unit import JubaSkipTest
from jubatest.remote import SyncRemoteProcess
from jubatest.process import LocalSubprocess
from jubatest.logger import log
class ClientGoogleTestBase():
@classmethod
def init_test(cls):
# Collect all test cases
args = [ cls.test_program, '--gtest_list_tests' ]
proc = LocalSubprocess(args)
proc.start()
returncode = proc.wait()
if returncode != 0:
raise JubaSkipTest('%s cannot list testcases' % cls.test_program)
# read input
stri = StringIO(proc.stdout)
cls.testcase = []
current_test = None
re_test = re.compile('^([a-zA-Z0-9_]+\.)')
re_testcase = re.compile('^ ([a-zA-Z0-9_]+)')
while True:
nl = stri.readline()
if nl == '': break
line = nl.strip('\n')
if line.find('Running main') != -1: continue
match = re_test.match(line)
if match:
current_test = match.group(1)
match = re_testcase.match(line)
if match and current_test:
cls.testcase.append('%s%s' % (current_test, match.group(1)))
def test_gtest(self):
for test in self.testcase:
test_result = 'PASS'
try:
log.info('gtest started: %s', test)
args = [ self.test_program, '--gtest_filter=%s' % test ]
env = { 'JUBATUS_HOST': self.client_node.get_host(),
'JUBATUS_PORT': str(self.target.get_host_port()[1]),
'JUBATUS_CLUSTER_NAME': self.name }
env.update(os.environ)
proc = LocalSubprocess(args, env)
proc.start()
returncode = proc.wait()
if returncode != 0:
test_result = 'FAIL'
# TODO: Report gtest result when error occured
print proc.stdout
finally:
log.info('gtest finished [%s] : %s', test_result, test)
class ClientStandaloneTest(JubaTestCase, ClientGoogleTestBase):
@classmethod
def setUpCluster(cls, env):
cls.env = env
cls.service = cls.env.get_param('JUBATUS_SERVICE')
if not cls.service:
raise JubaSkipTest('JUBATUS_SERVICE parameter is not set')
cls.test_program = cls.env.get_param('TEST_PROGRAM')
if not cls.test_program:
raise JubaSkipTest('TEST_PROGRAM parameter is not set')
cls.server1 = env.server_standalone(env.get_node(0), cls.service, default_config(cls.service))
cls.target = cls.server1
cls.name = ''
cls.client_node = env.get_node(0)
cls.init_test()
def setUp(self):
self.server1.start()
def tearDown(self):
self.server1.stop()
class ClientDistributedTest(JubaTestCase, ClientGoogleTestBase):
@classmethod
def setUpCluster(cls, env):
cls.env = env
cls.service = cls.env.get_param('JUBATUS_SERVICE')
if not cls.service:
raise JubaSkipTest('JUBATUS_SERVICE parameter is not set')
cls.test_program = cls.env.get_param('TEST_PROGRAM')
if not cls.test_program:
raise JubaSkipTest('TEST_PROGRAM parameter is not set')
cls.node0 = env.get_node(0)
cls.cluster = env.cluster(cls.service, default_config(cls.service))
cls.server1 = env.server(cls.node0, cls.cluster)
cls.server2 = env.server(cls.node0, cls.cluster)
cls.keeper1 = env.keeper(cls.node0, CLASSIFIER)
cls.target = cls.keeper1
cls.name = cls.cluster.name
cls.client_node = env.get_node(0)
cls.init_test()
def setUp(self):
for server in [self.server1, self.server2, self.keeper1]:
server.start()
def tearDown(self):
for server in [self.server1, self.server2, self.keeper1]:
server.stop()
// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2013 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <cstdlib>
#include <string>
#include <stdexcept>
namespace {
std::string host() {
return std::getenv("JUBATUS_HOST");
}
unsigned int port() {
char* str = std::getenv("JUBATUS_PORT");
if (!str) {
throw std::runtime_error("JUBATUS_PORT is unset");
}
return std::strtoul(str, NULL, 0);
}
std::string cluster_name() {
return std::getenv("JUBATUS_CLUSTER_NAME");
}
unsigned int timeout() {
char* str = std::getenv("TIMEOUT");
if (!str) {
return 5;
}
return std::strtoul(str, NULL, 0);
}
}
@kmaehashi
Copy link

 env = { 'JUBATUS_HOST': self.client_node.get_host(),
         'JUBAUTS_PORT': self.client_node.lease_port(),

lease_port すると新しいポートがプールからリースされてしまうので、self.target.get_host_port() で取得してください。

https://github.com/kmaehashi/jubatest/wiki/Reference-%28Draft%29-%28ja%29#jubaserverstandaloneserverproxy

ローカルプロセス化は、

stdout = self.client_node.run_process(args, env)

の代わりに

proc = LocalSubprocess(args, env)
proc.start()
proc.wait()
stdout = proc.stdout

にする感じです。

@suma
Copy link
Author

suma commented Dec 3, 2013

ありがとうございます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment