| | 123 | |
|---|
| | 124 | |
|---|
| | 125 | #-- |
|---|
| | 126 | # Check for session freshness, ticket #1419 |
|---|
| | 127 | # It checks that changes made to the data in thread B are reflected in thread A. |
|---|
| | 128 | #-- |
|---|
| | 129 | fresh_md = MetaData() |
|---|
| | 130 | test_table = Table("test", fresh_md, |
|---|
| | 131 | Column("id", Integer, primary_key=True), |
|---|
| | 132 | Column("val", String(40)) |
|---|
| | 133 | ) |
|---|
| | 134 | class Test(object): |
|---|
| | 135 | pass |
|---|
| | 136 | mapper(Test, test_table) |
|---|
| | 137 | |
|---|
| | 138 | class FreshRoot(RootController): |
|---|
| | 139 | def test1(self): |
|---|
| | 140 | assert session.query(Test).get(1).val == 'a' |
|---|
| | 141 | return dict() |
|---|
| | 142 | test1 = expose()(test1) |
|---|
| | 143 | def test2(self): |
|---|
| | 144 | session.query(Test).get(1).val = 'b' |
|---|
| | 145 | return dict() |
|---|
| | 146 | test2 = expose()(test2) |
|---|
| | 147 | def test3(self): |
|---|
| | 148 | assert session.query(Test).get(1).val == 'b' |
|---|
| | 149 | return dict() |
|---|
| | 150 | test3 = expose()(test3) |
|---|
| | 151 | |
|---|
| | 152 | def test_session_freshness(): |
|---|
| | 153 | if os.path.exists('freshtest.db'): |
|---|
| | 154 | os.unlink('freshtest.db') |
|---|
| | 155 | fresh_md.bind = 'sqlite:///freshtest.db' # :memory: can't be used in multiple threads |
|---|
| | 156 | test_table.create() |
|---|
| | 157 | fresh_md.engine.execute(test_table.insert(), dict(id=1, val='a')) |
|---|
| | 158 | |
|---|
| | 159 | cherrypy.root = FreshRoot() |
|---|
| | 160 | |
|---|
| | 161 | create_request("/test1") |
|---|
| | 162 | |
|---|
| | 163 | # Call test2 in a different thread |
|---|
| | 164 | class ThreadB(threading.Thread): |
|---|
| | 165 | def run(self): |
|---|
| | 166 | create_request("/test2") |
|---|
| | 167 | thrdb = ThreadB() |
|---|
| | 168 | thrdb.start() |
|---|
| | 169 | thrdb.join() |
|---|
| | 170 | |
|---|
| | 171 | create_request("/test3") |
|---|
| | 172 | |
|---|
| | 173 | assert 'AssertionError' not in cherrypy.response.body[0] |
|---|