【问题标题】:Testing a Solr distributed component测试 Solr 分布式组件
【发布时间】:2014-08-30 19:32:09
【问题描述】:

我开发了一个自定义 Solr 搜索组件,我需要为其编写单元测试。正如我在其他 Solr 组件的代码中看到的那样,在 Solr 中编写单元测试是通过扩展 SolrTestCaseJ4 类来完成的。不幸的是,SolrTestCaseJ4 不处理分布式设置中的测试,我的自定义组件只能在这样的设置中工作。事实上,我的组件在不在分布式设置中时故意返回空响应。

我正在想办法使用BaseDistributedSearchTestCase 类来测试我的组件。 BaseDistributedSearchTestCase 的问题在于它的工作方式无法解决我的问题。使用BaseDistributedSearchTestCase 时,您定义了一个测试方法,您可以在其中索引所有文档并执行一些查询。运行测试会在分布式设置和单核设置上执行请求。然后它比较每个设置的响应以验证它们的相等性。我无法在该流程中明确断言任何内容。

如何为 Solr 分布式组件编写单元测试?

【问题讨论】:

    标签: java unit-testing solr lucene


    【解决方案1】:

    在 Solr 4.7 中添加了一个类 MiniSolrCloudCluster,它实际上在本地“部署”(如果您只想要 ram 或在临时目录上)一个完整的 solr 集群,包括 zookeeper、分片和所有东西,用于您的测试。

    您可以在这里找到 jira:https://issues.apache.org/jira/browse/SOLR-5865

    我已经成功使用它对 solr 分布式组件进行了测试,以 Solr 测试为例,如下:

    私有静态 MiniSolrCloudCluster miniCluster; 私有静态 CloudSolrServer cloudSolrServer; @课前 公共静态无效设置()抛出异常{ miniCluster = new MiniSolrCloudCluster(2, null, new File("src/main/solr/solr.xml"), null, null); uploadConfigToZk("src/main/solr/content/conf/", "content"); // 覆盖 solrconfig 中的设置包括 System.setProperty("solr.tests.maxBufferedDocs", "100000"); System.setProperty("solr.tests.maxIndexingThreads", "-1"); System.setProperty("solr.tests.ramBufferSizeMB", "100"); // 使用非测试类,因此不需要 RandomizedRunner 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("内容"); cloudSolrServer.setParallelUpdates(false); cloudSolrServer.connect(); createCollection(cloudSolrServer, "内容", 2, 1, "内容"); } protected static void uploadConfigToZk(String configDir, String configName) 抛出异常 { SolrZkClient zkClient = null; 尝试 { 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)); } 最后 { if (zkClient != null) zkClient.close(); } } protected static void uploadConfigFileToZk(SolrZkClient zkClient, String configName, String nameInZk, File file) 抛出异常 { zkClient.makePath(ZkController.CONFIGS_ZKNODE + "/" + configName + "/" + nameInZk, file, false, true); } @下课以后 公共静态无效关闭()抛出异常{ 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("名称", 名称); modParams.set("numShards", numShards); modParams.set("replicationFactor", replicationFactor); modParams.set("collection.configName", configName); QueryRequest 请求 = 新 QueryRequest(modParams); request.setPath("/admin/collections"); 返回 server.request(request); } @测试 公共无效测试()抛出异常{ // 你在这里使用 cloudSolrServer 作为一个普通的 solrServer }

    【讨论】:

    • 优秀的帖子,但我相信这是 Solr 4.8.0 首次发布的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多