Thursday, February 26, 2009

It turns out that TestNG has built-in support for the functionality I just implemented for our JUnit tests.

Wednesday, February 11, 2009

At work the time to execute all of our our growing set of JUnit tests has slowly crept up. In 2005 it took 25 minutes to build and test everything. Now it takes 65 minutes, most of which is spent running tests. So we decided to run the longest-running tests (defined at test classes which take > 30 seconds to execute) only once a week. So by default when one of these long-running test is run as part of a group of tests, it doesn't execute; to cause those long-running tests to run within Ant, you'd set a property to indicate this. However, we still wanted to run those tests in, say, Eclipse, because presumably someone who tries to run the test in Eclipse really wants it to run; one way to make this work is to check whether the value of the TERM environment variable is set to "dumb."

I created an annotation called IntegrationTest, which can only be applied to types, not methods. A nice enhancement could be to apply the annotation to methods, too, so that only certain long-running test methods are excluded or included as desired.

To mark test classes as long-running and I annotated each one like so:

@IntegrationTest
@RunWith(JUnit4ClassRunner.class)
The code for JUnit4ClassRunner is (approximately):
package com.example.build;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;

public final class JUnit4ClassRunner extends BlockJUnit4ClassRunner {
private static final String TEST_TYPE_KEY = "test.type";

private static final Logger log =
Logger.getLogger(JUnit4ClassRunner.class.getName());

public JUnit4ClassRunner(Class<?> klass) throws InitializationError {
super(klass);
}

@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
if (shouldRun(method)) {
super.runChild(method, notifier);
} else {
log.info("Not running integration test: " + method.getMethod());
Description desc = Description.createTestDescription(
method.getMethod().getDeclaringClass(), "Integration test");
notifier.fireTestIgnored(desc);
}
}

protected boolean shouldRun(FrameworkMethod method) {
final TestType type = getTestType();

switch (type) {
case UNIT_ONLY:
return !isIntegrationTest(method);
case INTEGRATION_ONLY:
return isIntegrationTest(method);
case ALL:
return true;
default:
return true;
}
}

protected TestType getTestType() {
if (isIntegrationOnly()) {
return TestType.INTEGRATION_ONLY;
} else if (isEclipse() || isAllTests()) {
return TestType.ALL;
} else {
return TestType.UNIT_ONLY;
}
}

/**
* This is a bit of a hack, but seems to work. Eclipse sets the TERM
* environment variable to "dumb"; it's quite unlikely that a user would
* be running tests from the command line with their TERM set to "dumb."
*
* @return true if TERM is "dumb"
*/
protected boolean isEclipse() {
String value = System.getenv("TERM") == null ?
"" : System.getenv("TERM");
return value.equals("dumb");
}

protected boolean isIntegrationOnly() {
String value = System.getProperty(TEST_TYPE_KEY, "");
return value.equals("integration");
}

protected boolean isAllTests() {
String value = System.getProperty(TEST_TYPE_KEY, "");
return value.equals("all");
}

/**
* @param method The FrameworkMethod of some JUnit test method (i.e.
* a method annotated with @Test)
* @return true if the class in which the method is declared is annotated
* with @IntegrationTest
*/
protected boolean isIntegrationTest(FrameworkMethod method) {
Class<?> klass = method.getMethod().getDeclaringClass();
return klass.isAnnotationPresent(IntegrationTest.class);
}
}

Sunday, February 08, 2009

When I was in grad school, I cooked, but it usually wasn't very good. Hamburgers, mac and cheese, spaghetti, stuff like that. Since I finished grad school a few years ago, I haven't had to scrimp, so I haven't cooked much at all. For lunch I've usually gone to the burrito joint across the street. That changed this Christmas when I got a Cuisinart and a bunch of cook books and cooking gear. I've been making my own sorbets for a few weeks now. So far I've made batches of lime-basil, honeydew melon-mint, and raspberry-anise. And for lunch I've found two recipes that taste good reheated. Here's one of them, an Orange Sizhuan Chicken, before it goes into the fridge.


To top it off, eating this way is usually cheaper than going out, although I do have to be careful when I go to Whole Foods. Change is good.

Saturday, February 07, 2009

