tests/test-pending.t
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 40369 ef6cab7930b3
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.

Verify that pending changesets are seen by pretxn* hooks but not by other
processes that access the destination repo while the hooks are running.

The hooks (python and external) both reject changesets after some think time,
during which another process runs pull.  Each hook creates a file ('notify') to
indicate to the controlling process that it is running; the process removes the
file to indicate the hook can terminate.

init env vars

  $ d=`pwd`
  $ maxwait=20

utility to run the test - start a push in the background and run pull

  $ dotest() {
  >     rm -f notify
  >     printf 'push '; hg -R child-push tip --template '{node}\n'
  >     hg -R child-push -q push > push.out 2>&1 &
  > 
  >     # wait for hook to create the notify file
  >     i=$maxwait
  >     while [ ! -f notify -a $i != 0 ]; do
  >         sleep 1
  >         i=`expr $i - 1`
  >     done
  > 
  >     # run pull
  >     hg -R child-pull -q pull
  >     rc=$?
  > 
  >     # tell hook to finish; notify should exist.
  >     rm notify
  >     wait
  > 
  >     cat push.out
  >     printf 'pull '; hg -R child-pull tip --template '{node}\n'
  >     return $rc
  > }

python hook

  $ cat <<EOF > reject.py
  > import os
  > import time
  > def rejecthook(ui, repo, hooktype, node, **opts):
  >     ui.write(b'hook %s\\n' % repo[b'tip'].hex())
  >     # create the notify file so caller knows we're running
  >     fpath = os.path.join('$d', 'notify')
  >     f = open(fpath, 'w')
  >     f.close()
  >     # wait for ack - caller should delete the notify file
  >     i = int("$maxwait")
  >     while os.path.exists(fpath) and i > 0:
  >         time.sleep(1)
  >         i -= 1
  >     return True # reject the changesets
  > EOF

external hook

  $ cat <<EOF > reject.sh
  > printf 'hook '; hg tip --template '{node}\\n'
  > # create the notify file so caller knows we're running
  > fpath=$d/notify
  > touch \$fpath
  > # wait for ack - caller should delete the notify file
  > i=$maxwait
  > while [ -f \$fpath -a \$i != 0 ]; do
  >     sleep 1
  >     i=\`expr \$i - 1\`
  > done
  > exit 1 # reject the changesets
  > EOF

create repos

  $ hg init parent
  $ hg clone -q parent child-push
  $ hg clone -q parent child-pull
  $ echo a > child-push/a
  $ hg -R child-push add child-push/a
  $ hg -R child-push commit -m a -d '1000000 0'

test python hook

  $ cat <<EOF > parent/.hg/hgrc
  > [extensions]
  > reject = $d/reject.py
  > [hooks]
  > pretxnchangegroup = python:reject.rejecthook
  > EOF

  $ dotest
  push 29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
  hook 29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
  transaction abort!
  rollback completed
  abort: pretxnchangegroup hook failed
  pull 0000000000000000000000000000000000000000

test external hook

  $ cat <<EOF > parent/.hg/hgrc
  > [hooks]
  > pretxnchangegroup = sh $d/reject.sh
  > EOF

  $ dotest
  push 29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
  hook 29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
  transaction abort!
  rollback completed
  abort: pretxnchangegroup hook exited with status 1
  pull 0000000000000000000000000000000000000000

Test that pending on transaction without changegroup see the normal changegroup(
(issue4609)

  $ cat <<EOF > parent/.hg/hgrc
  > [hooks]
  > pretxnchangegroup=
  > pretxnclose = hg tip -T "tip: {node|short}\n"
  > [phases]
  > publishing=False
  > EOF

setup

  $ cd parent
  $ echo a > a
  $ hg add a
  $ hg commit -m a
  tip: cb9a9f314b8b

actual test

  $ hg phase --public .
  tip: cb9a9f314b8b