You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ji...@apache.org on 2024/11/26 20:18:05 UTC

(couchdb) branch main updated: Fix mango tests using custom db name (#5341)

This is an automated email from the ASF dual-hosted git repository.

jiahuili430 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/main by this push:
     new 7fe38c0ad Fix mango tests using custom db name (#5341)
7fe38c0ad is described below

commit 7fe38c0ad1cf5417a5241b6f63f5aa7c0b3a6733
Author: Jiahui Li <li...@gmail.com>
AuthorDate: Tue Nov 26 14:17:59 2024 -0600

    Fix mango tests using custom db name (#5341)
    
    * Add `-F, --failfast` to stop mango tests early on failures
    
    * Run mango tests with custom db instead of recreating class db
    
    When running mango tests, sometimes get
    `org.apache.lucene.store.NoSuchDirectoryException` error. This is because
    each test in the same class used the same database name, and in the setUp
    function always delete and recreate it, which caused a race condition.
    
    Adding `setUp()` and `teadown()` to create/delete the db for each test
    should solve the issue.
    
    * Replace `recreate()` with `super().setUp(db_per_test=True)`
---
 Makefile                                    |  2 +-
 Makefile.win                                |  2 +-
 src/mango/test/01-index-crud-test.py        |  4 +--
 src/mango/test/12-use-correct-index-test.py |  2 +-
 src/mango/test/13-stable-update-test.py     |  2 +-
 src/mango/test/14-json-pagination-test.py   |  2 +-
 src/mango/test/16-index-selectors-test.py   |  4 +--
 src/mango/test/17-multi-type-value-test.py  |  4 +--
 src/mango/test/18-json-sort.py              |  2 +-
 src/mango/test/19-find-conflicts.py         |  2 +-
 src/mango/test/20-no-timeout-test.py        |  2 +-
 src/mango/test/24-text-paginated-test.py    |  2 +-
 src/mango/test/25-beginswith-test.py        |  2 +-
 src/mango/test/mango.py                     | 49 ++++++++++++++++-------------
 14 files changed, 43 insertions(+), 38 deletions(-)

diff --git a/Makefile b/Makefile
index 231008014..a5aced923 100644
--- a/Makefile
+++ b/Makefile
@@ -339,7 +339,7 @@ mango-test: devclean all
 		--admin=adm:pass \
 		--no-eval "\
 COUCH_USER=adm COUCH_PASS=pass \
-src/mango/.venv/bin/nose2 -s src/mango/test -c src/mango/unittest.cfg $(MANGO_TEST_OPTS)"
+src/mango/.venv/bin/nose2 -F -s src/mango/test -c src/mango/unittest.cfg $(MANGO_TEST_OPTS)"
 
 
 .PHONY: weatherreport-test
diff --git a/Makefile.win b/Makefile.win
index c7d8bb000..9d811bf6e 100644
--- a/Makefile.win
+++ b/Makefile.win
@@ -310,7 +310,7 @@ mango-test: devclean all
 		--admin=adm:pass \
 		"\
 env COUCH_USER=adm COUCH_PASS=pass \
-src\mango\.venv\Scripts\nose2 -s src\mango\test -c src\mango\unittest.cfg $(MANGO_TEST_OPTS)"
+src\mango\.venv\Scripts\nose2 -F -s src\mango\test -c src\mango\unittest.cfg $(MANGO_TEST_OPTS)"
 
 
 ################################################################################
diff --git a/src/mango/test/01-index-crud-test.py b/src/mango/test/01-index-crud-test.py
index 4de777799..8084a6bf7 100644
--- a/src/mango/test/01-index-crud-test.py
+++ b/src/mango/test/01-index-crud-test.py
@@ -25,7 +25,7 @@ DOCS = [
 
 class IndexCrudTests(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
 
     def test_bad_fields(self):
         bad_fields = [
@@ -352,7 +352,7 @@ class IndexCrudTests(mango.DbPerClass):
 @unittest.skipUnless(mango.has_text_service(), "requires text service")
 class IndexCrudTextTests(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
 
     def test_create_text_idx(self):
         fields = [
diff --git a/src/mango/test/12-use-correct-index-test.py b/src/mango/test/12-use-correct-index-test.py
index c21ad6c5e..176835abf 100644
--- a/src/mango/test/12-use-correct-index-test.py
+++ b/src/mango/test/12-use-correct-index-test.py
@@ -50,7 +50,7 @@ DOCS = [
 
 class ChooseCorrectIndexForDocs(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOCS))
 
     def test_choose_index_with_one_field_in_index(self):
diff --git a/src/mango/test/13-stable-update-test.py b/src/mango/test/13-stable-update-test.py
index 303f3fab1..aebe62271 100644
--- a/src/mango/test/13-stable-update-test.py
+++ b/src/mango/test/13-stable-update-test.py
@@ -35,7 +35,7 @@ DOCS1 = [
 
 class SupportStableAndUpdate(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         # Hack to prevent auto-indexer from foiling update=False test
         # https://github.com/apache/couchdb/issues/2313
         self.db.save_doc(
diff --git a/src/mango/test/14-json-pagination-test.py b/src/mango/test/14-json-pagination-test.py
index 2d2430152..7b62f9c61 100644
--- a/src/mango/test/14-json-pagination-test.py
+++ b/src/mango/test/14-json-pagination-test.py
@@ -33,7 +33,7 @@ DOCS = [
 
 class PaginateJsonDocs(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOCS))
 
     def test_all_docs_paginate_to_end(self):
diff --git a/src/mango/test/16-index-selectors-test.py b/src/mango/test/16-index-selectors-test.py
index a30f601c4..d9cd058b3 100644
--- a/src/mango/test/16-index-selectors-test.py
+++ b/src/mango/test/16-index-selectors-test.py
@@ -82,7 +82,7 @@ oldschoolddoctext = {
 
 class IndexSelectorJson(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOCS))
 
     def test_saves_partial_filter_selector_in_index(self):
@@ -189,7 +189,7 @@ class IndexSelectorJson(mango.DbPerClass):
 @unittest.skipUnless(mango.has_text_service(), "requires text service")
 class IndexSelectorText(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOCS))
 
     def test_saves_partialfilterselector_in_index(self):
diff --git a/src/mango/test/17-multi-type-value-test.py b/src/mango/test/17-multi-type-value-test.py
index 21e7afda4..28cf8d954 100644
--- a/src/mango/test/17-multi-type-value-test.py
+++ b/src/mango/test/17-multi-type-value-test.py
@@ -52,7 +52,7 @@ class MultiValueFieldTests:
 
 class MultiValueFieldJSONTests(mango.DbPerClass, MultiValueFieldTests):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOCS))
         self.db.create_index(["name"])
         self.db.create_index(["age", "name"])
@@ -65,5 +65,5 @@ class MultiValueFieldJSONTests(mango.DbPerClass, MultiValueFieldTests):
 
 class MultiValueFieldAllDocsTests(mango.DbPerClass, MultiValueFieldTests):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOCS))
diff --git a/src/mango/test/18-json-sort.py b/src/mango/test/18-json-sort.py
index 21ecfefdb..ebc3b024e 100644
--- a/src/mango/test/18-json-sort.py
+++ b/src/mango/test/18-json-sort.py
@@ -25,7 +25,7 @@ DOCS = [
 
 class JSONIndexSortOptimisations(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOCS))
 
     def test_works_for_basic_case(self):
diff --git a/src/mango/test/19-find-conflicts.py b/src/mango/test/19-find-conflicts.py
index bf865d6ea..e0d2ef392 100644
--- a/src/mango/test/19-find-conflicts.py
+++ b/src/mango/test/19-find-conflicts.py
@@ -20,7 +20,7 @@ CONFLICT = [{"_id": "doc", "_rev": "1-23202479633c2b380f79507a776743d5", "a": 1}
 
 class ChooseCorrectIndexForDocs(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOC))
         self.db.save_docs_with_conflicts(copy.deepcopy(CONFLICT))
 
diff --git a/src/mango/test/20-no-timeout-test.py b/src/mango/test/20-no-timeout-test.py
index cffdfc335..051b9a56c 100644
--- a/src/mango/test/20-no-timeout-test.py
+++ b/src/mango/test/20-no-timeout-test.py
@@ -17,7 +17,7 @@ import unittest
 
 class LongRunningMangoTest(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         docs = []
         for i in range(100000):
             docs.append({"_id": str(i), "another": "field"})
diff --git a/src/mango/test/24-text-paginated-test.py b/src/mango/test/24-text-paginated-test.py
index 84b3a2f4b..bd420766c 100644
--- a/src/mango/test/24-text-paginated-test.py
+++ b/src/mango/test/24-text-paginated-test.py
@@ -24,7 +24,7 @@ class PaginatedResultsTest(mango.DbPerClass):
     UPDATES = 25
 
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.create_text_index(
             analyzer="keyword",
             default_field={},
diff --git a/src/mango/test/25-beginswith-test.py b/src/mango/test/25-beginswith-test.py
index df96560b4..071cc9a1f 100644
--- a/src/mango/test/25-beginswith-test.py
+++ b/src/mango/test/25-beginswith-test.py
@@ -28,7 +28,7 @@ def to_utf8_bytes(list):
 
 class BeginsWithOperator(mango.DbPerClass):
     def setUp(self):
-        self.db.recreate()
+        super().setUp(db_per_test=True)
         self.db.save_docs(copy.deepcopy(DOCS))
         self.db.create_index(["location"])
         self.db.create_index(["name", "location"])
diff --git a/src/mango/test/mango.py b/src/mango/test/mango.py
index a23ba3147..066b5a329 100644
--- a/src/mango/test/mango.py
+++ b/src/mango/test/mango.py
@@ -80,10 +80,7 @@ class Concurrently(object):
 
 
 class Database(object):
-    def __init__(
-        self,
-        dbname,
-    ):
+    def __init__(self, dbname):
         self.dbname = dbname
         self.sess = requests.session()
         self.sess.auth = (COUCH_USER, COUCH_PASS)
@@ -107,24 +104,19 @@ class Database(object):
 
     def delete(self):
         r = self.sess.delete(self.url)
+        r.raise_for_status()
 
     def recreate(self):
-        NUM_TRIES = 10
-
-        for k in range(NUM_TRIES):
-            r = self.sess.get(self.url)
-            if r.status_code == 200:
-                db_info = r.json()
-                docs = db_info["doc_count"] + db_info["doc_del_count"]
-                if docs == 0:
-                    # db exists and it is empty -- exit condition is met
-                    return
-                self.delete()
-            self.create()
-            time.sleep(k * 0.1)
-        raise Exception(
-            "Failed to recreate the database after {} tries".format(NUM_TRIES)
-        )
+        r = self.sess.get(self.url)
+        if r.status_code == 200:
+            db_info = r.json()
+            docs = db_info["doc_count"] + db_info["doc_del_count"]
+            if docs == 0:
+                # db is not in use, no need to recreate
+                return
+            self.delete()
+        self.create()
+        self.recreate()
 
     def save_doc(self, doc):
         self.save_docs([doc])
@@ -356,8 +348,21 @@ class DbPerClass(unittest.TestCase):
         if clean_up_dbs():
             klass.db.delete()
 
-    def setUp(self):
-        self.db = self.__class__.db
+    def setUp(self, db_per_test=False, partitioned=False):
+        if db_per_test:
+            self.db = Database(random_db_name())
+            self.db.create(q=1, n=1, partitioned=partitioned)
+            self.db_per_test = db_per_test
+        else:
+            self.db = self.__class__.db
+
+    def tearDown(self):
+        if (
+            hasattr(self, "db_per_test")
+            and self.__getattribute__("db_per_test")
+            and clean_up_dbs()
+        ):
+            self.db.delete()
 
 
 class UserDocsTests(DbPerClass):