|
| 1 | +.. Jython companion to coverage.rst |
| 2 | +
|
| 3 | +.. _coverage-jy: |
| 4 | + |
| 5 | +Test Coverage in Jython |
| 6 | +======================= |
| 7 | + |
| 8 | +In broad groups, we have three things to test: |
| 9 | + |
| 10 | +#. The Python Standard Library modules implemented in Python, |
| 11 | +#. The Java implementation of modules in the Standard Library, and |
| 12 | +#. The core of Jython implemented in Java. |
| 13 | + |
| 14 | +In each case, Jython relies heavily on the tests provided by the CPython |
| 15 | +reference implementation. |
| 16 | + |
| 17 | +Jython adopts the Python Standard Library wholesale, and |
| 18 | +its quality is assured by the CPython project. |
| 19 | +Jython makes some small changes to its copy |
| 20 | +to replace or add to tests and library modules. |
| 21 | +We aim to minimise the Jython-specific code in both the |
| 22 | +standard library and our tests. |
| 23 | + |
| 24 | +Python development follows the practice that all semantic changes and additions |
| 25 | +to the language and :abbr:`stdlib (standard library)` are accompanied by |
| 26 | +appropriate unit tests. |
| 27 | +This is the aim of ensuring sufficient :doc:`test coverage <coverage>`. |
| 28 | +To the extent that CPython achieves this, a large part of Jython developement |
| 29 | +may be driven by these freely provided tests. |
| 30 | + |
| 31 | +Improvements to the library and its test coverage may emerge from Jython, |
| 32 | +but the strategy depends on contributing them |
| 33 | +:doc:`upstream to CPython <coverage>`. |
| 34 | + |
| 35 | +.. _coverage-by-regrtest-jy: |
| 36 | + |
| 37 | + |
| 38 | +Test Coverage Challenges |
| 39 | +------------------------ |
| 40 | + |
| 41 | +There are places where Jython cannot depend on tests provided by CPython: |
| 42 | + |
| 43 | +#. Java integration (calling and being called by Java). |
| 44 | +#. Concurrency (where CPython's :abbr:`GIL (Global Interpreter Lock)` saves it |
| 45 | + but not Jython). |
| 46 | +#. Tests dependent on garbage collection (sometimes inadvertently). |
| 47 | +#. Tests that expose implementation details that differ. |
| 48 | + |
| 49 | +For these cases we create tests in Python (to supersede or supplement the |
| 50 | +ones in the CPython :mod:`test` module) and tests in Java. |
| 51 | + |
| 52 | + |
| 53 | +Java Integration |
| 54 | +'''''''''''''''' |
| 55 | + |
| 56 | +Java integration is unique to Jython, so we must have our own tests. |
| 57 | +Where possible, tests should be in Python, using :mod:`unittest`. |
| 58 | +Where tests have to be in Java, we recommend the JUnit_ framework. |
| 59 | + |
| 60 | + |
| 61 | +Concurrency |
| 62 | +''''''''''' |
| 63 | + |
| 64 | +The JVM has strong support for concurrency. |
| 65 | +There are many constructs in the Java library to support concurrency, |
| 66 | +which may be used directly from Python. |
| 67 | + |
| 68 | +Python also has support for concurrency, and a rich library, |
| 69 | +but in the CPython interpreter, |
| 70 | +only one thread may be executing CPython bytecode at a time. |
| 71 | +Concurrency is limited to releasing the global lock during blocking operations |
| 72 | +such as i/o. |
| 73 | +If a task is CPU-bound, |
| 74 | +one must use multiple CPython interpreters in order to scale it. |
| 75 | + |
| 76 | +By contrast, the JVM may switch threads at any time, |
| 77 | +part-way through what in CPython would be an atomic operation. |
| 78 | +(JVM instructions may be atomic, but this barely helps us at all.) |
| 79 | +Re-use or translation of implementations used in CPython needs to bear this |
| 80 | +difference in mind, and additional tests are likely to be necessary to |
| 81 | +assure us of correctness. |
| 82 | + |
| 83 | +Some Jython applications will run in an environments with high concurrency, |
| 84 | +and Java thread pools (such as a web-service), |
| 85 | +where faulty concurrency is more likely to be exposed than in typical uses of |
| 86 | +CPython. |
| 87 | + |
| 88 | + |
| 89 | +Garbage Collection |
| 90 | +'''''''''''''''''' |
| 91 | + |
| 92 | +Many older tests create objects they assume are garbage-collected immediately |
| 93 | +they go out of scope. |
| 94 | +Examples: |
| 95 | + |
| 96 | +* Files assumed closed as soon as dereferenced. |
| 97 | +* The timing of weak reference invalidation. |
| 98 | + |
| 99 | +These tests may need adaptation to Java. |
| 100 | +There is no way to force garbage collection in Java, only to "suggest" it. |
| 101 | +Tests that open files are a particular problem on Windows, |
| 102 | +where files that remain open when they pass out of scope, |
| 103 | +cannot be deleted. |
| 104 | + |
| 105 | + |
| 106 | +Jython Internals |
| 107 | +'''''''''''''''' |
| 108 | + |
| 109 | +Tests of internal methods not accessible to Python are necessarily in Java, |
| 110 | +usually as JUnit_ tests. |
| 111 | +Examples: |
| 112 | + |
| 113 | +* The buffer interface. |
| 114 | +* Type exposure. |
| 115 | + |
| 116 | + |
| 117 | +Filing the Issue |
| 118 | +---------------- |
| 119 | + |
| 120 | +Once you have increased coverage, you need to create an issue on the |
| 121 | +`Jython issue tracker`_ and a patch there. |
| 122 | +(In the future process, you will probably submit a |
| 123 | +:doc:`pull request <pullrequest>`.) |
| 124 | +On the issue set the "Components" to "Test" and "Versions" to the version of |
| 125 | +Jython you worked on (i.e., the in-development version). |
| 126 | + |
| 127 | +.. _Jython issue tracker: http://bugs.jython.org |
| 128 | +.. _Junit: http://junit.org |
| 129 | + |
0 commit comments