~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/core/SolrCoreTest.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.solr.core;
19
 
 
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;
29
 
 
30
 
import java.util.concurrent.*;
31
 
import java.util.*;
32
 
public class SolrCoreTest extends SolrTestCaseJ4 {
33
 
  @Override
34
 
  public void setUp() throws Exception {
35
 
    super.setUp();
36
 
    initCore("solrconfig.xml", "schema.xml");
37
 
  }
38
 
 
39
 
  @Override
40
 
  public void tearDown() throws Exception {
41
 
    deleteCore();
42
 
    super.tearDown();
43
 
  }
44
 
 
45
 
  @Test
46
 
  public void testRequestHandlerRegistry() {
47
 
    SolrCore core = h.getCore();
48
 
 
49
 
    EmptyRequestHandler handler1 = new EmptyRequestHandler();
50
 
    EmptyRequestHandler handler2 = new EmptyRequestHandler();
51
 
 
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 );
59
 
  }
60
 
 
61
 
  @Test
62
 
  public void testClose() throws Exception {
63
 
    final CoreContainer cores = h.getCoreContainer();
64
 
    SolrCore core = cores.getCore("");
65
 
 
66
 
    ClosingRequestHandler handler1 = new ClosingRequestHandler();
67
 
    handler1.inform( core );
68
 
 
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 );
73
 
    core.close();
74
 
    cores.shutdown();
75
 
    assertTrue("Handler not closed", handler1.closed == true);
76
 
  }
77
 
  
78
 
  @Test
79
 
  public void testRefCount() throws Exception {
80
 
    SolrCore core = h.getCore();
81
 
    assertTrue("Refcount != 1", core.getOpenCount() == 1);
82
 
    
83
 
    final CoreContainer cores = h.getCoreContainer();
84
 
    SolrCore c1 = cores.getCore("");
85
 
    assertTrue("Refcount != 2", core.getOpenCount() == 2);
86
 
 
87
 
    ClosingRequestHandler handler1 = new ClosingRequestHandler();
88
 
    handler1.inform( core );
89
 
 
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 );
94
 
   
95
 
    SolrCore c2 = cores.getCore("");
96
 
    c1.close();
97
 
    assertTrue("Refcount < 1", core.getOpenCount() >= 1);
98
 
    assertTrue("Handler is closed", handler1.closed == false);
99
 
    
100
 
    c1 = cores.getCore("");
101
 
    assertTrue("Refcount < 2", core.getOpenCount() >= 2);
102
 
    assertTrue("Handler is closed", handler1.closed == false);
103
 
    
104
 
    c2.close();
105
 
    assertTrue("Refcount < 1", core.getOpenCount() >= 1);
106
 
    assertTrue("Handler is closed", handler1.closed == false);
107
 
 
108
 
    c1.close();
109
 
    cores.shutdown();
110
 
    assertTrue("Refcount != 0", core.getOpenCount() == 0);
111
 
    assertTrue("Handler not closed", core.isClosed() && handler1.closed == true);
112
 
  }
113
 
    
114
 
 
115
 
  @Test
116
 
  public void testRefCountMT() throws Exception {
117
 
    SolrCore core = h.getCore();
118
 
    assertTrue("Refcount != 1", core.getOpenCount() == 1);
119
 
 
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);
126
 
 
127
 
    final int LOOP = 100;
128
 
    final int MT = 16;
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>() {
134
 
        void yield(int n) {
135
 
          try {
136
 
            Thread.sleep(0, (n % 13 + 1) * 10);
137
 
          } catch (InterruptedException xint) {
138
 
          }
139
 
        }
140
 
        
141
 
        public Integer call() {
142
 
          SolrCore core = null;
143
 
          int r = 0;
144
 
          try {
145
 
            for (int l = 0; l < LOOP; ++l) {
146
 
              r += 1;
147
 
              core = cores.getCore("");
148
 
              // sprinkle concurrency hinting...
149
 
              yield(l);
150
 
              assertTrue("Refcount < 1", core.getOpenCount() >= 1);              
151
 
              yield(l);
152
 
              assertTrue("Refcount > 17", core.getOpenCount() <= 17);             
153
 
              yield(l);
154
 
              assertTrue("Handler is closed", handler1.closed == false);
155
 
              yield(l);
156
 
              core.close();
157
 
              core = null;
158
 
              yield(l);
159
 
            }
160
 
            return r;
161
 
          } finally {
162
 
            if (core != null)
163
 
              core.close();
164
 
          }
165
 
        }
166
 
      };
167
 
      callees.add(call);
168
 
    }
169
 
 
170
 
    List<Future<Integer>> results = service.invokeAll(callees);
171
 
    for (Future<Integer> result : results) {
172
 
      assertTrue("loop=" + result.get() +" < " + LOOP, result.get() >= LOOP);
173
 
    }
174
 
    
175
 
    cores.shutdown();
176
 
    assertTrue("Refcount != 0", core.getOpenCount() == 0);
177
 
    assertTrue("Handler not closed", core.isClosed() && handler1.closed == true);
178
 
    
179
 
    service.shutdown();
180
 
    assertTrue("Running for too long...", service.awaitTermination(60, TimeUnit.SECONDS));
181
 
  }
182
 
 
183
 
  @Test
184
 
  public void testInfoRegistry() throws Exception {
185
 
    //TEst that SolrInfoMBeans are registered, including SearchComponents
186
 
    SolrCore core = h.getCore();
187
 
 
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);
193
 
    //try a default one
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);
199
 
  }
200
 
 
201
 
}
202
 
 
203
 
 
204
 
 
205
 
class ClosingRequestHandler extends EmptyRequestHandler implements SolrCoreAware {
206
 
  boolean closed = false;
207
 
 
208
 
  public void inform(SolrCore core) {
209
 
    core.addCloseHook( new CloseHook() {
210
 
      @Override
211
 
      public void preClose(SolrCore core) {
212
 
        closed = true;
213
 
      }
214
 
 
215
 
      @Override
216
 
      public void postClose(SolrCore core) {}
217
 
    });
218
 
  }
219
 
}
220
 
 
221
 
/**
222
 
 * An empty handler for testing
223
 
 */
224
 
class EmptyRequestHandler extends RequestHandlerBase
225
 
{
226
 
  @Override
227
 
  public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
228
 
    // nothing!
229
 
  }
230
 
 
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; }
235
 
}