server/test/unittest_extlite.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 07 Aug 2009 14:49:48 +0200
changeset 2737 aaef49a3c39b
parent 2637 07103211e511
child 2857 81d10fedc32e
permissions -rw-r--r--
use makedirs, some old instances don't have parent directory properly set
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'
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
     8
    def setUp(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
     9
        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
    10
        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
    11
        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
    12
        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
    13
        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
    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 tearDown(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
    16
        try:
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
            os.remove(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
        except:
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
            pass
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2200
diff changeset
    20
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
    21
    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
    22
        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
    23
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
    24
        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
    25
            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
    26
            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
    27
            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
    28
            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
    29
            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
    30
            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
    31
            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
    32
            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
    33
            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
    34
            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
    35
            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
    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
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
        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
    39
        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
    40
        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
    41
        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
    42
        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
    43
        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
    44
        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
    45
        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
    46
        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
    47
        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
    48
        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
    49
        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
    50
        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
    51
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
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
    53
    unittest_main()