Loading ...
Sorry, an error occurred while loading the content.

Re: [XP] Test-first an infinite loop

Expand Messages
  • Adam Sroka
    ... I have never TDDed the loop per se. What I would typically do is TDD the interesting stuff that happens in the loop. I would also write some end-to-end
    Message 1 of 31 , Apr 5, 2010
    • 0 Attachment
      On Mon, Apr 5, 2010 at 12:38 PM, Curtis Cooley <curtis.cooley@...> wrote:
      >
      >
      >
      > On Mon, Apr 5, 2010 at 8:59 AM, mthlr <mackinnon.m@...> wrote:
      >
      > >
      > >
      > > I'm not sure I understand where the issue is.
      > >
      > > The infinite loop is an implementation choice.
      > > TDD says we need to write our test first and then do the simplest thing to
      > > make the tests go green.
      > > It seems like we're picking the solution and then trying to figure out the
      > > test to check for it (which isn't really tdd, is it?)
      > >
      > > Or am I misreading this discussion?
      > >
      > > On another hand, there are likely implementations that we can't reach (as
      > > I'd imagine Godel's incompleteness theorem (from mathematics) can apply
      > > here)
      > >
      > > Just a touch confused...
      > >
      > I don't think you are confused at all. I think you are spot on. Bill is
      > asking in the situation where an infinite loop "will" be the implementation
      > how, or is it even possible, does one get there TDD.
      >
      > By "will be" I mean the obvious and simplest implementation appears to be an
      > infinite loop. So we sit down and start writing tests to see where it will
      > go. Has anybody ever done this and gotten to the infinite loop?
      >

      I have never TDDed the loop per se. What I would typically do is TDD
      the interesting stuff that happens in the loop. I would also write
      some end-to-end test that forced me to deploy the code into a running
      app.

      At that point I would most likely implement some daemon (Service)
      interface that was appropriate to the platform I was deploying it on.
      Hopefully, that would make the end-to-end tests pass.

      If I were feeling ambitious I also might write some end-to-end tests
      for things that I thought would cause the loop to break (Like sending
      a signal) and what I expected to happen after that. Where possible, I
      would isolate this interesting behavior and write micro tests for it.
    • Bill Michell
      Well, an infinite loop is definitely a special case construct - not something that you want to see in normal usage. Sounds like what we are looking for is a
      Message 31 of 31 , Apr 9, 2010
      • 0 Attachment
        Well, an infinite loop is definitely a special case construct - not something that you want to see in normal usage.

        Sounds like what we are looking for is a good use case - maybe a daemon that keeps running some processing method even when given no time by the current thread.

        So I'd make the processing method simply increment a counter.

        The first test just sets the counter to 0 and spawns a thread that runs our main method. The test then checks that the counter value eventually becomes at least 1 - and hence that our processing method got called. Note that spawning the thread is probably part of the test, not the method under test.

        No loop required - just a call to our processing method.

        The next test spawns the thread again, but this time checks the counter eventually becomes at least, say, 10.

        I'd posit that the simplest way to pass the test is to surround the worker method call with a while(1) construct. If you wanted to go via ten calls to the worker method first, knock yourself out.

        For completeness, I'd probably write a test that checks the counter with a decent time interval between the checks, and show that the counter value was continuing to increment. If you haven't reached while(1) already, then this test will surely get you there...

        Yes, it isn't a fast test, and yes, it involves spawning threads. Not pretty. But I'd argue that you are working with a special use case here - even though on the face of it, the construct is a really simple one. It also gives a nice clean design (separating the "never stop" from the "processing" stuff) that lets you do nice things like checking error conditions and termination conditions if those ever become interesting.

        On 8 Apr 2010, at 23:09, Tom wrote:

        > Eggzackly - hence my hesitation.
        >
        > --- In extremeprogramming@yahoogroups.com, Adam Sroka <adam.sroka@...> wrote:
        > >
        > > On Thu, Apr 8, 2010 at 11:06 AM, Tom <rossentj@...> wrote:
        > > >
        > > >
        > > >
        > > > I hesitate to suggest such ugly complexity - but ... maybe have the test start the target in a separate thread which it can kill when it's satisfied ... in a "finally" clause, of course....
        > > >
        > >
        > > At which point it may no longer be the simplest thing that could
        > > possibly work ;-)
        > >
        > > How would you change the test so that it was?
        > >
        >
        >

        --
        Bill Michell
        billmichell@...






        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.