server/test/unittest_extlite.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 05 Jun 2009 13:21:31 +0200
branchstable
changeset 2053 fb156d69bfd9
child 2200 25bb65dc4559
permissions -rw-r--r--
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)
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
        print 'SET IP'
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 = 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
    12
        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
    13
        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
    14
        cnx1.close()
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
        
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
    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
    17
        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
    18
            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
    19
        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
    20
            pass
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()
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
        
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
            print '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
    26
            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
    27
            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
    28
            print 't2 sel1'
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
            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
    30
            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
    31
            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
    32
            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
    33
            print 'done'
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
            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
    35
            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
    36
            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
    37
            print 't2 sel2'
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
            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
    39
            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
    40
            print 'done'
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
        print 't1 sel'
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
        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
    50
        print 'done'
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
        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
    52
        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
    53
        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
    54
        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
    55
        print 't1 insert'
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
        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
    57
        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
    58
        print 'done'
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
    59
        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
    60
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
    61
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
    62
    unittest_main()