server/test/unittest_extlite.py
branchstable
changeset 2200 25bb65dc4559
parent 2053 fb156d69bfd9
child 2637 07103211e511
equal deleted inserted replaced
2199:bd0a0f219751 2200:25bb65dc4559
     5 
     5 
     6 class SQLiteTC(TestCase):
     6 class SQLiteTC(TestCase):
     7     sqlite_file = '_extlite_test.sqlite'
     7     sqlite_file = '_extlite_test.sqlite'
     8     def setUp(self):
     8     def setUp(self):
     9         cnx1 = get_connection('sqlite', database=self.sqlite_file)
     9         cnx1 = get_connection('sqlite', database=self.sqlite_file)
    10         print 'SET IP'
       
    11         cu = cnx1.cursor()
    10         cu = cnx1.cursor()
    12         cu.execute('CREATE TABLE toto(name integer);')
    11         cu.execute('CREATE TABLE toto(name integer);')
    13         cnx1.commit()
    12         cnx1.commit()
    14         cnx1.close()
    13         cnx1.close()
    15         
    14 
    16     def tearDown(self):
    15     def tearDown(self):
    17         try:
    16         try:
    18             os.remove(self.sqlite_file)
    17             os.remove(self.sqlite_file)
    19         except:
    18         except:
    20             pass
    19             pass
       
    20         
    21     def test(self):
    21     def test(self):
    22         lock = threading.Lock()
    22         lock = threading.Lock()
    23         
    23 
    24         def run_thread():
    24         def run_thread():
    25             print 'run_thread'
       
    26             cnx2 = get_connection('sqlite', database=self.sqlite_file)
    25             cnx2 = get_connection('sqlite', database=self.sqlite_file)
    27             lock.acquire()
    26             lock.acquire()
    28             print 't2 sel1'
       
    29             cu = cnx2.cursor()
    27             cu = cnx2.cursor()
    30             cu.execute('SELECT name FROM toto')
    28             cu.execute('SELECT name FROM toto')
    31             self.failIf(cu.fetchall())
    29             self.failIf(cu.fetchall())
    32             cnx2.commit()
    30             cnx2.commit()
    33             print 'done'
       
    34             lock.release()
    31             lock.release()
    35             time.sleep(0.1)
    32             time.sleep(0.1)
    36             lock.acquire()
    33             lock.acquire()
    37             print 't2 sel2'
       
    38             cu.execute('SELECT name FROM toto')
    34             cu.execute('SELECT name FROM toto')
    39             self.failUnless(cu.fetchall())
    35             self.failUnless(cu.fetchall())
    40             print 'done'
       
    41             lock.release()
    36             lock.release()
    42 
    37 
    43         cnx1 = get_connection('sqlite', database=self.sqlite_file)
    38         cnx1 = get_connection('sqlite', database=self.sqlite_file)
    44         lock.acquire()
    39         lock.acquire()
    45         thread = threading.Thread(target=run_thread)
    40         thread = threading.Thread(target=run_thread)
    46         thread.start()
    41         thread.start()
    47         cu = cnx1.cursor()
    42         cu = cnx1.cursor()
    48         print 't1 sel'
       
    49         cu.execute('SELECT name FROM toto')
    43         cu.execute('SELECT name FROM toto')
    50         print 'done'
       
    51         lock.release()
    44         lock.release()
    52         time.sleep(0.1)
    45         time.sleep(0.1)
    53         cnx1.commit()
    46         cnx1.commit()
    54         lock.acquire()
    47         lock.acquire()
    55         print 't1 insert'
       
    56         cu.execute("INSERT INTO toto(name) VALUES ('toto')")
    48         cu.execute("INSERT INTO toto(name) VALUES ('toto')")
    57         cnx1.commit()
    49         cnx1.commit()
    58         print 'done'
       
    59         lock.release()
    50         lock.release()
    60 
    51 
    61 if __name__ == '__main__':
    52 if __name__ == '__main__':
    62     unittest_main()
    53     unittest_main()