There's an article over at TPMMuckraker that got me thinking. It's a bit of a digression, but bear with me.

There seem to be two understandings of "bureaucracy," one pejorative, one objective. In the pejorative version, bureaucracy is an organization -- typically governmental, like a regulatory agency, or that pot-bellied guy who works in the zoning department -- characterized by a terrifyingly blind, sometimes petty dedication to, say, filing all of the necessary paper work for your boat license. In the objective version, which I expect is more common amongst social scientists and the like, bureaucracy is an organization characterized by a formal structure, with rules guiding how it is supposed to go about accomplishing its mission.

I prefer the objective one; it's less driven by animus and, being more generic, it lends itself to broader application. It's more useful. With that definition, a corporation is a bureaucracy, which only makes sense: it's an organization with a blind, sometimes petty dedication to profit (if, of course, you assume that corporate governance is not completely broken, which it is). Or an army: an organization with a blind, petty dedication to killing (in the national interest).

There's a stereotype that government bureaucracies are extremely inefficient, but I think that's the wrong way to look at it. They are typically just as efficient at accomplishing the goals of the organization as corporations, the military, and other institutions. They just operate under different resource constraints. A well-run military given the right resources is an efficient killing machine. A well-run technology company given the right size (e.g. IBM) or right position in the technology life cycle (e.g. the early Cisco) is an efficient profit-making machine. And a well-run regulatory agency given the right authority is an efficient regulator of whatever it's regulating.

But like it or not, that's all they do. They don't know how to handle things that conflict with their mission; when that happens, people outside the bureaucracy may have to apply force to cause it to behave correctly. Depending on the degree of asymmetry of power between the outside group and the bureaucracy, something terrible might happen before the outside group succeeds, if they succeed at all.

So, with that in mind, please, dear reader, do remember that while bureaucracies serve a useful function within their own domain, they do little else. Banks, for example, do not love you. They're financial intermediaries, run for a profit, and they want your money; they don't give a damn whether your mother just died of cancer. Actually, I take that back, they do care if your mother just died of cancer, because that may make you more vulnerable and increase the chance that they can trick you into to giving them money to which they have no rightful claim. Bastards.

Thursday, February 05, 2009

Global cooling is not, mmkay?

Saturday, January 31, 2009

Looks like someone deployed a new feature too soon: Google is telling me that Google may be running malicious code.

Monday, January 26, 2009

It sure would be cool to be able to say that you devised a way to sequester carbon dioxide created in the production of cement; creating something that useful is a progressive engineer's dream.

What a meaningless exercise:

Yet one man’s flexibility is another man’s wishy-washiness, and Mr. Obama’s willingness to adapt carries the risk that he will either alienate his liberal base or fail to convert Republicans whose support he hopes to win. During his transition, Mr. Obama managed to charm conservatives; he wooed them at one dinner honoring Mr. McCain, and at another at the home of the columnist George F. Will.

But just days into the Obama presidency, some conservatives sound wary.

“I thought he did very well during the transition on things like the dinner with George Will, and all the words sounded good,” said Newt Gingrich, the Republican former speaker of the House. “But I think they are right at the cusp of either sliding down into a world where their words have no meaning or having to follow up their words with real behavior.”

There was a campaign, then an election, a transition period, and an inauguration. Unsurprisingly, the new administration is new. But note the melodramatic tone: "just days into Obama presidency," "the cusp of sliding down into a world where their words have no meaning." Sigh.

Monday, December 01, 2008

The early reviews of Amazon's CloudFront are in. The question is will Akamai do business differently now? Is Amazon going to eat Akamai's business from the bottom up, much like Windows NT and Linux did (in different ways) for the UNIX workstation market? Akamai's CDN includes real-time communications like VOIP and conferencing. I wonder if Amazon wants into that space too.

Tuesday, November 18, 2008

The opening salvo in the coming war between Amazon and Akamai.

Wednesday, November 05, 2008

That went well, at least for pollster.com. Predicted:

Actual:

Saturday, September 27, 2008

Went hiking in the mountains today with the folks and my nephew. The colors are changing beautifully.


DSC_0106





Saturday, September 20, 2008

Rereading an old networking textbook this afternoon, I ran across this passage:

There is frequently a trade-off between some sort of optimality and scalability. When hierarchy is introduced, information is hidden from some nodes in the network, hindering their ability to make perfectly optimal decisions. However, information hiding is essential to scalability, since it saves all nodes from having global knowledge. It is invariably true in large networks that scalability is a more pressing design goal than perfect optimality.

Computer Networks, A Systems Approach, 3rd ed., p. 318
Larry L. Peterson and Bruce S. Davie
This reminds me of an idea I and a friend have been kicking around lately: software can be optimal only within its own domain, where a domain amounts to a layer in some software stack. Kernels optimize, say, the mappings between virtual and physical pages. Compilers optimize register allocation. Application writers optimize data structures and algorithms. And that's about as good as it gets. As a counterexample, if an application writer were also to have control over and knowledge of register allocation, page tables, i/o buffering, and so on down the stack, she might be able to create a maximally optimal application, but it would come at the cost of scalability of her own productivity. It would take her longer. So efficient use of programmer time requires layering.

At first glance I thought that layering also requires software reuse, but perhaps it doesn't. An ISV, for example, could ship an application for which it wrote a one-off operating system and compiler, with layering dictated by the fact that the application, operating system, and compiler teams were completely separate and communicated only by design specs. But that's inefficient as well. So the spirit of the law of layering requires software reuse, which in turn requires that each layer be as general as possible.

All of which reminds me of the argument David Clark made back in the 80s about layering in networks. I suppose there are equivalent papers about layering in software architecture as well, the most likely (canonical) one being Douglas McIlroy's Mass Produced Software Components.

Thursday, September 04, 2008

Sometimes -- okay, usually -- it seems the Republic Party (might as well return the favor for "Democrat Party") thinks drilling constitutes a comprehensive energy policy. And as if that wasn't apparent before, it sure was clear after Guliani's speech last night in St. Paul:

John McCain will bring about the change that will create jobs and prosperity…let’s talk specifics…John McCain will lower taxes so our economy can grow. He will reduce government spending to strengthen our dollar. He will expand free trade so we can be even more competitive. He will lead us to energy independence so we can be free of foreign oil. And he’ll do it with an all-of-the-above approach, including nuclear power and off-shore drilling.

[ chants of “Drill, baby, drill”]

Giuliani laughs as the audience chants.

Thursday, August 28, 2008

In case you're wondering about the differences among select(2), poll(2), and the device-based polling approaches, they all do fundamentally the same thing -- notify the program when a socket is ready for reading or writing or when an error occurs on a socket. Select() came first in the history of UNIX, but it has a basic limitation on the number of sockets that can be monitored. It's 1024 in most cases, unless you recompile. Poll() solves this problem; it requires the programmer to allocate the array of socket structures that she passes to the system call, so in theory you can monitor an arbitrary number of sockets. (Poll() is also superior to select() in that it gives the programmer finer control over the kinds of events to watch for.) The problem that poll() has only really occurs on servers that handle an enormous number of sockets. That problem is, simply, that the array of socket structures must be copied from user space to kernel space and back again every time you call poll(). So far as I recall, this performance bottleneck came to light in the early 2000s when people were doing research on the scalability of Linux, but that's just a vague memory. The first implementation could have been done in FreeBSD or Solaris. At any rate, the user-kernel-user copy problem is the reason for what I call the device-based approaches (because they involve a file in /dev). With epoll (as it's known on Linux), the program tells the operating system which particular sockets to monitor, and the operating system tells the program when a particular socket has changed. It only tells the program that that particular socket changed. It doesn't say, "Hey, here is the entire of array of sockets you care about, and it's up to you to examine the structures to figure out which ones changed." So device-based polling is useful if you are polling a large number of sockets. Otherwise, plain old poll() should work just fine.

Friday, August 15, 2008

Thinking about domain-specific languages (DSLs) ... Generally, it is easier to keep track of the role of each argument to some function/method in languages with keyword parameters (e.g. Python, Ruby). Names are easier to remember than positions in a parameter list. In a language without keyword parameters, how do you make it easy to remember which parameter is what (putting aside for the moment the usefulness of IDEs in displaying the function/method signature for you)? Here's an example of how to do that in Java. Take this function:

