server/test/unittest_extlite.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 31 Aug 2009 18:59:10 +0200
branch3.5
changeset 3071 6ad4e6d4df86
parent 2857 81d10fedc32e
child 3147 83ad6ff13e3f
permissions -rw-r--r--
backport stable branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2053
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
import threading, os, time
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
from logilab.common.testlib import TestCase, unittest_main
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
from logilab.common.db import get_connection
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
class SQLiteTC(TestCase):
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
    sqlite_file = '_extlite_test.sqlite'
2857
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
     8
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
     9
    def _cleanup(self):
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
    10
        try:
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
    11
            os.remove(self.sqlite_file)
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
    12
        except:
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
    13
            pass
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
    14
2053
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
    def setUp(self):
2857
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
    16
        self._cleanup()
2053
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
        cnx1 = get_connection('sqlite', database=self.sqlite_file)
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
        cu = cnx1.cursor()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
        cu.execute('CREATE TABLE toto(name integer);')
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
        cnx1.commit()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
        cnx1.close()
2200
25bb65dc4559 test fixes, all server tests ok, except unittest_migractions (due to inter-tests-side-effects...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
    22
2053
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
    def tearDown(self):
2857
81d10fedc32e B [server] extlite test will not fail if tempfile exists
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2637
diff changeset
    24
        self._cleanup()
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2200
diff changeset
    25
2053
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
    def test(self):
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
        lock = threading.Lock()
2200
25bb65dc4559 test fixes, all server tests ok, except unittest_migractions (due to inter-tests-side-effects...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
    28
2053
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
        def run_thread():
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
            cnx2 = get_connection('sqlite', database=self.sqlite_file)
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
            lock.acquire()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
            cu = cnx2.cursor()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
            cu.execute('SELECT name FROM toto')
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
            self.failIf(cu.fetchall())
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
            cnx2.commit()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
            lock.release()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
            time.sleep(0.1)
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
            lock.acquire()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
            cu.execute('SELECT name FROM toto')
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
            self.failUnless(cu.fetchall())
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
            lock.release()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
        cnx1 = get_connection('sqlite', database=self.sqlite_file)
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
        lock.acquire()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
        thread = threading.Thread(target=run_thread)
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
        thread.start()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
        cu = cnx1.cursor()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
        cu.execute('SELECT name FROM toto')
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
        lock.release()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
        time.sleep(0.1)
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
        cnx1.commit()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    52
        lock.acquire()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
        cu.execute("INSERT INTO toto(name) VALUES ('toto')")
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    54
        cnx1.commit()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
        lock.release()
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
if __name__ == '__main__':
fb156d69bfd9 change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
    unittest_main()