簡體   English   中英

測試Solr分布式組件

[英]Testing a Solr distributed component

我開發了一個自定義Solr搜索組件,我需要編寫單元測試。 正如我在其他Solr組件的代碼中看到的,在Solr中編寫單元測試是通過擴展SolrTestCaseJ4類來完成的。 不幸的是, SolrTestCaseJ4不處理分布式設置中的測試,我的自定義組件僅在這樣的設置中工作。 事實上,我的組件在不在分布式設置中時故意返回空響應。

我試圖想辦法使用BaseDistributedSearchTestCase類來測試我的組件。 BaseDistributedSearchTestCase的問題在於它的工作原理無法解決我的問題。 使用BaseDistributedSearchTestCase時,您可以定義一個測試方法,在該方法中索引所有文檔並執行一些查詢。 運行測試會在分布式設置和單核設置上執行請求。 然后,它會比較每個設置的響應以驗證它們的相等性。 我無法在該流程中明確斷言任何內容。

如何為Solr分布式組件編寫單元測試?

在Solr 4.7中增加了一個類MiniSolrCloudCluster,它實際上在本地“部署”(如果你只想要ram或在temp目錄上)一個完整的solr集群,包括zookeeper,shards和所有東西,供你的測試使用。

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

我已經成功地使用它來對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