public void validateState(PBXConference conference, int added, int connecting, int connected, int disconnecting, int disconected) {
assertEquals(conference.added(), added);
assertEquals(conference.connecting(), connecting);
assertEquals(conference.connected(), connected);
assertEquals(conference.disconnecting(), disconnecting);
assertEquals(conference.disconnected(), disconnecting);
}

A client would invoke it like:
validateState(conference, 2, 1, 1, 0, 0);

but that sequence of numbers doesn't help the readability of the test. So instead, while it's a bit more verbose, we can change the function definition to:
public class ConferenceStateValidator {
private PBXConference conference;

private ConferenceStateValidator(PBXConference conference) {
this.conference = conference;
}

public ConferenceStateValidator added(int n) {
assertEquals(conference.added(), n);
return this;
}

public ConferenceStateValidator connecting(int n) {
assertEquals(conference.connecting(), n);
return this;
}

public ConferenceStateValidator connected(int n) {
assertEquals(conference.connected(), n);
return this;
}

public ConferenceStateValidator disconnecting(int n) {
assertEquals(conference.disconnecting(), n);
return this;
}

public ConferenceStateValidator disconnected(int n) {
assertEquals(conference.disconnected(), n);
return this;
}

public static ConferenceStateValidator validateState(PBXConference conference) {
return new ConferenceStateValidator(conference);
}
}

And the client (assuming they've statically imported validateState), can do:
validateState(conference).added(2)
.connected(1).connecting(1)
.disconnected(0).disconnecting(0);

which is much cleaner.

Wednesday, August 13, 2008

From Mozilla Labs, an idea whose time is coming. I was struck by this bit though.

Our next step is to gather feedback on the prototype and the ideas behind it. We want to know if the concept has promise and is worth pursuing further. We’re particularly interested in feedback on how messaging might fit into the browsing experience and if there are other interfaces (or refinements to the two interfaces built into the prototype) that would make it easier for users to have online conversations.

We’re still considering what may come after that, but possible extensions to the Snowl prototype include:

  • support for additional message sources, e.g. Facebook, AIM, Google Talk, etc.;
  • an interface for writing and sending messages to enable true two-way conversations;
Since Facebook and Google Talk already support or are going to support XMPP, the only question is whether Snowl will support it too. Other chat services should just follow suit. In other words, there's no point in mentioning "additional message sources" when all of those sources use XMPP. Just mention XMPP!

Thursday, August 07, 2008

Maybe a couple of months ago some blogger whose feed is aggregated at Planet Intertwingly (I don't remember who) wrote a post summarizing his complaints about Erlang. One complaint was about extracting a value from a tuple. Say you assign a tuple to some variable, as in

1> X = {a, 10}.
{a,10}
and you want the value of the second element of the tuple. How do you do that? Conventionally,
2> {_, Y} = X.
{a,10}
3> Y.
10
Now the variable Y has the value 10. (In Erlang, the underscore is the anonymous variable.) If a tuple has a large number of fields or contains nested tuples, such tuple-unpacking statements are unwieldy. And that's what the guy objected to. It's too verbose and easy to botch.

Fortunately, pattern matching provides a simple way to work around this. Define a function that matches the tuple (particularly the first atom)
4> ValueOf = fun({a, Value}) -> Value end.
#Fun
5> ValueOf(X).
10
If you define one function for each element in a tuple, you get accessor methods for the tuple and you don't have to continue writing long expressions with a lot of anonymous variables.

Saturday, August 02, 2008

Here's the second GPS email for Bob's hike:

SPOT Check OK. All is well !! Bob
ESN:0-7425741
Latitude:43.9801
Longitude:-121.8084
Nearest Location: Elk Lake, United States
Distance: 0 km(s)
Time:07/31/2008 22:35:38 (US/Mountain)
http://maps.google.com/maps?f=q&hl=en&geocode=&q=43.9801,-121.8084&ie=UTF8&z=12&om=1

A while ago I tweeted about the possibility of the growth of more continental/regional/local manufacturing in response to the growing costs of transportation. Twitter has some problems preventing me from getting the permalink; even so, it sort of goes without saying that that will happen. Anyway, the Times now has an article about the phenomenon.