2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.solr.core;
20
import org.apache.solr.SolrTestCaseJ4;
21
import org.apache.solr.handler.RequestHandlerBase;
22
import org.apache.solr.handler.component.SpellCheckComponent;
23
import org.apache.solr.handler.component.QueryComponent;
24
import org.apache.solr.request.SolrQueryRequest;
25
import org.apache.solr.request.SolrRequestHandler;
26
import org.apache.solr.response.SolrQueryResponse;
27
import org.apache.solr.util.plugin.SolrCoreAware;
28
import org.junit.Test;
30
import java.util.concurrent.*;
32
public class SolrCoreTest extends SolrTestCaseJ4 {
34
public void setUp() throws Exception {
36
initCore("solrconfig.xml", "schema.xml");
40
public void tearDown() throws Exception {
46
public void testRequestHandlerRegistry() {
47
SolrCore core = h.getCore();
49
EmptyRequestHandler handler1 = new EmptyRequestHandler();
50
EmptyRequestHandler handler2 = new EmptyRequestHandler();
52
String path = "/this/is A path /that won't be registered!";
53
SolrRequestHandler old = core.registerRequestHandler( path, handler1 );
54
assertNull( old ); // should not be anything...
55
assertEquals( core.getRequestHandlers().get( path ), handler1 );
56
old = core.registerRequestHandler( path, handler2 );
57
assertEquals( old, handler1 ); // should pop out the old one
58
assertEquals( core.getRequestHandlers().get( path ), handler2 );
62
public void testClose() throws Exception {
63
final CoreContainer cores = h.getCoreContainer();
64
SolrCore core = cores.getCore("");
66
ClosingRequestHandler handler1 = new ClosingRequestHandler();
67
handler1.inform( core );
69
String path = "/this/is A path /that won't be registered 2!!!!!!!!!!!";
70
SolrRequestHandler old = core.registerRequestHandler( path, handler1 );
71
assertNull( old ); // should not be anything...
72
assertEquals( core.getRequestHandlers().get( path ), handler1 );
75
assertTrue("Handler not closed", handler1.closed == true);
79
public void testRefCount() throws Exception {
80
SolrCore core = h.getCore();
81
assertTrue("Refcount != 1", core.getOpenCount() == 1);
83
final CoreContainer cores = h.getCoreContainer();
84
SolrCore c1 = cores.getCore("");
85
assertTrue("Refcount != 2", core.getOpenCount() == 2);
87
ClosingRequestHandler handler1 = new ClosingRequestHandler();
88
handler1.inform( core );
90
String path = "/this/is A path /that won't be registered!";
91
SolrRequestHandler old = core.registerRequestHandler( path, handler1 );
92
assertNull( old ); // should not be anything...
93
assertEquals( core.getRequestHandlers().get( path ), handler1 );
95
SolrCore c2 = cores.getCore("");
97
assertTrue("Refcount < 1", core.getOpenCount() >= 1);
98
assertTrue("Handler is closed", handler1.closed == false);
100
c1 = cores.getCore("");
101
assertTrue("Refcount < 2", core.getOpenCount() >= 2);
102
assertTrue("Handler is closed", handler1.closed == false);
105
assertTrue("Refcount < 1", core.getOpenCount() >= 1);
106
assertTrue("Handler is closed", handler1.closed == false);
110
assertTrue("Refcount != 0", core.getOpenCount() == 0);
111
assertTrue("Handler not closed", core.isClosed() && handler1.closed == true);
116
public void testRefCountMT() throws Exception {
117
SolrCore core = h.getCore();
118
assertTrue("Refcount != 1", core.getOpenCount() == 1);
120
final ClosingRequestHandler handler1 = new ClosingRequestHandler();
121
handler1.inform(core);
122
String path = "/this/is A path /that won't be registered!";
123
SolrRequestHandler old = core.registerRequestHandler(path, handler1);
124
assertNull(old); // should not be anything...
125
assertEquals(core.getRequestHandlers().get(path), handler1);
127
final int LOOP = 100;
129
ExecutorService service = Executors.newFixedThreadPool(MT);
130
List<Callable<Integer>> callees = new ArrayList<Callable<Integer>>(MT);
131
final CoreContainer cores = h.getCoreContainer();
132
for (int i = 0; i < MT; ++i) {
133
Callable<Integer> call = new Callable<Integer>() {
136
Thread.sleep(0, (n % 13 + 1) * 10);
137
} catch (InterruptedException xint) {
141
public Integer call() {
142
SolrCore core = null;
145
for (int l = 0; l < LOOP; ++l) {
147
core = cores.getCore("");
148
// sprinkle concurrency hinting...
150
assertTrue("Refcount < 1", core.getOpenCount() >= 1);
152
assertTrue("Refcount > 17", core.getOpenCount() <= 17);
154
assertTrue("Handler is closed", handler1.closed == false);
170
List<Future<Integer>> results = service.invokeAll(callees);
171
for (Future<Integer> result : results) {
172
assertTrue("loop=" + result.get() +" < " + LOOP, result.get() >= LOOP);
176
assertTrue("Refcount != 0", core.getOpenCount() == 0);
177
assertTrue("Handler not closed", core.isClosed() && handler1.closed == true);
180
assertTrue("Running for too long...", service.awaitTermination(60, TimeUnit.SECONDS));
184
public void testInfoRegistry() throws Exception {
185
//TEst that SolrInfoMBeans are registered, including SearchComponents
186
SolrCore core = h.getCore();
188
Map<String, SolrInfoMBean> infoRegistry = core.getInfoRegistry();
189
assertTrue("infoRegistry Size: " + infoRegistry.size() + " is not greater than: " + 0, infoRegistry.size() > 0);
190
//try out some that we know are in the config
191
SolrInfoMBean bean = infoRegistry.get(SpellCheckComponent.class.getName());
192
assertNotNull("bean not registered", bean);
194
bean = infoRegistry.get(QueryComponent.class.getName());
195
assertNotNull("bean not registered", bean);
196
//try a Req Handler, which are stored by name, not clas
197
bean = infoRegistry.get("standard");
198
assertNotNull("bean not registered", bean);
205
class ClosingRequestHandler extends EmptyRequestHandler implements SolrCoreAware {
206
boolean closed = false;
208
public void inform(SolrCore core) {
209
core.addCloseHook( new CloseHook() {
211
public void preClose(SolrCore core) {
216
public void postClose(SolrCore core) {}
222
* An empty handler for testing
224
class EmptyRequestHandler extends RequestHandlerBase
227
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
231
@Override public String getDescription() { return null; }
232
@Override public String getSource() { return null; }
233
@Override public String getSourceId() { return null; }
234
@Override public String getVersion() { return null; }