Jake Ginnivan's blog

nUnit 2.6.3, Async and .net 4.0 Broken

I am currently working on a .net 4.0 project which uses async/await quite heavily. We also are using the old AsyncTargeting pack rather than the RTM because we do not have stats on how many clients currently have the .net 4.0 KB 2468871 patch which enables PCL support. These issues affect the RTM version as well.

Recently I have upgraded to nCrunch 2.2 beta and ReSharper 8.1 both which ship with the upgraded nUnit 2.6.3 runner. After I upgraded these tools I noticed tests which should have been failing were passing and passing tests were being reported as failing but showing stack traces from a different test..

Also I was getting different results in nCrunch, R# and nUnits console runner. Something was broken.

NOTE: Async and TPL support is not supported in nUnit 2.x, but will be officially supported in v3.x and that it was a coincidence that it worked in 2.6.2. My discussions about the issues are at here on the nunit discussion board.
As far as I can tell, there is no reason that 4.0 cannot be supported because to offer framework support does not need any new features OR the classes in the Async Targeting Pack or .NET 4.5. At a minimum tests returning Task should be supported as TPL was introduced into the CLR for net40.
Recently I added async void and Task support to BDDfy which targets .NET 4.0, also xUnit 1.9.x supports Tasks in the current released version and has backported async void support to the 1.9.x codebase from the 2.0 and will be released if there is a need to release another patch release before 2.0 is released.

.NET 4.5 is fully supported and works as expected with nUnit, it is only .net 4.0 which is affected

On Async and Sync Contexts

This week has been a really heavy learning experience for me in terms of async/await, .ConfigureAwait() and how it interacts with Synchronisation Contexts. Quite a few of my assumptions were wrong, or the behaviour is different between the .NET 4.0 implementation (CTP3 and RTM) and what happens in .NET 4.5.

In this post I will cover:

  • What .ConfigureAwait(false) does in .net 4.0 and in .net 4.5, and why they act differently
  • Some guidance around using await in a rich client app (i.e await for offloading, not scalability) .net 4.0 in particular, .net 4.5 behaves better and the guidance is not as relevant
  • How you can use custom awaiters to switch contexts

Blog Migration

I have finally decided to move from FunnelWeb to Jekyll. My needs for a blog engine are changing, and at the moment I am liking the static site generators.

Those of you subscribed to my RSS feeds will likely get duplications, sorry about that.

LinqPad Migration script

Migrates posts, and images (putting them in the correct spot for Jekyll sites and rewriting links in your blog posts as it goes)

Available at http://share.linqpad.net/76oqrd.linq

Other Links

https://github.com/tokkonopapa/octopress-tagcloud

GitReleaseNotes Initial Release!

I have just used GitReleaseNotes to publish a release of GitReleaseNotes on GitHub https://github.com/JakeGinnivan/GitReleaseNotes/releases/tag/0.2.0!

I am excited about this project because it will save me a heap of time and effort with managing the open source projects I do releases for.

GitReleaseNotes was another project I kicked off about the same time that Simon Cropp and Andreas Öhlund were kicking off similar projects, see https://github.com/Particular/ReleaseNotesCompiler. We decided to start the projects off down different roads to start with, then maybe merge later once we both could experiment with ideas.

Git Flow Versioning

As a follow up to my last post http://jake.ginnivan.net/release-nuget-semver-packages-from-teamcity I have been investigating more into different ways to achieve semantic versioning and being able to release in an easy way.

Next stop on my trip was looking into Git-Flow and how it manages releases, initially it seemed very waterfally and too heavy to use on an open source project, but I thought it may fit at different clients. I was instantly drawn to the fact that releases have an explicit step to version at the start of the release process, this is great, I can manage my project, merge pull requests, add features then when I am ready, I can decide to release, put together release notes and figure out if this is a major, minor or patch release.

Much to my surpise, it is actually very light-weight and you can drop much of it if your process is simpler (say for open source).

https://github.com/TestStack/ConventionTests is my guinea pig!

To get started, I decided to try and implement git-flow manually to really understand what is going on. There are plenty of explainations out there, so I will just be running through what I did, and how it works from my point of view. I always have two remotes setup for my projects, ‘upstream’ is the main repository, ‘origin’ is my fork.

Release NuGet SemVer Packages From Teamcity

I have a number of open source projects and I do not really have a good release process. So I spend the arvo trying to figure out a good way to do it.

My goals were

  • Use GitHubs releases feature - https://github.com/blog/1547-release-your-software
  • I want to release from NuGet
  • Preferably write release notes before I click the button in TeamCity, this way i can add them on github to build up a release
  • Support SemVer, including pre-release packages
  • Assembly versions should be stamped with informational version as well as a version
  • Be able to link to the project GitHub releases from the NuSpec

Convention Tests V2 Released

ConventionTests v2 Released

Krzysztof Koźmic first spoke about ConventionTests at NDC 2012. You can find the video of that talk here, slides here and the introductory blog post here.

In v2, we have rewritten convention tests from the ground up to make it easier to get started, bundle some default conventions and also decouple it from a specific unit testing framework.

There is still plenty we can make better, so please raise issues on github with suggestions!

What is ConventionTests?

Convention over Configuration is a great way to cut down repetitive boilerplate code. But how do you validate that your code adheres to your conventions? Convention Tests is a code-only NuGet that provides a simple API to build validation rules for convention validation tests.

Pack URI in Unit Tests

I had a unit tests which constructed a pack uri, and I didn’t want to abstract it (needless abstraction) so here is how I solved a few issues.

Exception 1

System.UriFormatException : Invalid URI: Invalid port specified

This one is pretty easy to fix, you can use the PackUriHelper which registers a few things in it’s static ctor

PackUriHelper.Create(new Uri("reliable://0"))

Exception 2

System.NotSupportedException : The URI prefix is not recognized

This one is fixed by giving WPF the default resource assembly.

System.Windows.Application.ResourceAssembly = typeof(App).Assembly;

Now you should be able to unit tests around pack uri’s

Verifying Logged Messages With Log4Net

I came across a constructor which looked something like this, just to enable a test to inject a mocked ILog and validate the calls.

ClassCtor(...., Func<ILog> logFactory) { .. }

The log factory would grab inject a log for the class, but everywhere else in the app used

ILog log = LogManager.GetLogger(typeof(CLASS));

There must be a better way, something like a scoped appender or something, so I came up with this syntax

ClickOnce Signing Error

On my current project we use ClickOnce, and I am setting up the build server to sign using a proper cert rather than a self generated one.

The command:

mage.exe -New Application -ToFile <path>\App.exe.manifest -name "<Name>" -Version 0.1.1.1 -FromDirectory <path>\0.1.1.1\ -IconFile App.ico -CertHash "‏ca5da5a1f7c57411111111a79cbf50c4432ed949"

And was getting This certificate cannot be used for signing - "ca5da5a1f7c57411111111a79cbf50c4432ed949"

I was searching for what extended attributes are required, checking if I got the right thumbprint and wasted a bunch of time.

The fix was simple, remove the quotes around the thumbprint