Archive for the ‘programming’ Category
Integration testing HTTP service caller using PostBin.org
Problem: Post-Receive service hooks in GitHub are great. However, the lack all kind of flexibility chosing which commiters and/or branch of a repository the service hooks should be applied to. The other end, Hudson CI in this case, of the service hook call has the same problem.
Solution: As many other have, I started out to write a HTTP proxy which could provide some conditional logic for when to proceed with a Post-Receive call.
I started out driving the development of my proxy using TDD. My initial requirement was to NOT proceed with Post-Receive calls for certain commiters. So I started with the following failing end-to-end tests:
[Test]
public void GivenATargetAndAnIgnoredCommiter_WhenPostingPayloadWithCommitsByIgnoredUserOnly_ThenTargetWillNotBeCalled() {}
[Test]
public void GivenATargetAndAnIgnoredCommiter_WhenPostingPayloadWithCommitsWithAMixOfIgnoredAndNotIgnoredUsers_ThenTargetWillBeCalled() {}
After a while of hacking around I ended up with the following tests which tested my needs of the logic in my little service hook proxy:
[Test]
public void GivenATargetAndAnIgnoredCommiter_WhenPostingPayloadWithCommitsByIgnoredUserOnly_ThenTargetWillNotBeCalled()
{
var postReceiveHook = GivenATargetAndAnIgnoredCommiter();
Payload payload = PayloadWithCommitsByIgnoredUserOnly();
postReceiveHook.Post(payload);
Assert.AreNotEqual( payload, _testTarget.Received );
}
[Test]
public void GivenATargetAndAnIgnoredCommiter_WhenPostingPayloadWithCommitsWithAMixOfIgnoredAndNotIgnoredUsers_ThenTargetWillBeCalled()
{
var postReceiveHook = GivenATargetAndAnIgnoredCommiter();
Payload payload = PayloadWithCommitsWithAMixOfIgnoredAndNotIgnoredUsers();
postReceiveHook.Post(payload);
Assert.AreEqual( payload, _testTarget.Received );
}
But these tests, are they really end-to-end tests involving a Hudson CI instance?
Let’s take a look at the class behind the _testTarget field:
internal class TestTarget : IPostReceiveTarget
{
public HttpStatusCode Call( Payload payload )
{
Received = payload;
return HttpStatusCode.OK;
}
public Payload Received { get; private set; }
}
I don’t think so!
But do I want to bundle a complete Hudson CI instance with my NUnit test project? No! So where to draw the line?
Let’s try to find out what we actually want here.
- Is it important to actually verify that the Hudson CI server can process the GitHub Post-Receive Service Hook JSON payload? Nah.
- Is it important to verify that it can forward requests to different target URLs (Hudson jobs)? Nah.
- Is it important to verify that it can receive a payload from GitHub and then resend the same payload to an arbitrary HTTP POST target? Yes!
Let’s take a look at the interface I wrote to separate the conditional logic from faking HTTP requests to Hudson CI in the previous tests:
public interface IPostReceiveTarget
{
HttpStatusCode Call( Payload payload );
}
Right here I felt a bit bored because I started out writing the HTTP POST service call gateway, no tests involved.
Ended up with the following piece:
public class HttpPostTarget : IPostReceiveTarget
{
public HttpPostTarget( string targetUrl )
{
Url = targetUrl;
}
public HttpStatusCode Call( Payload payload )
{
var wc = new WebClient {Encoding = Encoding.UTF8};
wc.Headers.Add( HttpRequestHeader.ContentType, "application/json" );
try
{
wc.UploadString( Url, JsonConvert.SerializeObject( payload ) );
return HttpStatusCode.OK;
}
catch ( WebException ex )
{
var response = ex.Response as HttpWebResponse;
return response == null ? HttpStatusCode.BadRequest : response.StatusCode;
}
}
public string Url { get; private set; }
}
So what do you say? This code can’t be tested? Well, I could go down the road and write a test which utilizes the HttpListener and verify the payload received by the listener in the test. Doable, yes indeed, but earlier today I was wasting some hour or two on getting the HttpListener to work with Windows Firewall (a.k.a. the “netsh” ceremony) which totally failed since I was hosting the code on a network share on my Mac and trying to fire up the listener on the Windows 7 Parallels VM….Aaarghh! Frustration! I just want the simplest possible thing that could work out of the box!
Suddenly I remembered a web hook debug thingy I stumpled upon a couple of months earlier:
- Go to www.postbin.org and make a PostBin
- Your created PostBin’s URL is shown
- Start making HTTP POST requests to that URL
- See the result at your PostBin’s URL or access the Atom feed for it (append /feed to the URL)
Excellent!
So what is the most LOC effective and least ugly integration test I can make out of this which tests my HTTP POST code?
Here is what I ended up with:
[TestFixture]
public class HttpPostTargetPostBinTests
{
[Test]
public void GivenAPayloadAndAPostBinBucket_WhenCallingTheTarget_ThenPostBinReturnsThePostedPayload()
{
const string postBinBucket = "http://www.postbin.org/93483c01";
var target = new HttpPostTarget( postBinBucket );
var commitId = new Random().Next().ToString();
Payload samplePayload = SamplePayload( commitId );
var status = target.Call( samplePayload );
var content = GetFirstEntryContentOfAtomFeed( postBinBucket + "/feed" );
Assert.AreEqual( HttpStatusCode.OK, status );
StringAssert.Contains( """ + commitId + """, content );
}
static string GetFirstEntryContentOfAtomFeed( string atomUrl )
{
XNamespace atom = "http://www.w3.org/2005/Atom";
return XDocument.Load( atomUrl ).Descendants( atom + "content" ).First().Value;
}
private Payload SamplePayload( string commitId )
{
return new Payload { Commits = new[] { new Commit { Id = commitId, Author = new Author { Email = "foo@bar.tm", Name = "Foo Bar" } } } };
}
}
Someone else can probably make it prettier and more effective but this just works out of the box, without adding any external library references except for NUnit.
Conclusion:
It is very easy to get into trouble when writing tests for problems which are closely tied up with or depending on some technical concepts. In my case I was trying to get away as far as possible from a data format (JSON) and a network protocol (HTTP). A good start seems to be to make the abstractions where you hit these techie stuff. Think of that before you find yourself extending the problem context; Do you really need that infrastructure or external library to deal with your current design issues? Stay in context. When time comes and you need to hit the outer limit of the context, proceed as always: Make the simplest possible thing that could work.
Note:
PostBin is a web site made by Jeff Lindsay. You can fork his work on GitHub.
WordWrap function in c#
Today I worked on a piece of code at RemoteX and needed a pretty simple word wrapping function in C# which:
1) breaks between words, if possible
2) breaks words, if they’re too long to fit on one line
I used the almighty code snippet resource but couldn’t find a quick fix for my need without digging further into the different solutions, so here is my contribution to the long list of word-wrapping functions posted around the web.
Just like I tried to use several of the code snippets I found on the web, you are free to use mine.
I take no responsibility for any bugs you may find in this code snippet may include. Please let the tests guide you!
https://github.com/anderssonjohan/snippets/blob/master/wordwrap/WordWrapTests.cs
Swedish Alt.Net UG Coding Dojo at Avega, Stockholm
Yesterday I was at my very first Coding Dojo with my fellow collegues Morten and Sebastian. I was somewhat nervous before this event. Coding in group is not an every day occasion for me
. But it went very well and I can only concur on what Morten blogged about.
Switching component implementations in Castle Windsor
Problem: Test suite for a system for which I need to replace (inject) the behavior of a certain component. The system is using Castle Windsor with an XML configuration file.
The replacement for the real component is as simple as:
public bool IUsernamePasswordVerifier.VerifyPasswordFor( string username, string password ){ return true;}
ComponentRewriterFacility.cs:
using System;using System.Collections.Generic;using Castle.Core;using Castle.Core.Configuration;using Castle.MicroKernel;
namespace ServiceTests.Service{ public class ComponentRewriteFacility : IFacility { readonly IDictionary<Type, Type> _rewrites;
public ComponentRewriteFacility() { _rewrites = new Dictionary<Type, Type>(); }
public void AddRewrite<I, T>() where T : I { _rewrites.Add( typeof(I), typeof(T) ); }
public virtual void Init( IKernel kernel, IConfiguration facilityConfig ) { if( kernel == null ) throw new ArgumentNullException( "kernel" ); kernel.ComponentRegistered += ComponentRegistered; }
public void Terminate() { }
void ComponentRegistered( string key, IHandler handler ) { if( !ShouldRewrite( handler.ComponentModel ) ) return; handler.ComponentModel.Implementation = _rewrites[handler.ComponentModel.Service]; }
protected virtual bool ShouldRewrite( ComponentModel componentModel ) { return _rewrites.ContainsKey( componentModel.Service ); } }}
And in our test suite initializer we add the “re-write rule” like this:
IWindsorContainer container = new WindsorContainer();container.AddFacility<ComponentRewriteFacility>( f => f.AddRewrite<IUsernamePasswordVerifier, AnythingGoesUsernamePasswordVerifier>() );container.AddFacility<WcfFacility>();container.Install( Castle.Windsor.Installer.Configuration.FromXmlFile( "Windsor.xml" ) );
Food, Code, Time and Quality
Being an enthusiastic software developer is not without its battles. As an metaphor I would like to illustrate this particular (kind of) battle I think of, with some of my own experience as a parent and a bit of a cooking “enthusiast”.
Ok, so you come home late with the kids and you have just had a great time in the park playing around all afternoon. The kids are happy and you feel you have a nice quality moment in life. You also feel quite satisfied with your big dinner plans. Tic-tac-tic-tac. Time flies and you hurry to get home in time for..
Next stop: Dinner.
You: Really enthusiastic about good cooking, nutricious food etc.
Time: Just too late. There is no time to prepare a nice meal of food from the ground up with hungry kids running around, soon screaming and beaten up by each other..
Solution: What was the phone number to that near by pizza/burger/salad/indian/china restaurant again?
If you think of situations like this but with defect software in production, you might have taken one of the following paths what regards the “solution” part:
“- Hey, let’s solve this by increasing the disk space for now so that Windows Error Reporting doesn’t fill up the system volume with error logs”
“- Request timeout? Let’s add another web node so we can continue with our big deployment. Let’s investigate the timeout issue later.”
Depending on your current situation you may right now feel “yeah, what’s wrong with all that?” or “Stop procrastinate! I would ever never do such things – fix the real problem instead”.
Of course you want to start from the ground up and build quality in, but when the shit already have hit the fan, what would YOU do?
I’m not saying that eating fast food or cutting corners in systems development are good things to do all the time. However, there are differences of having a failing/defect system in production and when you are in “experimentation mode” (a.k.a building new features).
For both cases of experimenting/developing with cooking and doing software development, you don’t know for how long you need to experiment to get it “feature completed”. What you DO know is that you for sure want to “get it right”, but again, not necessarily “feature complete”. Therefore you have to do timeboxing to deliver on time without any loss in quality.
To avoid being in the park with the kids too long in the afternoon I can easily add some more automated tests. I can set up my mobile phone to buzz a sound. I could also check the azimuth of the sun, look at the clock tower, use a wrist watch and what not to get a signal when it is time to get started with the dinner.
But even though these tests are valuable and can reveal defects, they are no good if I just ignore to use them.
As I said before, it’s probably not a problem if this happens one or two times. But if you continue doing this you will have a quite dissatified customer (the family).
Or would you? Really?
It certainly depends on how you define what quality time with your family is about.
(yes, believe me when I say my own and my common-law spouse’s definition of “quality” DO differ when the shit already hit the fan in terms of cooking dinner in the weekdays
)
The same goes for computer systems running in production. You and your team may have a higher tolerance (for what some people would call) defects in production.
Maybe you aren’t a “timebox” person and maybe you define quality in other means than the number of automated tests / automated test coverage percentage.
When development tools mature, people changes their minds and/or gets replaced, the definition of describing “good quality” of a particular system may also change alot during the lifetime of the system.
The challange is not just about more automated tests. It’s about developers and managers (a.k.a. people), LOCs, test coverage, tools, time and process.
Further reading:
* The engineering manager’s lament
* Choose Two – The Project Triangle
* We Need More Automated Tests
How to inject XSL stylesheet reference in WCF service response using a Message encoder
In order to shape up the presentation of the responses from a RESTful WCF web service to clients such as Firefox, you’ll need to add an XSL stylesheet reference.
In order to do this, the most straight forward thing to do is to hack into your XML serializable classes and implement this using IXmlSerializable.
Since we want to control this in our service, based on the endpoint URI, this approach was not suitable for our needs.
Also, we don’t like cluttering down our serializable entity classes with “presentation logic”/encoding stuff.
I posted a question about this in MSDN Forums and got a pretty nice answer from Marco Zhou. He has written a message encoder which injects the XSL stylesheet reference on a lower level which is exactly what we wanted to do.
Thanks Marco!
Castle Windsor 2.0 RTMed
Oh yeah! Castle Windsor 2.0 is RTMed!
I just can’t wait ’til we upgrade RemoteX Applications.
A simple bandwidth test based on HTTP, AJAX and jQuery
Recently I had the opportunity to work with my friend Micael on a HTTP broadband testing application. This test will be used as a zero-touch diagnose tool to identify network related issues between users and a video conference system server.
The test was produced for the non-profit organization Infinite Family which involves users located both in the USA and South Africa. The video conference system is located in South Africa and the internet connection to South Africa is provided by a satellite link. Only the hop over the satellite link adds a few hundred milliseconds in response time (ICMP Ping), so it can be really poor conditions for a video link!
The test has equal capabilities as many of the other tests out there. What it does is that it tries to calculate a response time, which of course is not ICMP Ping as one might think, the test only does HTTP and AJAX. Instead, the test uses a HTTP HEAD request to get somewhat a measurement of how long it takes to establish a TCP session and send some data over port 80.
The test does not only measure the response time, it also measure upload and download speeds. For these kind of tests a lot more data is sent back and forth to the HTTP server using (AJAX) HTTP requests.
The whole test consists of one HTML page with references to jQuery and jQuery UI.
This test was the first project in which I could try out jQuery for real. I had some hard time get it working properly on my PCs and the kids’ Mac, but now it works in IE, Opera, Firefox and Safari on both Mac OS and Windows.
I must say I really like working with jQuery. To me it’s somewhat aligned with the way I use LINQ and extension methods when coding in C#, so it was a piece of cake start coding with this neat Javascript library.
The progress bar used in the test is a pretty simple thing to do with a few DIV elements and a bit of CSS. That was the approach I used in an early version, but I decided to try out jQuery UI as soon as I jumped on the jQuery track.
You can find the test right here.
How To Make the Firefox/YouTube Wait Symbol In WPF
Recently I thought about doing one of those trendy animated “hour glasses”, the “wait” symbol which appears when that computer of yours is waiting or working on stuff in the background.
So what do I mean by “doing one”.. In HTML? with jQuery UI perhaps? No and no.
About 3-4 months ago we started to get WPF into production in our product at work and now we’re doing all our new features based on WPF and XAML.
So this animated “hour glass” (or “spinner” which is the word I will use from here) must of course be created in WPF.
I believe that you guys and girls out there who have worked with XAML, since the spec came out in 2006, probably just say “Say hi to XAML, my friend”. For me however, I’m quite proud of how easy it was for me to create this animated little thing.
When I brought this up during lunch a couple of days ago I discussed what the least CPU consumtive design would be.
My first design thought I came up with was based on 8 circles with ColorAnimations which would make the circles look like the were rotating.
After a quick discussion with my fellow collegues Morten and Wilhelm I got interested about the idea of using a rotating (using RenderTransform) Canvas instead. This idea worked out very well and here is the result (you must have .Net installed to view it in your browser).
So the XAML required to achieve this is quite simple, here is a summary:
- A Canvas is used to distribute the 8 circles on
- Each circle has its fixed position and its own transparency (which makes the “tail” effect)
- We add a trigger to the FrameworkElement.Loaded event for the Canvas, in which we…
- …add a DoubleAnimation targeting the Angle property of the RotateTransform we also added to the Canvas
- We use a ViewBox as the parent of the Canvas which lets us scale the animation to fit in i.e. a TabPage header control (like the tabs in Firefox)
And here is the XAML:
<Viewbox> <Canvas Width="80" Height="80" Name="canvas"> <Canvas.RenderTransform> <RotateTransform Angle="0" CenterX="40" CenterY="40" /> </Canvas.RenderTransform> <Canvas.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="canvas" Storyboard.TargetProperty="(Canvas.RenderTransform).(RotateTransform.Angle)" To="360" Duration="0:0:0.7" RepeatBehavior="Forever"/> </Storyboard>
</BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Canvas.Triggers> <Ellipse Canvas.Top="0" Canvas.Left="30" Width="20" Height="20" Fill="#08000000"/> <Ellipse Canvas.Top="10" Canvas.Left="50" Width="20" Height="20" Fill="#15000000"/>
<Ellipse Canvas.Top="30" Canvas.Left="60" Width="20" Height="20" Fill="#38000000"/> <Ellipse Canvas.Top="50" Canvas.Left="50" Width="20" Height="20" Fill="#55000000"/> <Ellipse Canvas.Top="60" Canvas.Left="30" Width="20" Height="20" Fill="#88000000"/> <Ellipse Canvas.Top="50" Canvas.Left="10" Width="20" Height="20" Fill="#aa000000"/> <Ellipse Canvas.Top="30" Canvas.Left="0" Width="20" Height="20" Fill="#cc000000"/> <Ellipse Canvas.Top="10" Canvas.Left="10" Width="20" Height="20" Fill="#ff000000"/>
</Canvas> </Viewbox>
Pretty easy, or what do you think?
A fluent interface and the Builder pattern
My former collegue Thomas Lundström blogged about fluent interfaces quite a while ago, which he followed up in a comment to Johan Lindfors’ post “A small DSL to order coffee” (in Swedish).
The blog post and its comments discusses two different approaches to fluent interfaces. The first approach is based on getters which mutates the instance when they are called. Some people think this is a very bad design – getters should not mutate the instance!
I agree on that. I strongly believe that a get operation should be idempotent and not mutate the object instance. This for the same reason a HTTP GET request should not mutate a resource and a HTTP POST/PUT should mutate a collection/the resource.
(That’s why I prefer RESTful web services more than SOAP over HTTP, but that’s a totally different blog post.
The second approach is the one that Thomas points out. It’s the classical Builder Pattern where you have a totally separate class which is used to build object instances. It differentiates itself among the creational OOD design patterns by targeting primarily mutable objects. It’s like a bunch of Factory methods which is used like a Factory, but instead of having thousands of different methods in one Factory (overloads for example), the Builder is used in several calls to create/configure the final object instance.
An approach which was recently used at work is a mix of the two approaches mentioned above. Instead of having a separate builder we have builder methods on the object instance itself, either directly or via extension methods. Separate from the builder methods we got some Factory Methods. These are the starting point to create a basic instance. The builder methods are then used to further shape the object instance to our needs.
Approach #1 – getters which creates/mutate the object instace:
Coffee myLatte = Coffee.OrderLatte.Double.FatFree.Vanilla;
Approach #2 – a separate builder
Coffee myLatte = CoffeeBuilder.Latte().Double().FatFree().Vanilla().Build();
Our approach – a mix of factory methods and builder methods on the object instance itself
Coffee myLatte = Coffee.Latte().Double().FatFree().Vanilla();
So what’s the pros and cons with the last two approaches?
I guess they are quite similar, but here is some pros:
- The separate builder seems a bit more aligned with the Separation Of Concerns principle
- The built-in builder methods via extension methods provides a lightweight class structure, even for classes in a third-party class library, such as the BCL.
(Please post a comment if you can think of more pros and cons or if you just totally disagree with me
So of course, the $10.000 question answer is… It depends on your context!
Anyhow, here is a sample how we tend to use this mix of Factory Methods and Builder methods at work.
The following code is used to build construct the collection of DataGridViewColumns used in a Windows Forms DataGridView control.
The CreateColumns() method is used to create an enumeration of the columns to use and TextColumn() is a sample Factory Method.
protected override IEnumerable CreateColumns(){ yield return TextColumn() .For( WorkOrderPresenter.Properties.Title ) .WeightedWidth( 100 );
yield return TextColumn() .For( WorkOrderPresenter.Properties.Description ) .WeightedWidth( 200 );
yield return ComboBoxColumn( _activityBindingSource ) .For( WorkOrderPresenter.Properties.Activity ) .WeightedWidth( 100 ) .Width( 90, 130 );....}
///
/// Creates a textbox column with default behavior.///
protected virtual DataGridViewTextBoxColumn TextColumn(){ return new DataGridViewTextBoxColumn().WrapContents().WithDefaultBehavior();}
The methods For(), WeightedWidth(), Width(), WrapContents() and WithDefaultBehavior() are Builder (extension) methods on DataGridViewColumn.