Archive for December, 2009

Software Patents: Our Industry’s Nuclear Weapon

2 commentsWritten on December 29th, 2009 by
Categories: Opinions

I'm the kind of guy who believes that any attained knowledge and experience should be shared with your peers. If you come up with a great new idea, then it is in everyone's best interest to share it with the world and let as many people as possible benefit from it. That's probably the biggest reason why i love the idea of Open Source Software so much, though i'm not dogmatic about it. Then again, a lot of people aren't interested in what is in everyone's best interest and are merely looking out for themselves or their shareholders.

In the software development industry, many of the large corporations have been quite busy on the software patent front in the last couple of years. Every single idea or innovation that those companies could get patented has probably been patented by now. And for what? What's the large scale benefit that has come from it? Some small companies have won patent suits against big companies. And who benefited from that? The consumer? Not really. A very large minority of people benefited from software patents on a financial level at best. Our industry sure didn't benefit from it. The consumer sure as hell never benefited from it.

I've always compared software patents to nuclear weapons. Every large country wants them, yet nobody truly wants to use them unless they have no other choice. If a large country ever uses nuclear weapons, they know that retaliation will occur and that there is no true benefit. In the case of nuclear weapons, once they are used everybody loses. It's pretty much the same thing with software patents. Some small companies who are only interested in a quick profit will be glad to use them, but only if they know damn well that they can't get hit with a counter-suit for patent infringements that they might have been guilty of. But big companies? How many of them have truly benefited from them? The only value they've shown to have so far is as a defensive measure. As in: if you sue us for patent infringement, you know damn well that we will counter sue you based on the patents that we have and that you are surely violating. A nice example of that is the current Apple-Nokia dispute which will end up costing only Apple and Nokia money and not a single consumer is actually going to benefit from that. The only people that will truly benefit from it are the lawyers representing both parties.

So really, what's the point in software patents? Microsoft has amassed a boatload of patents in the past 10 years or so, and what do they have to show for it? They lost one patent dispute which prevents them from selling certain version of MS Word and none of the patents they hold is going to save them from that particular ruling. In fact, can you name any situation where a big company truly benefited from a software patent they hold? Sure, Amazon has its One-Click patent but that didn't really prevent other e-commerce sites from offering a highly usable alternative. I can't really think of any other situation where a patent has really benefited a large corporation.

The only people who've truly benefited from software patents so far have been niche companies who happened to got lucky, and lawyers representing both sides in the disputes. That's it. If a small company or individual happens to have had a certain idea or implementation patented at the right time, they could simply wait long enough until they could sue a large company for infringing on their patent and watch the money come in. The large companies (you know, the ones we buy our products from) simply add their expenses to the cost price of the products that we buy. Does that sound fair to you? And before you go all "we should protect individuals and small companies" on me, think very carefully on how many of those patents have been awarded in a fair manner. I don't really follow software patents that closely, but as a former Linux user who was concerned for some of his favorite software projects, i've seen my share of awarded patents where there truly was quite a bit of 'prior art' that should have prevented the patent from ever being awarded in the first place.

The thing is... if you have a great idea and want to profit from it, then you need to market it right and provide the best possible consumer experience. You know that countless competitors are going to copy your ideas, and most of them will do it without being guilty of infringing your worthless patent. No matter how valuable your idea is, it doesn't mean anything if the total package doesn't entice the consumer to purchase your product over someone else's. So why even bother with software patents in the first place?

Recommended Book: 97 Things Every Project Manager Should Know

2 commentsWritten on December 29th, 2009 by
Categories: Books

As i mentioned in my (short) review of '97 Things Every Software Architect Should Know', i love the format of these "97 Things" books. I already pre-ordered the upcoming '97 Things Every Programmer Should Know' and was curious about the other book in the series: 97 Things Every Project Manager Should Know.

Now, i'm not a project manager and have no intentions of becoming one in the future. But as a software developer, i do think it's important to have an understanding of what good (and bad) project management entails so i'd recommend this book to project managers as well as developers. As usual with books in this series, it's full of useful, real-world advice and common sense contributed by experienced and established project managers. Each piece of advice is again only 2 small pages, and no topic is ever discussed too in-depth. While not everyone will get something out of every bit of advice, there surely will be quite a few that'll make you think about improvements that can be made in your company.

