github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/core/gtest_ir_warden.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #include "common/configuration.h"
    21  #include "common/version.h"
    22  #include "core/ir_warden.h"
    23  #include "fs/storage_holder.h"
    24  #include "fs/util.h"
    25  #include <boost/format.hpp>
    26  #include <gtest/gtest.h>
    27  
    28  TEST(test_core, ir_warden_instance_init) {
    29    auto &instance = neb::core::ir_warden::instance();
    30  }
    31  
    32  TEST(test_core, is_sync_already) {
    33    auto &instance = neb::core::ir_warden::instance();
    34    instance.on_timer();
    35    instance.wait_until_sync();
    36    EXPECT_EQ(instance.is_sync_already(), true);
    37  }
    38  
    39  std::string get_db_path_for_read() {
    40    std::string cur_path = neb::configuration::instance().nbre_root_dir();
    41    return neb::fs::join_path(cur_path, "test/data/read-data.db/");
    42  }
    43  
    44  std::string get_db_path_for_write() {
    45    std::string cur_path = neb::configuration::instance().nbre_root_dir();
    46    return neb::fs::join_path(cur_path, "test/data/write-data.db/");
    47  }
    48  
    49  std::string get_blockchain_path_for_read() {
    50    std::string cur_path = neb::configuration::instance().neb_db_dir();
    51    return cur_path;
    52  }
    53  
    54  typedef std::pair<std::string, neb::version> depend_t;
    55  void gen_ir(const std::string &name, const neb::version &v,
    56              neb::block_height_t height, const std::vector<depend_t> &depends,
    57              neb::fs::rocksdb_storage *rs) {
    58  
    59    nbre::NBREIR ir;
    60    ir.set_name(name);
    61    ir.set_version(v.data());
    62    ir.set_height(height);
    63    for (auto &dep : depends) {
    64      auto deps = ir.add_depends();
    65      deps->set_name(dep.first);
    66      deps->set_version(dep.second.data());
    67    }
    68  
    69    auto size = ir.ByteSizeLong();
    70    neb::bytes buf(size);
    71    ir.SerializeToArray((void *)buf.value(), buf.size());
    72  
    73    neb::fs::ir_manager_helper::update_ir_list(name, rs);
    74    neb::fs::ir_manager_helper::update_ir_versions(name, v.data(), rs);
    75    neb::fs::ir_manager_helper::deploy_ir(name, v.data(), buf, rs);
    76  }
    77  
    78  TEST(test_core, get_ir_by_name_version) {
    79  
    80    std::string name = "nr";
    81    neb::version v(0, 0, 1);
    82    neb::block_height_t height = 123;
    83    std::vector<depend_t> depends;
    84    auto rs = neb::fs::storage_holder::instance().nbre_db_ptr();
    85    gen_ir(name, v, height, depends, rs);
    86  
    87    auto &instance = neb::core::ir_warden::instance();
    88    auto nbreir_ptr = instance.get_ir_by_name_version(name, v.data());
    89  
    90    EXPECT_EQ(nbreir_ptr->name(), "nr");
    91    EXPECT_EQ(nbreir_ptr->version(), v.data());
    92    EXPECT_EQ(nbreir_ptr->height(), 123);
    93    EXPECT_EQ(nbreir_ptr->depends().size(), 0);
    94  }
    95  
    96  TEST(test_core, get_ir_by_name_height) {
    97  
    98    std::string name = "dip";
    99    neb::version v(1, 2, 3);
   100    neb::block_height_t height = 456;
   101    std::vector<depend_t> depends;
   102    depends.push_back(std::make_pair("nr", neb::version(0, 0, 1)));
   103    auto rs = neb::fs::storage_holder::instance().nbre_db_ptr();
   104    gen_ir(name, v, height, depends, rs);
   105  
   106    auto &instance = neb::core::ir_warden::instance();
   107    auto ret_ptr = instance.get_ir_by_name_height(name, height);
   108    auto ret = *ret_ptr;
   109    EXPECT_EQ(ret.size(), 2);
   110  
   111    auto it = ret.begin();
   112    auto nbre_ir_ptr = it;
   113    EXPECT_EQ(nbre_ir_ptr->name(), name);
   114    EXPECT_EQ(nbre_ir_ptr->version(), v.data());
   115    EXPECT_EQ(nbre_ir_ptr->height(), height);
   116    EXPECT_EQ(nbre_ir_ptr->depends_size(), 1);
   117  }
   118  
   119  TEST(test_core, ir_warden_instance_dealloc) {
   120    auto &instance = neb::core::ir_warden::instance();
   121    instance.release();
   122  }