SignatureTests

From APIDesign

(Redirected from Sigtest)
Jump to: navigation, search

Since the rise of XP and other Agile methodologies testing become more and more popular. Not only among dedicated quality departments, but also among developers. It is not surprising that these days a successful projects is not just code, but also set of tests to verify that the release is good enough and that the project moves in the right direction.

The API Design is not exceptional in this aspect. Proper API Design requires testing and verification. The goal when designing an API is to prevent the amoeba shaking effect - e.g. prevent each new release to completely destabilize existing usages. This requires standard unit testing as well, as something special - testing for binary compatibility.

Contents

Signature Testing Tools

Java

The NetBeans project is using its SigTest - a slight extension to classical JDK sigtest tool with an excellent integration to Maven - see GitHub repository for details.

The sigtest is the official OpenJDK tool and since version 2.1 (which has been release in Mar 2009) it supports also BackwardCompatibility tests. We run the tests daily to ensure that our API are still binary compatible and whether our APIs are changing intentionally and not accidentally. This is example of one of sigtest reports:

Base version: 1.2
Tested version: 1.3
 
Added Methods
-------------
 
org.nb.cnd.api.lexer.CndLexerUtilities: 
method public final static boolean CndLexerUtilities.isKeyword(java.lang.String)

Whenever you make a release of your API, you can use the sigtest Ant task to generate a golden file and store it in your version control next to your API sources. Since then, you can always verify that the APIs that you are creating are still compatible with the golden file by running the signature check against new version of your library.

Simple and useful. I cannot imagine API design without such verification tool. Thanks the sigtest team for releasing such mature 2.1 version!

Existing alternatives to sigtest:

Sample usage:

   japi-compliance-checker -lib NAME -old OLD.jar -new NEW.jar

C/C++

There is a backward binary compatibility checker for shared libraries in C/C++. An anonymous coward just provided a link to API compliance checker.

It is good that C world gets such a useful tool, just realize the difference with Java. Adding new field into a struct deserves a warning(!) as it may be incompatible for everyone who embeds such structure inside own struct. Adding new virtual method in backward compatible way is almost impossible, as far as I can see.

Don't get me wrong, I am glad the need for BackwardCompatibility is realized among C programmers. Just the self control to achieve such compatibility needs to be much higher than among Java programmers (and even those need a lot of self control). We still need a language that will give use evolution capabilities while letting us act in almost cluelessness mode.

--JaroslavTulach 19:08, 9 August 2009 (UTC)


Introducing Signature Tests into Your Project Workflow

API signature tests are part of Truffle project workflow since today! Let me describe what I have done, as similar changes are likely needed in any Java project that wants to use SignatureTests to keep backward compatibility of its APIs.

Identify Your API

Not all Java packages in your codebase are an API! Make sure you identify what is and what isn't part of an API. This is useful for packaging (for example to include correct OSGi metadata in manifests of your JAR files), generating Javadoc (see the Truffle documentation) and also for signature testing - you don't want to be notified about every change in implementation packages, you care only about the API parts.

Each project uses its own way to identify the API packages. The Truffle project decided to use package-info.java - if there is a package-info.java file in a directory, its (public) classes are part of the API, if not, they are an implementation detail.

The Right Tool

First and foremost one needs to select a signature testing tool. As my background is related to NetBeans, my choice was almost guaranteed. I decided to use NetBeans APITest.

Slight problem was that the tool hasn't been updated to JDK8 language constructs (e.g. default and static methods in interfaces). However, as NetBeans need to use the tool as well, I got a great help from Tomáš Zezula. He is currently working on switching the NetBeans codebase to JDK8 and he updated the APITest to deal properly with JDK8 features (see here and here). It is always good to build on shoulders of giants - e.g. use a tool that a mature project like NetBeans maintains.

Download the APITest from here.

Integrate into Build Process

Signature testing needs to be integrated into build process of your project - in case there is a violation with respect to BackwardCompatibility, the build should fail. As (almost) every big project invents its own build harness, the ways to integrate differ. For example, NetBeans is using Ant based harness and thus one deals with the signature testing tool via Ant as described at netbeans:SignatureTest page.

The Truffle project (as well as Graal) is using Python based harness. As such I had to write a bit of Python code to integrate the APITest into it (see here). You basically need two operations:

  1. generate - which will take a snapshot of your API at a certain point of time (usually at a release time)
  2. check - which will compare the current state of your API with the existing snapshot

To invoke these operations in Truffle repository use:

$ mx sigtest --generate
# generates following files
truffle/com.oracle.truffle.api.dsl/snapshot.sigtest
truffle/com.oracle.truffle.api.interop.java/snapshot.sigtest
truffle/com.oracle.truffle.api.interop/snapshot.sigtest
truffle/com.oracle.truffle.api.object/snapshot.sigtest
truffle/com.oracle.truffle.api/snapshot.sigtest
truffle/com.oracle.truffle.api.vm/snapshot.sigtest
 
# check whether there are any incompatible changes
$ mx sigtest --check binary
 
# check if there are any API changes
$ mx sigtest --check all

I've integrated the "--check binary" into so called gate check, so if there is an incompatible change, the build fails - see the changeset.

Setting the Snapshots Up

BackwardCompatibility is important between releases, so once you do a release (whatever that means), generate a snapshot of your API state. The most recent (e.g. JavaOne 2015) release of Truffle is 0.9, so I just did:

$ hg up -C truffle-0.9
$ mx build
$ mx sigtest --generate
$ hg up default
$ hg add .
$ hg ci -m "Adding API snapshots as for version truffle-0.9 and enabling their check in the gate"

Here is how such snapshot files look like: changeset. Of course, it turned out that not everything is perfect (how it could be! we weren't running the tests and it is so hard to reason about BackwardCompatibility without testing):

  • Removing API elements that were deprecated at the time of truffle-0.9 release from the list of required API elements: 2ce4c49bc131 - Truffle project is still in its early phases, so according to our compatibility policy removing deprecated elements is OK. Once we reach release 1.0, we won't do things like this.
  • The check discovered a removed constructor 5033b980cc68 which was clearly a bug
  • Adjusting to binary incompatible change of (on)returnExceptional parameters 7273b139fff2 - this was an intentional and agreed on change (in spite of being against the compatibility policy)
  • And at last, there was an API design problem - a return type of an API visible method was not public - e.g. visible

In all of these cases I manually updated the signature files to match the expected state. Then the compatibility check finally succeeded. Since today it will be way easier to keep BackwardCompatibility in our Truffle project!

Notification of Daily Changes

In addition to catching incompatible changes, it is useful to track compatible ones as well.

Some of the API changes happen by an accident and when they aren't noticed in time, they may become part of a release. But then they have to stay (as they are like stars) regardless how bad they are. Nobody wants to maintain erroneous code or API, so it is better to catch such changes as soon as possible and revert them before they propagate to a release.

Having a Hudson job to watch daily compatible changes and notify us about them is the best way to prevent accidental API changes. The job shall have two parts: generate new snapshot of today's APIs:

$ mx sigtest --generate
$ zip snapshots.zip truffle/*/*sigtest

and make them persisted artifacts of the job. The other part is to take these artifacts from the last successful build and compare them against current state:

$ wget $HUDSON_URL/lastSuccessfulBuild/snaphots.zip
$ unzip snapshots.zip
$ mx sigtest --check all

then it is just a matter of sending the output of the last command to an observer mailing list, so humans can decide whether the change is intended or not.

I believe that with infrastructure like this the Truffle project will be able to stick to its promise of compatibility and better preserve investments done by language writers porting their languages to the fastest (J)VM on the Planet!

Personal tools