While there is no structure in the order of the tips, the book does list each tip by topic in the beginning. Topics that are covered are agile methods, general software development, managing people and teams, dealing with distributed teams, improving communication, managing stakeholders, project processes, dealing with requirements, dealing with end-users and self-management. The book claims to be useful to project managers in general and not just for software projects, though i think non-software project managers will probably not get a lot of valuable information out of this book since the large majority of the tips are rather specific to software development.

Definitely worth reading.

New Year’s Resolutions

6 commentsWritten on December 27th, 2009 by
Categories: About The Blog, Opinions

These are the technical commitments i’d like to make for 2010:

Keep improving Agatha

It hasn’t been long since i open-sourced it but i already consider it one of the best decisions i’ve made so far.  Most of it was written about 18 months ago, and it had been almost a year since any modifications were made to it before open-sourcing it.  Some new features have already been added to it, and there are some more cool new features coming up in the next 2 releases.  Hopefully, we (the people who work on it and the people who use it) can keep coming up with improvements.

I’d obviously like to see more people using it and increased acceptance in general, but that’s something that will take time and more effort, if it will ever happen that is.  If it doesn’t happen, then the people who did take a chance on it will at least get to benefit from the effort that will be put into it.

Get more involved with NHibernate again

My last commit for NHibernate dates back to August 19th.  Definitely not proud of that, but i certainly intend to get back to contributing actively.  While it’s not a very pleasant code-base to work with, i did find fixing issues for NH to be rather satisfying because you know you’re either helping a few people, or a hell of a lot of them depending on the impact of the bug.  I’d also love to clean up a lot of the query batching code because it is quite messy and repetitive.   If any of you tracks NHibernate’s development and you don’t see a commit from me for a while, feel free to bitch at me because of it :)

Increase acceptance of QuickNet

I’m starting to love QuickNet more and more.  In the first post i wrote about it i mentioned that i thought it was a revolutionary step in automated testing (at least for the .NET world) and i still stand by that statement.  I’d go as far as saying that if you don’t see the potential and the value of it, that i can’t help but question your willingness to improve the quality of your work in general.  I was actually very disappointed with the lack of feedback that i got on my last post about it, even though i specifically asked you to comment on it.  Obviously, i can’t expect everyone to leave comments, but there are quite a few of you who claim to be interested in automated testing and writing not only clean but also correct code so i was certainly surprised that a lot of those people didn’t respond to the questions i asked at the end of that post.  I’m pretty sure that once a big name blogger jumps on it, most of you will jump on the bandwagon as well.  Until that time comes, i’m going to continue writing about it and trying to get people interested in using it.  If you don’t look forward to those posts, you might want to unsubscribe from my feed right now :)

Keep blogging

It’s been a pretty successful year for this blog.  I plan to keep up the amount of effort i put into it, and i also want to continue building on some core principles that are pretty much the foundation of not only this blog, but also what i do and how i do it.  I want to keep learning.  I want to keep sharing knowledge and experience.  I want to keep offering my honest and open opinions, no matter how frank or blunt they may be at times or who they might rub the wrong way. I want to remain true to myself.  I will keep treating people the same, no matter how little or big their name is.  A good idea is a good idea and a bad idea is a bad idea and i will continue on treating them as such, no matter who or where it came from.  

My wish for you

I wish everyone the best for the following year, and i particularly wish for you to understand and be inspired by the following quote:

go farther, go further, go harder
is that not why we came?
and if not, then why bother?

Bonus points to whoever can name the author of that without googling it :)

Testing Agatha’s Caching Functionality With QuickNet

9 commentsWritten on December 24th, 2009 by
Categories: agatha, quicknet, testing

In this post i’m going to give you a very detailed explanation of a QuickNet test that i wrote for Agatha’s caching layer.  If i do my job well, you’ll have a much better view on what QuickNet does, how it works, and how it can help you.  I do want to ask you to keep a very open mind and to forget pretty much everything that you know about automated testing, including your opinions on what should or should not be done in an automated test.  If you have problems with that, you might be interested in more conservative reading on automated testing.  Still here? Alright, let’s get started.  Please stay focused throughout, because you will need it :p

