##// END OF EJS Templates
wireprotov2: implement commands as a generator of objects...
wireprotov2: implement commands as a generator of objects Previously, wire protocol version 2 inherited version 1's model of having separate types to represent the results of different wire protocol commands. As I implemented more powerful commands in future commits, I found I was using a common pattern of returning a special type to hold a generator. This meant the command function required a closure to do most of the work. That made logic flow more difficult to follow. I also noticed that many commands were effectively a sequence of objects to be CBOR encoded. I think it makes sense to define version 2 commands as generators. This way, commands can simply emit the data structures they wish to send to the client. This eliminates the need for a closure in command functions and removes encoding from the bodies of commands. As part of this commit, the handling of response objects has been moved into the serverreactor class. This puts the reactor in the driver's seat with regards to CBOR encoding and error handling. Having error handling in the function that emits frames is particularly important because exceptions in that function can lead to things getting in a bad state: I'm fairly certain that uncaught exceptions in the frame generator were causing deadlocks. I also introduced a dedicated error type for explicit error reporting in command handlers. This will be used in subsequent commits. There's still a bit of work to be done here, especially around formalizing the error handling "protocol." I've added yet another TODO to track this so we don't forget. Test output changed because we're using generators and no longer know we are at the end of the data until we hit the end of the generator. This means we can't emit the end-of-stream flag until we've exhausted the generator. Hence the introduction of 0-sized end-of-stream frames. Differential Revision: https://phab.mercurial-scm.org/D4472

File last commit:

r39024:1aab0007 default
r39595:07b58266 default
Show More
test-absorb-filefixupstate.py
207 lines | 6.4 KiB | text/x-python | PythonLexer
/ tests / test-absorb-filefixupstate.py
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 from __future__ import absolute_import, print_function
import itertools
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 from mercurial import pycompat
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 from hgext import absorb
class simplefctx(object):
def __init__(self, content):
self.content = content
def data(self):
return self.content
def insertreturns(x):
# insert "\n"s after each single char
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 if isinstance(x, bytes):
return b''.join(ch + b'\n' for ch in pycompat.bytestr(x))
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 else:
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 return pycompat.maplist(insertreturns, x)
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
def removereturns(x):
# the revert of "insertreturns"
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 if isinstance(x, bytes):
return x.replace(b'\n', b'')
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 else:
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 return pycompat.maplist(removereturns, x)
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
def assertlistequal(lhs, rhs, decorator=lambda x: x):
if lhs != rhs:
raise RuntimeError('mismatch:\n actual: %r\n expected: %r'
% tuple(map(decorator, [lhs, rhs])))
def testfilefixup(oldcontents, workingcopy, expectedcontents, fixups=None):
"""([str], str, [str], [(rev, a1, a2, b1, b2)]?) -> None
workingcopy is a string, of which every character denotes a single line.
oldcontents, expectedcontents are lists of strings, every character of
every string denots a single line.
if fixups is not None, it's the expected fixups list and will be checked.
"""
expectedcontents = insertreturns(expectedcontents)
oldcontents = insertreturns(oldcontents)
workingcopy = insertreturns(workingcopy)
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 state = absorb.filefixupstate(pycompat.maplist(simplefctx, oldcontents))
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 state.diffwith(simplefctx(workingcopy))
if fixups is not None:
assertlistequal(state.fixups, fixups)
state.apply()
assertlistequal(state.finalcontents, expectedcontents, removereturns)
def buildcontents(linesrevs):
# linesrevs: [(linecontent : str, revs : [int])]
revs = set(itertools.chain(*[revs for line, revs in linesrevs]))
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 return [b''] + [
b''.join([l for l, rs in linesrevs if r in rs])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 for r in sorted(revs)
]
# input case 0: one single commit
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 case0 = [b'', b'11']
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# replace a single chunk
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case0, b'', [b'', b''])
testfilefixup(case0, b'2', [b'', b'2'])
testfilefixup(case0, b'22', [b'', b'22'])
testfilefixup(case0, b'222', [b'', b'222'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# input case 1: 3 lines, each commit adds one line
case1 = buildcontents([
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 (b'1', [1, 2, 3]),
(b'2', [ 2, 3]),
(b'3', [ 3]),
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 ])
# 1:1 line mapping
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case1, b'123', case1)
testfilefixup(case1, b'12c', [b'', b'1', b'12', b'12c'])
testfilefixup(case1, b'1b3', [b'', b'1', b'1b', b'1b3'])
testfilefixup(case1, b'1bc', [b'', b'1', b'1b', b'1bc'])
testfilefixup(case1, b'a23', [b'', b'a', b'a2', b'a23'])
testfilefixup(case1, b'a2c', [b'', b'a', b'a2', b'a2c'])
testfilefixup(case1, b'ab3', [b'', b'a', b'ab', b'ab3'])
testfilefixup(case1, b'abc', [b'', b'a', b'ab', b'abc'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# non 1:1 edits
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case1, b'abcd', case1)
testfilefixup(case1, b'ab', case1)
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# deletion
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case1, b'', [b'', b'', b'', b''])
testfilefixup(case1, b'1', [b'', b'1', b'1', b'1'])
testfilefixup(case1, b'2', [b'', b'', b'2', b'2'])
testfilefixup(case1, b'3', [b'', b'', b'', b'3'])
testfilefixup(case1, b'13', [b'', b'1', b'1', b'13'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# replaces
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case1, b'1bb3', [b'', b'1', b'1bb', b'1bb3'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# (confusing) replaces
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case1, b'1bbb', case1)
testfilefixup(case1, b'bbbb', case1)
testfilefixup(case1, b'bbb3', case1)
testfilefixup(case1, b'1b', case1)
testfilefixup(case1, b'bb', case1)
testfilefixup(case1, b'b3', case1)
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# insertions at the beginning and the end
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case1, b'123c', [b'', b'1', b'12', b'123c'])
testfilefixup(case1, b'a123', [b'', b'a1', b'a12', b'a123'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# (confusing) insertions
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case1, b'1a23', case1)
testfilefixup(case1, b'12b3', case1)
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# input case 2: delete in the middle
case2 = buildcontents([
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 (b'11', [1, 2]),
(b'22', [1 ]),
(b'33', [1, 2]),
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 ])
# deletion (optimize code should make it 2 chunks)
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case2, b'', [b'', b'22', b''],
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 fixups=[(4, 0, 2, 0, 0), (4, 2, 4, 0, 0)])
# 1:1 line mapping
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case2, b'aaaa', [b'', b'aa22aa', b'aaaa'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# non 1:1 edits
# note: unlike case0, the chunk is not "continuous" and no edit allowed
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case2, b'aaa', case2)
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# input case 3: rev 3 reverts rev 2
case3 = buildcontents([
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 (b'1', [1, 2, 3]),
(b'2', [ 2 ]),
(b'3', [1, 2, 3]),
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 ])
# 1:1 line mapping
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case3, b'13', case3)
testfilefixup(case3, b'1b', [b'', b'1b', b'12b', b'1b'])
testfilefixup(case3, b'a3', [b'', b'a3', b'a23', b'a3'])
testfilefixup(case3, b'ab', [b'', b'ab', b'a2b', b'ab'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# non 1:1 edits
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case3, b'a', case3)
testfilefixup(case3, b'abc', case3)
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# deletion
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case3, b'', [b'', b'', b'2', b''])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# insertion
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case3, b'a13c', [b'', b'a13c', b'a123c', b'a13c'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# input case 4: a slightly complex case
case4 = buildcontents([
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 (b'1', [1, 2, 3]),
(b'2', [ 2, 3]),
(b'3', [1, 2, ]),
(b'4', [1, 3]),
(b'5', [ 3]),
(b'6', [ 2, 3]),
(b'7', [ 2 ]),
(b'8', [ 2, 3]),
(b'9', [ 3]),
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 ])
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case4, b'1245689', case4)
testfilefixup(case4, b'1a2456bbb', case4)
testfilefixup(case4, b'1abc5689', case4)
testfilefixup(case4, b'1ab5689', [b'', b'134', b'1a3678', b'1ab5689'])
testfilefixup(case4, b'aa2bcd8ee', [b'', b'aa34', b'aa23d78', b'aa2bcd8ee'])
testfilefixup(case4, b'aa2bcdd8ee',[b'', b'aa34', b'aa23678', b'aa24568ee'])
testfilefixup(case4, b'aaaaaa', case4)
testfilefixup(case4, b'aa258b', [b'', b'aa34', b'aa2378', b'aa258b'])
testfilefixup(case4, b'25bb', [b'', b'34', b'23678', b'25689'])
testfilefixup(case4, b'27', [b'', b'34', b'23678', b'245689'])
testfilefixup(case4, b'28', [b'', b'34', b'2378', b'28'])
testfilefixup(case4, b'', [b'', b'34', b'37', b''])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# input case 5: replace a small chunk which is near a deleted line
case5 = buildcontents([
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 (b'12', [1, 2]),
(b'3', [1]),
(b'4', [1, 2]),
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953 ])
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case5, b'1cd4', [b'', b'1cd34', b'1cd4'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# input case 6: base "changeset" is immutable
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 case6 = [b'1357', b'0125678']
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case6, b'0125678', case6)
testfilefixup(case6, b'0a25678', case6)
testfilefixup(case6, b'0a256b8', case6)
testfilefixup(case6, b'abcdefg', [b'1357', b'a1c5e7g'])
testfilefixup(case6, b'abcdef', case6)
testfilefixup(case6, b'', [b'1357', b'157'])
testfilefixup(case6, b'0123456789', [b'1357', b'0123456789'])
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
# input case 7: change an empty file
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 case7 = [b'']
Augie Fackler
absorb: import extension from Facebook's hg-experimental...
r38953
Augie Fackler
tests: port test-absorb-filefixupstate to Python 3...
r39024 testfilefixup(case7, b'1', case7)