| 1 |
from cherrypy.test import test |
|---|
| 2 |
test.prefer_parent_path() |
|---|
| 3 |
|
|---|
| 4 |
import os |
|---|
| 5 |
localDir = os.path.dirname(__file__) |
|---|
| 6 |
import sys |
|---|
| 7 |
import threading |
|---|
| 8 |
import time |
|---|
| 9 |
|
|---|
| 10 |
import cherrypy |
|---|
| 11 |
from cherrypy.lib import sessions |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
def setup_server(): |
|---|
| 15 |
class Root: |
|---|
| 16 |
|
|---|
| 17 |
_cp_config = {'tools.sessions.on': True, |
|---|
| 18 |
'tools.sessions.storage_type' : 'ram', |
|---|
| 19 |
'tools.sessions.storage_path' : localDir, |
|---|
| 20 |
'tools.sessions.timeout': 0.017, |
|---|
| 21 |
'tools.sessions.clean_freq': 0.017, |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
def testGen(self): |
|---|
| 25 |
counter = cherrypy.session.get('counter', 0) + 1 |
|---|
| 26 |
cherrypy.session['counter'] = counter |
|---|
| 27 |
yield str(counter) |
|---|
| 28 |
testGen.exposed = True |
|---|
| 29 |
|
|---|
| 30 |
def testStr(self): |
|---|
| 31 |
counter = cherrypy.session.get('counter', 0) + 1 |
|---|
| 32 |
cherrypy.session['counter'] = counter |
|---|
| 33 |
return str(counter) |
|---|
| 34 |
testStr.exposed = True |
|---|
| 35 |
|
|---|
| 36 |
def setsessiontype(self, newtype): |
|---|
| 37 |
self.__class__._cp_config.update({'tools.sessions.storage_type': newtype}) |
|---|
| 38 |
setsessiontype.exposed = True |
|---|
| 39 |
|
|---|
| 40 |
def index(self): |
|---|
| 41 |
sess = cherrypy.session |
|---|
| 42 |
c = sess.get('counter', 0) + 1 |
|---|
| 43 |
time.sleep(0.01) |
|---|
| 44 |
sess['counter'] = c |
|---|
| 45 |
return str(c) |
|---|
| 46 |
index.exposed = True |
|---|
| 47 |
|
|---|
| 48 |
def delete(self): |
|---|
| 49 |
cherrypy.session.delete() |
|---|
| 50 |
sessions.expire() |
|---|
| 51 |
return "done" |
|---|
| 52 |
delete.exposed = True |
|---|
| 53 |
|
|---|
| 54 |
def delkey(self, key): |
|---|
| 55 |
del cherrypy.session[key] |
|---|
| 56 |
return "OK" |
|---|
| 57 |
delkey.exposed = True |
|---|
| 58 |
|
|---|
| 59 |
def blah(self): |
|---|
| 60 |
return self._cp_config['tools.sessions.storage_type'] |
|---|
| 61 |
blah.exposed = True |
|---|
| 62 |
|
|---|
| 63 |
def iredir(self): |
|---|
| 64 |
raise cherrypy.InternalRedirect('/blah') |
|---|
| 65 |
iredir.exposed = True |
|---|
| 66 |
|
|---|
| 67 |
cherrypy.tree.mount(Root()) |
|---|
| 68 |
cherrypy.config.update({'environment': 'test_suite'}) |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
from cherrypy.test import helper |
|---|
| 72 |
|
|---|
| 73 |
class SessionTest(helper.CPWebCase): |
|---|
| 74 |
|
|---|
| 75 |
def test_0_Session(self): |
|---|
| 76 |
self.getPage('/testStr') |
|---|
| 77 |
self.assertBody('1') |
|---|
| 78 |
self.getPage('/testGen', self.cookies) |
|---|
| 79 |
self.assertBody('2') |
|---|
| 80 |
self.getPage('/testStr', self.cookies) |
|---|
| 81 |
self.assertBody('3') |
|---|
| 82 |
self.getPage('/delkey?key=counter', self.cookies) |
|---|
| 83 |
self.assertStatus(200) |
|---|
| 84 |
|
|---|
| 85 |
self.getPage('/setsessiontype/file') |
|---|
| 86 |
self.getPage('/testStr') |
|---|
| 87 |
self.assertBody('1') |
|---|
| 88 |
self.getPage('/testGen', self.cookies) |
|---|
| 89 |
self.assertBody('2') |
|---|
| 90 |
self.getPage('/testStr', self.cookies) |
|---|
| 91 |
self.assertBody('3') |
|---|
| 92 |
self.getPage('/delkey?key=counter', self.cookies) |
|---|
| 93 |
self.assertStatus(200) |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
time.sleep(1.25) |
|---|
| 97 |
self.getPage('/') |
|---|
| 98 |
self.assertBody('1') |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
self.getPage('/delete', self.cookies) |
|---|
| 102 |
self.assertBody("done") |
|---|
| 103 |
f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')] |
|---|
| 104 |
self.assertEqual(f(), []) |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
self.getPage('/') |
|---|
| 108 |
f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')] |
|---|
| 109 |
self.assertNotEqual(f(), []) |
|---|
| 110 |
time.sleep(2) |
|---|
| 111 |
self.assertEqual(f(), []) |
|---|
| 112 |
|
|---|
| 113 |
def test_1_Ram_Concurrency(self): |
|---|
| 114 |
self.getPage('/setsessiontype/ram') |
|---|
| 115 |
self._test_Concurrency() |
|---|
| 116 |
|
|---|
| 117 |
def test_2_File_Concurrency(self): |
|---|
| 118 |
self.getPage('/setsessiontype/file') |
|---|
| 119 |
self._test_Concurrency() |
|---|
| 120 |
|
|---|
| 121 |
def _test_Concurrency(self): |
|---|
| 122 |
client_thread_count = 5 |
|---|
| 123 |
request_count = 30 |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
self.getPage("/") |
|---|
| 127 |
self.assertBody("1") |
|---|
| 128 |
cookies = self.cookies |
|---|
| 129 |
|
|---|
| 130 |
data_dict = {} |
|---|
| 131 |
|
|---|
| 132 |
def request(index): |
|---|
| 133 |
for i in xrange(request_count): |
|---|
| 134 |
self.getPage("/", cookies) |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
data_dict[index] = v = int(self.body) |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
ts = [] |
|---|
| 142 |
for c in xrange(client_thread_count): |
|---|
| 143 |
data_dict[c] = 0 |
|---|
| 144 |
t = threading.Thread(target=request, args=(c,)) |
|---|
| 145 |
ts.append(t) |
|---|
| 146 |
t.start() |
|---|
| 147 |
|
|---|
| 148 |
for t in ts: |
|---|
| 149 |
t.join() |
|---|
| 150 |
|
|---|
| 151 |
hitcount = max(data_dict.values()) |
|---|
| 152 |
expected = 1 + (client_thread_count * request_count) |
|---|
| 153 |
self.assertEqual(hitcount, expected) |
|---|
| 154 |
|
|---|
| 155 |
def test_3_Redirect(self): |
|---|
| 156 |
|
|---|
| 157 |
self.getPage('/testStr') |
|---|
| 158 |
self.getPage('/iredir', self.cookies) |
|---|
| 159 |
self.assertBody("file") |
|---|
| 160 |
|
|---|
| 161 |
def test_4_File_deletion(self): |
|---|
| 162 |
|
|---|
| 163 |
self.getPage('/testStr') |
|---|
| 164 |
|
|---|
| 165 |
id = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1] |
|---|
| 166 |
path = os.path.join(localDir, "session-" + id) |
|---|
| 167 |
os.unlink(path) |
|---|
| 168 |
self.getPage('/testStr', self.cookies) |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
if __name__ == "__main__": |
|---|
| 173 |
setup_server() |
|---|
| 174 |
helper.testmain() |
|---|