Unit Testing Race Situation

In the current project I’m working on, we are intensively writing unit tests with 100% code coverage following TDD.

In this post I’m going to share one of very exciting situations that I faced couple of days ago while working on a module which is supposed to initialise database on fist access.

While the first caller is waiting for the database to be initialised, the second caller needs to wait as well and do not trigger the initialise method again. After the wait is over the second caller needs to know that the database is initialised and continue the normal work.

The fact of putting the initialisation in the database client is not the centre of this post.

There are couple of ways of tackling this but IMO the best way is to actually make a race situation happen in your unit test. Some developers do this by firing an action and delaying that by putting the first thread to sleep and fire the second thread while the first thread is asleep. The biggest problem is that how long do you want to put the first thread to sleep. On different machine the process of running the unit tests in the test runner takes different time to run. So you should either introduce a very long delay (seconds) or a short one that runs on your machine but may fail on the build server.

Here is how I solved this issue:

 

Continue reading Unit Testing Race Situation

Trigger Visual Studio Team Services (VSTS) CI Build from BitBucket

We use BitBucket for our source control and VSTS for CI. VSTS build definition has a trigger option which will monitor the repository every so many seconds (min is 60s) and if new changes are there it will trigger the build. But that feature only works when you have VSTS repositories  and for any external repository the developer needs to login to VSTS after the changes are pushed and wait for at least 60 seconds for the build to kick off. I’m pretty sure that manually triggering the build takes less time. Also, that model by itself is not efficient specially in the world of Webhooks.

BitBucket supports Webhooks. For example you can call add Webhooks for each push (unfortunately you can not specify a branch). The problem is that VSTS CI build definition does not accept Webhooks as trigger.

In this post I’m going to show how this can be done by using BitBucket Webhooks , Azure Function Apps and VSTS REST API.

Continue reading Trigger Visual Studio Team Services (VSTS) CI Build from BitBucket

Creating LINQ Extension

Creating extensions  is one of the very useful techniques in C# that can help us extend the functionality of an existing class or library without modifying the origin. It is also aligned with the 2nd principle of SOLID programming, Open Close Principle.

In this article I will focus on creating a LINQ extension method and will point out a simple but important tip that you need to watch out for.

Continue reading Creating LINQ Extension