Add function for effective length calculation when actions are present.

This commit is contained in:
Aldo Cortesi 2012-07-23 10:47:33 +12:00
parent 32bd16aa31
commit 204a556aa7
2 changed files with 50 additions and 3 deletions

View File

@ -31,7 +31,7 @@ def ready_actions(length, lst):
itms = list(i) itms = list(i)
if i[0] == "r": if i[0] == "r":
itms[0] = random.randrange(length) itms[0] = random.randrange(length)
if i[0] == "a": elif i[0] == "a":
itms[0] = length+1 itms[0] = length+1
ret.append(tuple(itms)) ret.append(tuple(itms))
ret.sort() ret.sort()
@ -68,10 +68,10 @@ def write_values(fp, vals, actions, sofar=0, skip=0, blocksize=BLOCKSIZE):
offset += send_chunk(fp, v, blocksize, offset, a[0]-sofar-offset) offset += send_chunk(fp, v, blocksize, offset, a[0]-sofar-offset)
if a[1] == "pause": if a[1] == "pause":
time.sleep(a[2]) time.sleep(a[2])
elif a[1] == "inject":
send_chunk(fp, a[2], blocksize, 0, len(a[2]))
elif a[1] == "disconnect": elif a[1] == "disconnect":
return True return True
elif a[1] == "inject":
send_chunk(fp, a[2], blocksize, 0, len(a[2]))
send_chunk(fp, v, blocksize, offset, len(v)) send_chunk(fp, v, blocksize, offset, len(v))
sofar += len(v) sofar += len(v)
except tcp.NetLibDisconnect: except tcp.NetLibDisconnect:
@ -501,6 +501,9 @@ class Code:
class Message: class Message:
version = "HTTP/1.1" version = "HTTP/1.1"
def length(self): def length(self):
"""
Calculate the length of the base message without any applied actions.
"""
l = sum(len(x) for x in self.preamble()) l = sum(len(x) for x in self.preamble())
l += 2 l += 2
for i in self.headers: for i in self.headers:
@ -510,6 +513,20 @@ class Message:
l += len(self.body) l += len(self.body)
return l return l
def effective_length(self, actions):
"""
Calculate the length of the base message with all applied actions.
"""
# Order matters here, and must match the order of application in
# write_values.
l = self.length()
for i in reversed(actions):
if i[1] == "disconnect":
return i[0]
elif i[1] == "inject":
l += len(i[2])
return l
def serve(self, fp): def serve(self, fp):
started = time.time() started = time.time()
if self.body and not utils.get_header("Content-Length", self.headers): if self.body and not utils.get_header("Content-Length", self.headers):

View File

@ -433,6 +433,36 @@ class TestResponse:
testlen(rparse.parse_response({}, "400'msg':h'foo'='bar'")) testlen(rparse.parse_response({}, "400'msg':h'foo'='bar'"))
testlen(rparse.parse_response({}, "400'msg':h'foo'='bar':b@100b")) testlen(rparse.parse_response({}, "400'msg':h'foo'='bar':b@100b"))
def test_effective_length(self):
def testlen(x, actions):
s = cStringIO.StringIO()
x.serve(s)
assert x.effective_length(actions) == len(s.getvalue())
actions = [
]
r = rparse.parse_response({}, "400'msg':b@100")
actions = [
(0, "disconnect"),
]
r.actions = actions
testlen(r, actions)
actions = [
(0, "disconnect"),
(0, "inject", "foo")
]
r.actions = actions
testlen(r, actions)
actions = [
(0, "inject", "foo")
]
r.actions = actions
testlen(r, actions)
def test_read_file(): def test_read_file():
tutils.raises(rparse.FileAccessDenied, rparse.read_file, {}, "=/foo") tutils.raises(rparse.FileAccessDenied, rparse.read_file, {}, "=/foo")