First of all, requests whose response is eligible for caching must override the Equals method and the GetHashCode method.  I’ll use the following two request/response combinations for this test:

    [EnableResponseCaching(Seconds = 1)]

    public class FirstCachedRequest : Request

    {

        public string String { get; set; }

 

        public bool Equals(FirstCachedRequest other)

        {

            if (ReferenceEquals(null, other)) return false;

            if (ReferenceEquals(this, other)) return true;

            return Equals(other.String, String);

        }

 

        public override bool Equals(object obj)

        {

            if (ReferenceEquals(null, obj)) return false;

            if (ReferenceEquals(this, obj)) return true;

            if (obj.GetType() != typeof(FirstCachedRequest)) return false;

            return Equals((FirstCachedRequest)obj);

        }

 

        public override int GetHashCode()

        {

            return (String != null ? String.GetHashCode() : 0);

        }

    }

 

    public class FirstCachedResponse : Response { }

 

    [EnableResponseCaching(Seconds = 2)]

    public class SecondCachedRequest : Request

    {

        public int Integer { get; set; }

 

        public bool Equals(SecondCachedRequest other)

        {

            if (ReferenceEquals(null, other)) return false;

            if (ReferenceEquals(this, other)) return true;

            return other.Integer == Integer;

        }

 

        public override bool Equals(object obj)

        {

            if (ReferenceEquals(null, obj)) return false;

            if (ReferenceEquals(this, obj)) return true;

            if (obj.GetType() != typeof(SecondCachedRequest)) return false;

            return Equals((SecondCachedRequest)obj);

        }

 

        public override int GetHashCode()

        {

            return Integer;

        }

    }

 

    public class SecondCachedResponse : Response {}

 

Agatha’s Request Processor should cache responses for requests that are eligible for caching, and when subsequent requests need to be processed which are equal (in their values) to previous requests whose response has been cached already, the cached response needs to be returned instead of handling the request again.

In order to inspect the usage of the caching layer during tests, i wrote the following CacheManagerSpy class which inherits from my normal CacheManager class (which is used by Agatha):

    public class CacheManagerSpy : CacheManager

    {

        private List<CacheEntry> cacheEntries;

        private List<Response> returnedCachedResponses;

 

        public IEnumerable<CacheEntry> CacheEntries

        {

            get { return cacheEntries; }

        }

 

        public IEnumerable<Response> ReturnedCachedResponses

        {

            get { return returnedCachedResponses; }

        }

 

        public CacheManagerSpy(CacheConfiguration configuration, ICacheProvider cacheProvider) : base(configuration, cacheProvider)

        {

            Clear();

        }

 

        public void Clear()

        {

            cacheEntries = new List<CacheEntry>();

            returnedCachedResponses = new List<Response>();

        }

 

        protected override Response GetCachedResponseFor(Request request, string region)

        {

            var cachedResponse = base.GetCachedResponseFor(request, region);

            returnedCachedResponses.Add(cachedResponse);

            return cachedResponse;

        }

 

        protected override void StoreInCache(Request request, Response response, TimeSpan expiration, string region)

        {

            cacheEntries.Add(new CacheEntry(request, response, expiration, region));

            base.StoreInCache(request, response, expiration, region);

        }

 

        public class CacheEntry

        {

            public Request Request { get; private set; }

            public Response Response { get; private set; }

            public TimeSpan Expiration { get; private set; }

            public string Region { get; private set; }

 

            public CacheEntry(Request request, Response response, TimeSpan expiration, string region)

            {

                Request = request;

                Response = response;

                Expiration = expiration;

                Region = region;

            }

        }

    }

 

This Spy basically gives me the ability to see which entries are stored in the cache, and which responses have been returned.

Now i can create a QuickNet Acid test.  I’m going to go over the code step by step instead of listing it all at once because each step probably needs quite a bit of explanation if you don’t understand QuickNet yet.

First of all, here’s the definition of the test:

    public class RequestProcessorCachingSpecs : AcidTest

    {

        public RequestProcessorCachingSpecs() : base(10, 2000) { }

 

The 10 means that QuickNet will perform 10 testruns.  The 2000 means that each testrun will contain 2000 executions of the defined transitions.  Don’t worry yet about what a transition is, i’ll get to that later on in the post.

We’ll also need some fields to access some of the classes that will be used in this test:

        protected static IRequestHandler<FirstCachedRequest> firstCachedRequestHandler;

        protected static IRequestHandler<SecondCachedRequest> secondCachedRequestHandler;

        protected static IRequestProcessor requestProcessor;

        protected static CacheManagerSpy cacheManager;

 

Yeah, i know what you’re thinking.  Static is bad right?  Well, not in this case since we need to be able to use these instances in some inner classes that we’ll go over soon enough and these references will hold the same instance for each testrun (which, as mentioned consists of 2000 transition executions).  Anyway, forget about the static keyword for a second and keep reading.

Now we can implement the SetUp of our testrun:

        public override void SetUp()

        {

            IoC.Container = new Agatha.Castle.Container();

 

            var serviceLayerConfiguration = new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(),

                Assembly.GetExecutingAssembly(), IoC.Container)

            {

                BusinessExceptionType = typeof(BusinessException),

                SecurityExceptionType = typeof(SecurityException),

                CacheManagerImplementation = typeof(CacheManagerSpy)

            };

            serviceLayerConfiguration.Initialize();

 

            // i want to take advantage of the automatic initialization, so i'm just resolving the requestprocessor instead of creating it

            requestProcessor = IoC.Container.Resolve<IRequestProcessor>();

            // the cache manager is a singleton so i can just resolve it and it'll be the same one the request processor uses

            cacheManager = (CacheManagerSpy)IoC.Container.Resolve<ICacheManager>();

 

            firstCachedRequestHandler = MockRepository.GenerateMock<IRequestHandler<FirstCachedRequest>>();

            secondCachedRequestHandler = MockRepository.GenerateMock<IRequestHandler<SecondCachedRequest>>();

 

            IoC.Container.RegisterInstance(firstCachedRequestHandler);

            IoC.Container.RegisterInstance(secondCachedRequestHandler);

        }

 

This setup code will be executed for each testrun.  So QuickNet will execute this 10 times, and each time it will run 2000 transition executions on the classes that we set up for the current testrun. 

So what exactly is a transition? It’s just a piece of code that you want to execute.  That piece of code can receive input and it can return output.  It’s basically the code that you’re testing.  Our transition will simply consist of calling Agatha’s Request Processor and instructing it to process 2 requests.  Those requests would be the two request types shown earlier which are eligible for caching.  After QuickNet executes the transition, it will also verify that our defined specifications for that particular transition are indeed correct.

Let’s define the input that our transition will receive.  In this particular case, we need a Request instance, an instance of a Request Handler for that Request, and the Response that the Request Handler needs to return if there’s no valid Response available in the cache.  So we’ll use the following helper class to define this input:

        public class ProcessInputElement

        {

            public IRequestHandler RequestHandler { get; private set; }

            public Request Request { get; private set; }

            public Response Response { get; private set; }

 

            public bool RequestHandlerWasExecuted { get; private set; }

 

            public ProcessInputElement(IRequestHandler requestHandler, Request request, Response response)

            {

                RequestHandler = requestHandler;

                Request = request;

                Response = response;

            }

 

            public void StubHandler()

            {

                RequestHandler

                    .Stub(r => r.Handle(Arg<Request>.Is.Same(Request))) // WHY: reference check iso equality check

                    .Return(Response)

                    .WhenCalled(a => RequestHandlerWasExecuted = true)

                    .Repeat.Once();

            }

        }

 

An instance of this class basically combines the Request, its Request Handler and the Response that needs to be returned.  It also contains a helper method so we can easily instruct the mocked Request Handler to return the given Response when it is asked to handle the given Request.  And we’ll also set a simple flag to true if the Request Handler has indeed been called.

As i mentioned a few times already, QuickNet will execute our transition (which i’ll show later on in the post) 2000 times for each testrun.  Obviously, we can’t be expected to create these instances manually all the time, so we need to define some generators that QuickNet will be able to use to randomly generate the input that it can pass to the transition.  We first need two generators which are capable of generating random instances of the FirstCachedRequest and SecondCachedRequest types:

        public class FirstRequestGenerator : BaseGenerator<FirstCachedRequest>

        {

            public FirstRequestGenerator()

            {

                AddGeneratorForProperty(r => r.String, new StringGenerator(1, 1)); // generates a random string of 1 character

            }

        }

 

        public class SecondRequestGenerator : BaseGenerator<SecondCachedRequest>

        {

            public SecondRequestGenerator()

            {

                AddGeneratorForProperty(r => r.Integer, new IntGenerator(0, 5));

            }

        }

 

The FirstRequestGenerator class is capable of returning a new instance of FirstCachedRequest whenever it is asked to do so, and it will populate the FirstCachedRequest instance’s String property with a random string consisting of one character.   Some of the generated instances will be considered equals of each other if the generated string value is equal, and some will not be considered as equals if the generated string value is not equal.  The SecondRequestGenerator basically does the same thing, except that it will populate the SecondCachedRequest instances’ Integer property with a random int value between 0 and 5.  So again, some of the generated instances will be considered as equals, and some won’t.   The important part to remember is that QuickNet will generate random instances for us and that we don’t have to worry about it.

Now that we have the generators for the request instances, we can write a generator which can generate random ProcessInputElement instances:

        public class ProcessInputTupleGenerator : BaseGenerator<Tuple<ProcessInputElement, ProcessInputElement>>

        {

            private FirstRequestGenerator firstRequestGenerator = new FirstRequestGenerator();

            private SecondRequestGenerator secondRequestGenerator = new SecondRequestGenerator();

 

            protected override Tuple<ProcessInputElement, ProcessInputElement> GetDefaultInstance()

            {

                var firstElement = new ProcessInputElement(firstCachedRequestHandler, firstRequestGenerator.GetRandomValue(),

                                                           new FirstCachedResponse());

                var secondElement = new ProcessInputElement(secondCachedRequestHandler, secondRequestGenerator.GetRandomValue(),

                                                            new SecondCachedResponse());

 

                return Tuple.New(firstElement, secondElement);

            }

        }

 

Actually, this generator doesn’t just generate one instance of ProcessInputElement, it generates a tuple consisting of two ProcessInputElement instances.  The first value in the tuple will always contain a random instance of FirstCachedRequest, its Request Handler and a FirstCachedResponse instance that needs to be returned by the Request Handler if there’s no cached response yet.  The same goes for the second value in the tuple, except that it has a random instance of SecondCachedRequest, its Request Handler and a SecondCachedResponse instance.

Again, every time QuickNet will execute our transition, it will ask the ProcessInputTupleGenerator to generate a new tuple, and that tuple will contain the random request instances.  The generated tuple is then passed into the transition and the transition will be executed.

Now i can finally show you the transition that we’ll define:

        public class ProcessRequestsTransition : MetaTransition<Tuple<ProcessInputElement, ProcessInputElement>, Response[]>

        {

            public ProcessRequestsTransition()

            {

                Generator = new ProcessInputTupleGenerator();

                Execute =

                    input =>

                        {

                            cacheManager.Clear(); // clears the spy for every execution of this transition

 

                            input.First.StubHandler();

                            input.Second.StubHandler();

 

                            return requestProcessor.Process(new[] {input.First.Request, input.Second.Request});

                        };

            }

        }

 

As you can see, the ProcessRequestsTransition inherits from the MetaTransition<TInput, TOutput> class.  So we basically just defined that the input of the transition is a Tuple<ProcessInputElement, ProcessInputElement> and the output is an array of Response objects.  In the constructor of the transition, we define that the generator to be used to generate the input values is the ProcessInputTupleGenerator.  And we also define what actually needs to be executed by the transition.  In this case, the piece of code that will always be executed will first clear the CacheManagerSpy instance (so we can safely inspect it during our specification verification), it will then prepare both Request Handler mocks and then it simply calls the Process method of the Request Processor.  It’s output (a Response array) will be the output of the transition since that is our return value.

After all that work, we can finally define our specifications for this transition.  Let’s start with the first one:

        [SpecFor(typeof(ProcessRequestsTransition))]

        public Spec ResponsesAreCachedIfTheyArentInTheCacheYet(Tuple<ProcessInputElement, ProcessInputElement> input, Response[] output)

        {

            return new Spec(() =>

                    {

                        Action<ProcessInputElement> verify =

                            element =>

                                {

                                    Ensure.Equal(element.Response, cacheManager.CacheEntries.First(e => e.Request.Equals(element.Request)).Response);

                                    Ensure.True(output.Contains(element.Response));

                                    Ensure.True(element.RequestHandlerWasExecuted);

                                };

 

                        if (cacheManager.CacheEntries.Any(e => e.Request.Equals(input.First.Request)))

                        {

                            verify(input.First);

                        }

 

                        if (cacheManager.CacheEntries.Any(e => e.Request.Equals(input.Second.Request)))

                        {

                            verify(input.Second);

                        }

                    })

                .IfAfter(() => cacheManager.ReturnedCachedResponses.Any(r => r == null));

        }

 

The SpecFor attribute makes it possible to define which Transition this Spec belongs to.  The method has 2 parameters, which correspond with the input that will be passed into the transition, and the output that the transition returned.  It’s very important to realize that this method is executed before the transition is executed (yet after the input has been generated), but the block of code that we pass into the Spec instance will only be executed after the transition has been executed.  Actually, that block of code will only be executed if the Spec’s precondition and postcondition have been satisfied.  The precondition (which can be defined with the Spec class’ If method) will be evaluated before the transition is executed.  If it evaluates to false, the spec will be ignored for the current execution of the transition.  The postcondition (which can be defined with the Spec class’ IfAfter method) is evaulated after the transition is executed.  If it evaulates to false, the spec will be ignored.  If it evaluates to true, the spec will be verified using the input that was passed into the transition and the output that was returned from the transition.

For this particular spec, we define a postcondition that the spec only needs to be verified if the CacheManager returned one or more null values instead of cached responses.  If the CacheManager returns a null, it means that there was no cached response for the request it was given.  And that is when we need to verify the following things:

  1. The request was handled by the Request Handler
  2. The response that was returned by the Request Handler has been put in the cache
  3. The response array that was returned by the Request Processor contains the expected response

We perform this check for both requests, or only one of them, depending on which one was not in the cache yet.  Remember that the transition will be executed 2000 times using the same request processor and the same cache manager.  Which means that cached responses from previous transition executions might still be in the cache.  Also keep in mind that one of the request types has a defined expiration of 1 second, and the other an expiration of 2 seconds which means that there will be plenty of transition executions where they are either both in the cache, only one of them, or none of them.  All of those cases are now covered with the code in this spec.

Here’s another spec:

        [SpecFor(typeof(ProcessRequestsTransition))]

        public Spec CachedResponsesAreReturnedWhenAvailableInsteadOfCallingTheHandler(Tuple<ProcessInputElement, ProcessInputElement> input, Response[] output)

        {

            return new Spec(() =>

                        {

                            Action<ProcessInputElement> verify =

                                element =>

                                    {

                                        Ensure.False(element.RequestHandlerWasExecuted);

                                        Ensure.True(output.Contains(cacheManager.ReturnedCachedResponses.First(r => r != null && r.GetType() == element.Response.GetType())));

                                    };

 

                            if (cacheManager.ReturnedCachedResponses.Any(r => r != null &&  r.GetType() == input.First.Response.GetType()))

                            {

                                verify(input.First);

                            }

 

                            if (cacheManager.ReturnedCachedResponses.Any(r => r != null && r.GetType() == input.Second.Response.GetType()))

                            {

                                verify(input.Second);

                            }

                        })

                .IfAfter(() => cacheManager.ReturnedCachedResponses.Any(r => r != null));

        }

 

Here we verify that when a cached response was returned by the CacheManager:

  1. The Request Handler was not executed
  2. The response array returned by the Request Processor contains the cached response

We again perform this check for both requests, or only one of them depending on which one had a cached response.

You might think that this was a lot of work, but was it really?  It’s about 160 lines of code, and i’m quite sure that the functionality that i’m testing is covered much more thoroughly than it would’ve been with 160 lines of code for classical unit tests.   In fact, i challenge each one of you to come up with a more thorough test for this in less than 160 lines of code.  Also, i can keep adding specs quite easily and those really don’t contain that much code.  But my functional coverage would increase dramatically, much more than it would with classical unit tests.

Granted, the learning curve is steep as i mentioned already in a previous post.  But once you start to get it, you become pretty productive with it and you’ll catch a lot more bugs before you deliver your software because of it.

Now, i tried hard to make everything here as clear as possible, so i’d love to hear from you whether or not:

  1. you understood it
  2. you like it
  3. you think it’s way over the top
  4. you want to start doing this too

How QuickNet Found 2 Bugs That You And I Didn’t

