But it works on my PC!

The random thoughts of Richard Fennell on technology and software development

Further thoughts on mocking out a SMTP Server

I posted on the problems I had had mocking out an SMTP server, well I have moved on a bit. As I said in the update note in the last post, I had given up on nDumbster and moved over to LumiSoft's freeware mail server.

The Lumisoft server is a far better starting point as it is a 'real' server that supports all the major mail protocols. As all the source (and binaries if you can't be bothered to build it yourself) are shipped it is easy to create a wrapper class for unit testing purposes that can do whatever you need.

However even with this much improved server I still had a problem with System.Net.Mail calls. I had four tests

  • Send a single email with System.Web.Mail
  • Send a single email with System.Net.Mail
  • Send a batch of three emails with System.Web.Mail
  • Send a batch of three emails with System.Net.Mail

Before each test a new SMTP server was created, and when the test completed it was disposed of. Each test worked if run by itself.

The problem was that if the tests were run as a batch the final test failed. When the SMTP server was checked it was found to have no messages recorded, not the expected three. However, the logging said three messages had been sent OK. Swapping the order if the tests did not effect the basic issue that the second System.Net.Mail test reported no emails received, whilst the System.Web.Mail tests were fine.

By adding a unique identifier to each created SMTP server it could be seen that the fourth test was sending its mail to the second SMTP server (which should have been disposed of well before the fourth test was started) hence the final test failing.

The problem appears to be that the threading model inside System.Net.Mail holds the TcpClient object in memory for longer than you would expect, so somehow allows the fourth test to reuse the connection (and server) from the second test. Though it is unclear how you are able to have two servers both on port 25 at the same time. I guess this theory could also go some way to explaining the issues I had with the nDumbster implementation.

Though not perfect, the solution I used was to make the Smtp Server instance static for the test class so for all the tests I created just one instance of the server. Before each test I cleared down the received messages store. Thus far this is working reliably for both single tests and batches of tests.

Update 6 Oct 2008 - The use of a static Smtp server per class is not a full solution, but just moves the problem on i.e. you get the same problem is you have two classe in the batch each with their own server. I need to find a better way to rip the Smtp server of memory - Maybe an AppDomain?

Update 10 Oct 2008 - Well AppDomains don't help. The best solution I have found is to not use a static Smtp server but to instaniate a non static member one and then start it in the test systems TestInit method. In the test systems TestCleardown I stop the server and dispose of it. However this does not fix the problem. I therefore then send an email to the server I have just disposed using System.Net,Mail. This obviously fails but does cleardown the internal cached items inside the System.Net.Mail structure. I just catch the expected exception and carry on. I know it is not elegent but it works both within a test class and an assembly containing many classes 

'Datadude' merged with Team Developer

It was announced overnight by Microsoft that the Database Professional SKU for Visual Studio will be made available to all people who have a licensed copy of Team Developer.

This is great news as it addresses the problem of where to put the expensive copy of DataDude (which I think has been a barrier to it's uptake), in most companies there is not the clear distinction between code and DB devs.

So as of tomorrow you can get DataDude via MSDN. You can read more about the recent announcements here: http://msdn.microsoft.com/en-us/vstudio/products/cc948977.aspx

Mocking out an email server

I am currently looking at automation of acceptance testing for workflows and a common task is to make sure an email has been sent. To do this in a TDD style I have been using the mock SMTP server nDumbster.

Now this was a port from Java in the the days of .NET 1.1 and does not seem to have had any development since. This can be seen because the following test using the .Net 1.1 System.Web.Mail call works beautifully, returning in a second or so

[TestMethod]
public void CanRecieveSingleWebMail()
{
System.Web.Mail.SmtpMail.SmtpServer = "localhost";
System.Web.Mail.SmtpMail.Send("somebody@foo.com", "everybody@bar.com", "This is the subject", "This is the body.");
Assert.AreEqual(1, smtpServer.ReceivedEmail.Length);
}

However the test using the .Net 2.0 System.Net.Mail is not so good

[TestMethod]
public void CanRecieveSingleNetMail()
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("localhost");
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage("somebody@foo.com", "everybody@bar.com", "This is the subject", "This is the body.");
client.Send(msg);
Assert.AreEqual(1, smtpServer.ReceivedEmail.Length);
}

It does work but takes 100 seconds to return, a bit of a pain for TDD!

After a bit of debugging I found the problem was the final input.ReadLine(); in HandleSmtpTranslation which we expect to get a null at the end of a message, for Systsem.Web.Mail this returns instantly, but for System.Net.Mail this takes 100 second, but then is fine.

As a work round I have put in the logic

string line = null;
if (smtpState != SmtpState.QUIT)
{
    line = input.ReadLine();
}
if (line == null)
{
    break;
}

This works fine for single mail messages, using both the old and new API.

There is still a problem, with the System.Web.Mail API you can send multiple emails in a test, but this still fails for System.Net.Mail – the first goes OK, but the second fails saying the server is not present.

By the way I have ruled anti virus and anything specific to port 25 causing the problem. I suspect a threading issue as I believe System.Web.Mail was single threaded wrapper for COM and System.Net.Mail is multi threaded pure .Net logic.

If I get a better solution I will post about it, but I have enough functionality for me at present.

Update 28th Sep – Got an email today via the SourceForge forum that pointed me at the freeware LumiSoft mail server. On a first look this certainly appears to do the job. You just need to wire it into your tests in a wrapper class of your own.

First thoughts on the Dell Mini 9

After Alt.net I had thought getting an ultra light notebook was a good idea. At most conference I need to browser, blog and read email; I don’t need to carry around a full development desktop replacement laptop.

Whilst at ReMix I had enough of the old battery in my Acer laptop, so just before it ran out of juice again I ordered a Dell Mini 9, which was actually cheaper than my HTC phone! Today it arrived, a lot sooner than I was expecting, Dell has said it would be next month.

Inspiron Mini 9

My first thoughts? Well the keyboard is small, typing is an interesting experience as I type this post. Also I would like a disk activity light; during the XP setup there a good period when I was not sure what was going on, a flickering led is always reassuring.

Other than these minor gripes it seem very good, it is light, the battery life seem as advertised and it is fast enough for Office 2007 (so I can use it for presentations) and messing around development (I popped on C#  Express but there is disk space left for VS2008 if I needed it).

I will report further when I have lived with it a while.

Post ReMix thoughts

So how was ReMix? well a bit like last year not earth shattering, but what can you expect. We have PDC in a month so there is going to be no major announcements, also this is a rerun event from the US MIX conference, again reducing the chances of anything new.

So if you are a developer and been to TechEd or just to local community events (and as Andy Westgarth said we are very lucky at the quality and number of community events and speakers in the UK) you will have heard nothing new about Silverlight etal. Though it is far to say that this may not be true for designers, and I felt the development track was pitched at this level.

It is with design where I think ReMix was different this year; there were noticeably more designers present and I thought the design track a lot stronger. As I had seen most of the developer track elsewhere I spent a good while in designer sessions, and as usual when going beyond your normal bounds it makes you think.

What has been bubbling in my mind is how does the design process fit into agile processes? Bill Buxton in his sessions (and new book) spoke about how it was important for the designer to provide a variety of concepts for the client/project and not invest all their effort in a single design, thus removing choice form the 'client' . It got me think should the same apply to the developers/architect? Is this even possible? At the grossest sense it would mean developers would propose (prototype?) versions of a product in PHP, Java, Flash ASP.NET, Silverlight etc. Now we all know this does not happen, early in a project a hopefully informed choice will made as to the technology to use (often this choice is made in the choice of who gets the business - a PHP house or a Microsoft house), and it is rare a change will be made in base technology once the choice is made. Even though a project is agile there are limits as to how major a change of direction could be,

So is this a difference between design and development, in web design we can ask the client how it should look, you still need to provide quality design with sane user interactions, but the client can have some choice e.g. 'I like  a more cartoon style', 'I want just like that but in red'. You are not going to get the same interaction from the bulk of clients for development and architecture, it is rare you hear 'Just like that but can you use SOAP'. Development constrains tend to be just that, things that must be done to meet a standard, not something open to choice at the non specialist level.

So ReMix was an interesting event, and good to see many friend from the community. And if it opened my mind to something new it must have been worth attending.

Local user groups

I have been asked a couple of times whist at the Alt.net and ReMix conferences about the local user groups around Leeds. Well these are the regular free community events I know about:

Black Marble community events - my company runs free community events at a hotel between Leeds and Bradford roughly monthly from autumn to spring. We invite a variety of leading .NET community speakers.

The Yorkshire Extreme Programming and Agile Methods Club (XPClub) - meets the 2nd Wednesday of the month in the centre of Leeds. This is a group of .NET and Java developers interested in agile development bast practice.

BCS West Yorkshire Branch - the local branch of the BCS meet either in the Leeds or York providing a wide range of IT related sessions (they also have links to the events run by the local Institution of Engineering and Technology branches)

Leeds branch of SQL Server User Group - the local branch of the national SQL User groups. As the name suggests is targeted at SQL users.

Geek Up - run monthly events cross the north of England targeted at web designers and web developers (I have not been to this group, but members of the XPClub have and say they enjoyed it)

Vbug - run some events in Leeds, but I think these are organised by the Manchester branch

Now there my be others that I don't know about, if you know of one please let me know.

Blend and Source control

It is all well and good Microsoft saying that a developers and designer can share the same project WPF/Silverlight files in Visual Studio and Expression Blend, but whilst Blend does not have the ability to use a source control repository (TFS, SVN or anything else for that matter) and actually strips out  any source control binding it finds in a project file, this is for me unworkable experience. How has this product got to V2 without this feature?

For any company interested in a quality development process source control must be the most basic safety net, irrespective of how agile their methodology is.

I suppose there is one agile model where it could almost work and that is the designer and developer pair programing on the same PC with Blend and VS installed - but how realistic is that? and certainly does not scale beyond two people.

So as I am at Remix, I just asked Scott Guthrie when we would see source control in Blend - he said next version, some time next year, but on the plus side it should also include TFS work item integration. All good but we need it now, this is such a barrier to adoption of the Blend products.