|
3 | 3 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. |
4 | 4 |
|
5 | 5 | import datetime |
6 | | -import json |
7 | 6 | import http.server |
| 7 | +import json |
8 | 8 | import threading |
9 | 9 | from contextlib import contextmanager |
10 | 10 | from copy import deepcopy |
|
13 | 13 |
|
14 | 14 | from socorro import settings |
15 | 15 | from socorro.external.es import search_common |
16 | | -from socorro.external.es.supersearch import SuperSearch |
17 | 16 | from socorro.external.es.super_search_fields import FIELDS |
| 17 | +from socorro.external.es.supersearch import SuperSearch |
18 | 18 | from socorro.lib import BadArgumentError, libdatetime |
19 | | - |
20 | | -from socorro.libclass import build_instance |
21 | 19 | from socorro.lib.libdatetime import utc_now |
22 | 20 | from socorro.lib.libooid import create_new_ooid |
| 21 | +from socorro.libclass import build_instance |
23 | 22 |
|
24 | 23 |
|
25 | 24 | @contextmanager |
26 | | -def mock_es_server(ip, port, post_response): |
| 25 | +def mock_es_server(ip, port, post_response, status_code=200, delay=0): |
27 | 26 | class MockHTTPRequestHandler(http.server.BaseHTTPRequestHandler): |
28 | 27 | """mock request handler""" |
29 | 28 |
|
30 | 29 | def do_POST(self): # pylint: disable=invalid-name |
31 | 30 | """Handle GET requests""" |
32 | | - self.send_response(200) |
| 31 | + if delay: |
| 32 | + import time |
| 33 | + |
| 34 | + time.sleep(delay) |
| 35 | + self.send_response(status_code) |
33 | 36 | self.send_header("Content-Type", "application/json") |
34 | 37 | self.send_header("X-Elastic-Product", "Elasticsearch") |
35 | 38 | self.end_headers() |
@@ -1884,3 +1887,54 @@ def test_get_with_failing_shards(self, es_helper): |
1884 | 1887 | {"type": "shards", "index": "other_index", "shards_count": 1}, |
1885 | 1888 | ] |
1886 | 1889 | assert res["errors"] == errors_exp |
| 1890 | + |
| 1891 | + def test_get_with_too_complex_query(self, es_helper): |
| 1892 | + ip, port = "127.0.0.1", 9998 |
| 1893 | + too_complex_response = { |
| 1894 | + "error": { |
| 1895 | + "root_cause": [ |
| 1896 | + { |
| 1897 | + "type": "too_complex_to_determinize_exception", |
| 1898 | + "reason": ( |
| 1899 | + "Determinizing automaton with 501 states and 125250 " |
| 1900 | + "transitions would require more than 10000 effort." |
| 1901 | + ), |
| 1902 | + } |
| 1903 | + ], |
| 1904 | + "type": "search_phase_execution_exception", |
| 1905 | + "reason": "all shards failed", |
| 1906 | + "phase": "query", |
| 1907 | + "grouped": True, |
| 1908 | + "failed_shards": [], |
| 1909 | + "caused_by": { |
| 1910 | + "type": "too_complex_to_determinize_exception", |
| 1911 | + "reason": ( |
| 1912 | + "Determinizing automaton with 501 states and 125250 " |
| 1913 | + "transitions would require more than 10000 effort." |
| 1914 | + ), |
| 1915 | + }, |
| 1916 | + }, |
| 1917 | + "status": 400, |
| 1918 | + } |
| 1919 | + with settings.override(**{"ES_STORAGE.options.url": f"http://{ip}:{port}"}): |
| 1920 | + crashstorage = self.build_crashstorage() |
| 1921 | + api = SuperSearchWithFields(crashstorage=crashstorage) |
| 1922 | + |
| 1923 | + with mock_es_server(ip, port, too_complex_response, status_code=400): |
| 1924 | + with pytest.raises(BadArgumentError): |
| 1925 | + api.get(signature="@a?a?a?a?a?") |
| 1926 | + |
| 1927 | + def test_get_with_connection_timeout(self, es_helper): |
| 1928 | + ip, port = "127.0.0.1", 9999 |
| 1929 | + with settings.override( |
| 1930 | + **{ |
| 1931 | + "ES_STORAGE.options.url": f"http://{ip}:{port}", |
| 1932 | + "ES_STORAGE.options.timeout": 0.05, |
| 1933 | + } |
| 1934 | + ): |
| 1935 | + crashstorage = self.build_crashstorage() |
| 1936 | + api = SuperSearchWithFields(crashstorage=crashstorage) |
| 1937 | + |
| 1938 | + with mock_es_server(ip, port, {}, delay=0.2): |
| 1939 | + with pytest.raises(BadArgumentError): |
| 1940 | + api.get(signature="@a?a?a?a?a?") |
0 commit comments