Up the socket timout, and catch timeouts in SearchClient.excerpt(). Also add

test that improves coverage of the SearchClient().query() socket exceptions.
This commit is contained in:
James Socol 2010-04-21 12:47:12 -07:00
Родитель 3109abe8ff
Коммит 97fe787bae
3 изменённых файлов: 38 добавлений и 14 удалений

Просмотреть файл

@ -92,14 +92,14 @@ class SearchClient(object):
try:
result = sc.Query(query, self.index)
except socket.timeout:
log.error("Query has timed out!")
raise SearchError("Query has timed out!")
log.error('Query has timed out!')
raise SearchError('Query has timed out!')
except socket.error, msg:
log.error("Query socket error: %s" % msg)
raise SearchError("Could not execute your search!")
log.error('Query socket error: %s' % msg)
raise SearchError('Could not execute your search!')
except Exception, e:
log.error("Sphinx threw an unknown exception: %s" % e)
raise SearchError("Sphinx threw an unknown exception!")
log.error('Sphinx threw an unknown exception: %s' % e)
raise SearchError('Sphinx threw an unknown exception!')
if result:
return result['matches']
@ -108,17 +108,25 @@ class SearchClient(object):
def excerpt(self, result, query):
"""
Returns an excerpt for the passed-in string
Given document content and a search query (both strings), uses
Sphinx to build an excerpt, highlighting the keywords from the
query.
Takes in a string
Length of the final excerpt is roughly determined by
SEARCH_SUMMARY_LENGTH in settings.py.
"""
documents = [result]
# build excerpts that are longer and truncate
# see multiplier constant definition for details
raw_excerpt = self.sphinx.BuildExcerpts(documents, self.index, query,
{'limit': settings.SEARCH_SUMMARY_LENGTH
* settings.SEARCH_SUMMARY_LENGTH_MULTIPLIER})[0]
try:
# build excerpts that are longer and truncate
# see multiplier constant definition for details
raw_excerpt = self.sphinx.BuildExcerpts(
documents, self.index, query,
{'limit': settings.SEARCH_SUMMARY_LENGTH
* settings.SEARCH_SUMMARY_LENGTH_MULTIPLIER})[0]
except socket.timeout:
log.error('Building excerpt timed out!')
raw_excerpt = ''
excerpt = smart_unicode(raw_excerpt)
for p in self.compiled_patterns:

Просмотреть файл

@ -20,7 +20,7 @@ import re
from struct import *
# Kitsune customizations
K_TIMEOUT = 1 # Socket timeout in seconds
K_TIMEOUT = 2 # Socket timeout in seconds
# known searchd commands
SEARCHD_COMMAND_SEARCH = 0

Просмотреть файл

@ -5,10 +5,12 @@ import os
import shutil
import time
import json
import socket
from django.test import client
from django.db import connection
import mock
from nose import SkipTest
from nose.tools import assert_raises
import test_utils
@ -345,3 +347,17 @@ def test_sphinx_down():
"""
wc = WikiClient()
assert_raises(SearchError, wc.query, 'test')
query = lambda *args, **kwargs: WikiClient().query(*args, **kwargs)
@mock.patch('search.clients.WikiClient')
def test_excerpt_timeout(sphinx_mock):
def sphinx_error(cls):
raise cls
sphinx_mock.query.side_effect = lambda *a: sphinx_error(socket.timeout)
assert_raises(SearchError, query, 'xxx')
sphinx_mock.query.side_effect = lambda *a: sphinx_error(Exception)
assert_raises(SearchError, query, 'xxx')