简体   繁体   English

测试Solr分布式组件

[英]Testing a Solr distributed component

I have developed a custom Solr search component for which I need to write unit tests. 我开发了一个自定义Solr搜索组件,我需要编写单元测试。 As I have seen in the code of other Solr components, writing unit tests in Solr is done by extending the SolrTestCaseJ4 class. 正如我在其他Solr组件的代码中看到的,在Solr中编写单元测试是通过扩展SolrTestCaseJ4类来完成的。 Unfortunately, SolrTestCaseJ4 doesn't deal with testing in a distributed setting, and my custom component works only in such a setting. 不幸的是, SolrTestCaseJ4不处理分布式设置中的测试,我的自定义组件仅在这样的设置中工作。 As a matter of fact, my component deliberately returns empty responses when not in a distributed setting. 事实上,我的组件在不在分布式设置中时故意返回空响应。

I'm trying to think of a way to use the BaseDistributedSearchTestCase class to test my component. 我试图想办法使用BaseDistributedSearchTestCase类来测试我的组件。 The problem with BaseDistributedSearchTestCase is that how it works won't solve my issue. BaseDistributedSearchTestCase的问题在于它的工作原理无法解决我的问题。 When using BaseDistributedSearchTestCase you define a single test method where you index all the documents and perform some queries. 使用BaseDistributedSearchTestCase时,您可以定义一个测试方法,在该方法中索引所有文档并执行一些查询。 Running the tests executes the requests both on a distributed setting and on a single core setting. 运行测试会在分布式设置和单核设置上执行请求。 It then compares the responses of each setting to verify their equality. 然后,它会比较每个设置的响应以验证它们的相等性。 I cannot explicitly assert anything in that flow. 我无法在该流程中明确断言任何内容。

How do I write unit tests for a Solr distributed component? 如何为Solr分布式组件编写单元测试?

In Solr 4.7 has been added a class, MiniSolrCloudCluster, that actually "deploys" locally (and if you want ram only or on a temp dir) a complete solr cluster, with zookeeper, shards and everything, for your tests. 在Solr 4.7中增加了一个类MiniSolrCloudCluster,它实际上在本地“部署”(如果你只想要ram或在temp目录上)一个完整的solr集群,包括zookeeper,shards和所有东西,供你的测试使用。

You can find the jira here : https://issues.apache.org/jira/browse/SOLR-5865 你可以在这里找到jira: https//issues.apache.org/jira/browse/SOLR-5865

I've successfully used it to perform tests on solr distributed components, taking example from Solr tests, as follows : 我已经成功地使用它来对solr分布式组件进行测试,以Solr测试为例,如下所示:

private static MiniSolrCloudCluster miniCluster;
    private static CloudSolrServer cloudSolrServer;

    @BeforeClass
    public static void setup() throws Exception {
        miniCluster = new MiniSolrCloudCluster(2, null, new File("src/main/solr/solr.xml"), null, null);
        uploadConfigToZk("src/main/solr/content/conf/", "content");

        // override settings in the solrconfig include
        System.setProperty("solr.tests.maxBufferedDocs", "100000");
        System.setProperty("solr.tests.maxIndexingThreads", "-1");
        System.setProperty("solr.tests.ramBufferSizeMB", "100");
        // use non-test classes so RandomizedRunner isn't necessary
        System.setProperty("solr.tests.mergeScheduler", "org.apache.lucene.index.ConcurrentMergeScheduler");
        System.setProperty("solr.directoryFactory", "solr.RAMDirectoryFactory");        

        cloudSolrServer = new CloudSolrServer(miniCluster.getZkServer().getZkAddress(), false);
        cloudSolrServer.setRequestWriter(new RequestWriter());
        cloudSolrServer.setParser(new XMLResponseParser());
        cloudSolrServer.setDefaultCollection("content");
        cloudSolrServer.setParallelUpdates(false);
        cloudSolrServer.connect();

        createCollection(cloudSolrServer, "content", 2, 1, "content");

    }

    protected static void uploadConfigToZk(String configDir, String configName) throws Exception {
        SolrZkClient zkClient = null;
        try {
            zkClient = new SolrZkClient(miniCluster.getZkServer().getZkAddress(), 10000, 45000, null);
            uploadConfigFileToZk(zkClient, configName, "solrconfig.xml", new File(configDir, "solrconfig.xml"));
            uploadConfigFileToZk(zkClient, configName, "schema.xml", new File(configDir, "schema.xml"));
            uploadConfigFileToZk(zkClient, configName, "stopwords_en.txt", new File(configDir, "stopwords_en.txt"));
            uploadConfigFileToZk(zkClient, configName, "stopwords_it.txt", new File(configDir, "stopwords_it.txt"));

            System.out.println(zkClient.getChildren(ZkController.CONFIGS_ZKNODE + "/" + configName, null, true));
        } finally {
            if (zkClient != null)
                zkClient.close();
        }
    }

    protected static void uploadConfigFileToZk(SolrZkClient zkClient, String configName, String nameInZk, File file) throws Exception {
        zkClient.makePath(ZkController.CONFIGS_ZKNODE + "/" + configName + "/" + nameInZk, file, false, true);
    }

    @AfterClass
    public static void shutDown() throws Exception {
        miniCluster.shutdown();
    }

    protected static NamedList createCollection(CloudSolrServer server, String name, int numShards, int replicationFactor, String configName) throws Exception {
        ModifiableSolrParams modParams = new ModifiableSolrParams();
        modParams.set(CoreAdminParams.ACTION, CollectionAction.CREATE.name());
        modParams.set("name", name);
        modParams.set("numShards", numShards);
        modParams.set("replicationFactor", replicationFactor);
        modParams.set("collection.configName", configName);
        QueryRequest request = new QueryRequest(modParams);
        request.setPath("/admin/collections");
        return server.request(request);
    }

    @Test
    public void test() throws Exception {
      // Do you stuff here using cloudSolrServer as a normal solrServer
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM