test_manager.cpp 2.5 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include "test_tool.h"
#include "test_manager.h"
#include "framework/manager.h"
#include "framework/service.h"

namespace baidu {
namespace paddle_serving {
namespace unittest {

using baidu::paddle_serving::predictor::Manager;
using baidu::paddle_serving::predictor::InferService;
using baidu::paddle_serving::predictor::ParallelInferService;
using baidu::paddle_serving::predictor::FLAGS_use_parallel_infer_service;
using baidu::paddle_serving::predictor::InferServiceManager;

struct ManagerItem {
    int a; 
    float b;

    int init(const comcfg::ConfigUnit& c) {
        return 0;
    }

    static const char* tag() {
        return "Item";
    }
};

TEST_F(TestManager, test_manager_instance) {
    ManagerItem* item = Manager<ManagerItem>::instance().create_item();
    EXPECT_FALSE(item == NULL);
    item->a = 1;
    item->b = 2.0;
}

TEST_F(TestManager, test_infer_service_create) {
    InferService seq;
    ParallelInferService par;

    FLAGS_use_parallel_infer_service = false;
    EXPECT_EQ(typeid(seq),
            typeid(*InferServiceManager::instance().create_item()));

    FLAGS_use_parallel_infer_service = true;
    EXPECT_EQ(typeid(par), 
            typeid(*InferServiceManager::instance().create_item()));
}

TEST_F(TestManager, test_conf_success) {
    const char* conf_content = 
        "[@Item]\n\
        name: item1\n\
        a:b\n\
        [@Item]\n\
        name: item2\n\
        c:d";

    AutoTempFile file(conf_content);

    typedef Manager<ManagerItem> mgr;
    EXPECT_EQ(mgr::instance().initialize("./", file.name()), 0);

    ManagerItem* item11 = mgr::instance().item("item1");
    ManagerItem* item12 = &mgr::instance()["item1"];
    EXPECT_EQ(item11, item12);

    ManagerItem* item21 = mgr::instance().item("item2");
    ManagerItem* item22 = &mgr::instance()["item2"];
    EXPECT_EQ(item21, item22);
}

TEST_F(TestManager, test_conf_success_item_not_found) {
    const char* conf_content = 
        "[@Item1]\n\
        name: item1\n\
        a:b\n\
        [@Item2]\n\
        name: item2\n\
        c:d";

    AutoTempFile file(conf_content);

    typedef Manager<ManagerItem> mgr;
    EXPECT_EQ(mgr::instance().initialize("./", file.name()), 0);
}

TEST_F(TestManager, test_conf_failed_name_not_found) {
    const char* conf_content = 
        "[@Item]\n\
        name2: item1\n\
        a:b\n\
        [@Item]\n\
        name: item2\n\
        c:d";

    AutoTempFile file(conf_content);

    typedef Manager<ManagerItem> mgr;
    EXPECT_EQ(mgr::instance().initialize("./", file.name()), -1);
}

}
}
}