4 commentsWritten on December 22nd, 2009 by
Categories: agatha, quicknet, testing

I recently posted the first draft of the caching implementation of Agatha.  You might want to take another look at the piece of code that deals with processing requests in the Request Processor.  I thought it was alright.  And if only 10% of my readers actually read that code, then it means that about 200 people thought it was alright too because nobody mentioned a possible problem with that part.  Today i was adding some cached requests/responses to the RequestProcessor’s QuickNet exception handling test.  And lo and behold, 2 bugs showed up that i hadn’t anticipated.

Before i discuss the two bugs, i’d like to explain what the exception handling quicknet test does, and how it works.  It basically calls the request processing code a bunch of times, each time with a set of requests where either none, one or more of the request handlers will throw an exception during the handling of the requests.  The order of the exceptions or which handlers will throw an exception is completely randomized (thanks to QuickNet) and thus it varies from test to test.  This particular QuickNet test will perform 50 testruns, where each testrun consists of 50 transitions that will be executed.  Each transition is a piece of functionality that will be executed (in this case, the processing of requests) with randomized input (in this case, there is a fixed set of requests but which ones will fail will differ with each run of the transition) and after a transition is executed, the relevant specs are verified (in this case, a bunch of checks to make sure that the whole error handling functionality always does what it needs to do) to make sure that the piece of code that you’re testing (your transition, in this case the processing of requests) actually works.

Instead of testing with a mocked caching layer, i used the real thing in the tests.  I also added 2 cached request types to the test request types, and gave one of them an expiration of 1 second, the other an expiration of 2 seconds.  This means that during the entire QuickNet testrun, some requests will return cached responses, some won’t, some cached responses will expire and subsequent requests of that particular type need to be executed and have their responses cached again until they expire.   In the meantime, QuickNet is constantly firing requests at the RequestProcessor, and some of them will randomly fail.  Our specs need to verify that the returned responses always contain the correct exception information for the batch of requests that was processed.

And here’s the beauty of the whole thing.  I hadn’t even thought about how exception handling also influences how you deal with cached responses.  I didn’t take exception handling into account when adding the caching code, and without even adding specific tests to see whether the caching worked correctly, my QuickNet specs that intended to cover something that doesn’t really have anything to do with caching uncovered 2 issues with my caching code.   I was suddenly getting error messages from QuickNet that in some cases, the exception information wasn’t correct.   And it frequently took over 30 executions of the transition, or sometimes multiple testruns before some of those specs would fail.  The longer it takes for a bug to show up in a QuickNet test, the better the bug is at hiding and the harder it is to find it manually because it almost certainly is an edge-case that you hadn’t considered at all and that only occurs in certain situations.

And those are the type of bugs that you typically don’t find with traditional unit tests.  When you write traditional tests that are aimed at asserting correct behavior, you typically only write tests for the problems that you thought about.  Sometimes you’ll get lucky and traditional unit tests will inform you of incorrect behavior that you didn’t think of,  but it rarely happens for the edge-cases.  And those are the bugs that can be really hard and painful to find and fix.

By now you’re probably wondering what the two bugs are.  The first one is that if an exception occurs during the handling of a request which is eligible for caching, the response with the exception information would be stored in the cache, and the next time that that particular request would be processed while the cached response hadn’t expired yet, you’d simply get the original response with the exception information again instead of actually handling the request again (which might have been handled successfully this time).  The second bug was that if an earlier request in the batch of requests already failed, it could possibly return a valid response later on in the same batch for one of the cached requests if its response hadn’t expired yet.   Pretty stupid huh?  But they were there nevertheless, and sooner or later somebody would’ve tripped over it and it would’ve caused frustration for the user who would run into it, and pain for me because i’d have to go looking for it long after i’d written the code.

Again, i seriously doubt that traditional unit tests would’ve prevented the existence of these 2 problems since it never occurred to me while i was writing it.  The QuickNet tests did find it, even before i added specific tests for the caching functionality to them, which i found pretty impressive and a huge benefit to this sort of testing.  Granted, the learning curve for writing valuable QuickNet tests is steep (i’m not even halfway there IMO) and it takes a lot of effort when you’re starting out with it.  But if it helps me prevent the sort of bugs that are otherwise easily missed and could negatively influence the perception of my project, then i definitely consider it worth it.