Unit Test parallel execution of static classes or ServiceLocator

Please note that I am NOT promoting an anti-pattern or code smell here. I know using the service locator or static classes that hold the state or cause the side-effect are not in everyone’s favor but let’s face it. Life is not perfect. What if you happen to be in a decade old legacy project that heavily depends on static classes or service locator pattern? But it shouldn’t stop you from adding the unit tests if you want.

Source Code for Test Project: https://github.com/michaelsync/Michael-Sync-s-blog-sample/tree/master/XunitTestsParallel

Mocking Static Classes

The problem with the static class is that you can’t mock them unless your mocking library supports it. Here is the list of mock libraries (both free and commercial) that supports the static class mocking in .NET world.

Let’s say we are using Microsoft Fakes. You can mock the DateTime.Now as below.

// Shims can be used only in a ShimsContext:
            using (ShimsContext.Create())
            {
              // Arrange:
                // Shim DateTime.Now to return a fixed date:
                System.Fakes.ShimDateTime.NowGet = 
                () =>
                { return new DateTime(fixedYear, 1, 1); };

                // Instantiate the component under test:
                var componentUnderTest = new MyComponent();

              // Act:
                int year = componentUnderTest.GetTheCurrentYear();

              // Assert: 
                // This will always be true if the component is working:
                Assert.AreEqual(fixedYear, year);
            }

It’s pretty easy, huh? What about the service locator?

Mocking Service Locator

Mocking the service locator is very easy as well since it allows us to set the mock locator using

ServiceLocator.SetLocatorProvider()

method.

The example below shows how to mock the service locator using Moq in xunit test.


public class TestClass1 {

        public TestClass1() {
            var mock = new Mock<IDataStore>();
            mock.Setup(m => m.GetData())
                .Returns(1); /* Returns 1 from TestClass1 */

            var mockServiceLocator = new Mock<IServiceLocator>();
            mockServiceLocator.Setup(ms => ms.GetInstance<IDataStore>())
                .Returns(mock.Object);

            ServiceLocator.SetLocatorProvider(() => mockServiceLocator.Object);
        }

        [Fact]
        private void TestClass1TestMethod1() {
            var data = ServiceLocator.Current.GetInstance<IDataStore>().GetData();
            Assert.Equal<int>(1, data);
        }

        [Fact]
        private void TestClass1TestMethod2() {
            var data = ServiceLocator.Current.GetInstance<IDataStore>().GetData();
            Assert.Equal<int>(1, data);
        }
}

So far so good. What is the problem?

Test Parallelization

Some test frameworks let you run the tests in parallel. For example, xunit 2.0 which is released early this year (March 16, 2015) has a new feature that let the tests from different classes run in parallel by default. (You can read about it in “Parallelism” in the release note or here “Running Tests in Parallel” ). xUnit 2.0 uses the concepts called “Test Collection” to decide which tests can run against each other in parallel. Each test class is a unique test collection and

ParallelizeTestCollections

property is set to true by default.

So, the tests that has the dependency on server locator or static class will be failed randomly with test parallelization but the test will work fine when you run it individually. Because what you setup in one test will be overridden the setup from different test class so the assertion will be failed when the overrides happens.

I have TestClass1 in my previous example above. I will add another class called TestClass2 to show you te random test failture.

 
public class TestClass2 {
        public TestClass2() {
            var mock = new Mock<IDataStore>();
            mock.Setup(m => m.GetData())
                .Returns(2); /* Returns 2 here */

            var mockServiceLocator = new Mock<IServiceLocator>();
            mockServiceLocator.Setup(ms => ms.GetInstance<IDataStore>())
                .Returns(mock.Object);

            ServiceLocator.SetLocatorProvider(() => mockServiceLocator.Object);
        }

        [Fact]
        private void TestClass2TestMethod1() {
            var data = ServiceLocator.Current.GetInstance<IDataStore>().GetData();
            Assert.Equal<int>(2, data);
        }

        [Fact]
        private void TestClass2TestMethod2() {
            var data = ServiceLocator.Current.GetInstance<IDataStore>().GetData();
            Assert.Equal<int>(2, data);
        }
}

Try running all tests a few times after adding the TestClass2. You will see that at least one test out of four tests will be failed randomly. It is because the locator is a static class and what we setup (let’s say we return 1.) in TestClass1 will be overridden by the setup from TestClass2 so the assertion will be failed.

Random Test Failed

How do we fix that?

Initially, I looked at Microsoft Fakes which is shipped with Visual Studio and is also the next generation of Moles. According to the concurrency section of this document, it doesn’t have thread affinity so no go for me.

Shim types apply to all threads in the AppDomain and don’t have thread affinity. This is an important fact if you plan to use a test runner that support concurrency: tests involving shim types cannot run concurrently. This property is not enfored by the Fakes runtime.

Then, I look at Telerik’s JuckMock. The paid version of JuckMock supports what they called “elevated-mocking” that allows you to mock the static class. (Note: The free version “JuckMock Lite” doesn’t support the elevated-mocking. )

So, I refactored my test code as below.


public class TestClass1 {
    private readonly Mock<IServiceLocator> mockServiceLocator;

    public TestClass1() {
        var mock = new Mock<IDataStore>();
        mock.Setup(m => m.GetData())
            .Returns(1);

        mockServiceLocator = new Mock<IServiceLocator>();
        mockServiceLocator.Setup(ms => ms.GetInstance<IDataStore>())
            .Returns(mock.Object);
    }


    [Fact]
    private void TestClass1TestMethod1() {
        Telerik.JustMock.Mock.Arrange<IServiceLocator>(() => ServiceLocator.Current)
            .Returns(mockServiceLocator.Object);

        var data = ServiceLocator.Current.GetInstance<IDataStore>().GetData();
        Assert.Equal<int>(1, data);
    }

    [Fact]
    private void TestClass1TestMethod2() {
        Telerik.JustMock.Mock.Arrange<IServiceLocator>(() => ServiceLocator.Current)
                        .Returns(mockServiceLocator.Object);

        var data = ServiceLocator.Current.GetInstance<IDataStore>().GetData();
        Assert.Equal<int>(1, data);
    }
}

public class TestClass2 {
    private readonly Mock<IServiceLocator> mockServiceLocator;

    public TestClass2() {
        var mock = new Mock<IDataStore>();
        mock.Setup(m => m.GetData())
            .Returns(2);

        mockServiceLocator = new Mock<IServiceLocator>();
        mockServiceLocator.Setup(ms => ms.GetInstance<IDataStore>())
            .Returns(mock.Object);
    }

    [Fact]
    private void TestClass2TestMethod1() {
        Telerik.JustMock.Mock.Arrange<IServiceLocator>(() => ServiceLocator.Current)
            .Returns(mockServiceLocator.Object);

        var data = ServiceLocator.Current.GetInstance<IDataStore>().GetData();
        Assert.Equal<int>(2, data);
    }

    [Fact]
    private void TestClass2TestMethod2() {
        Telerik.JustMock.Mock.Arrange<IServiceLocator>(() => ServiceLocator.Current)
            .Returns(mockServiceLocator.Object);

        var data = ServiceLocator.Current.GetInstance<IDataStore>().GetData();
        Assert.Equal<int>(2, data);
    }
}

Edited: I can’t move Mock.Arrange() to the constructor because I think JustMock has a thread affinity and xunit might use the different thread to call the constructor and test method. That’s why I repeat the code in every test method. End Edited.

Before you run the test, there is a little small thing that you need to do. You need to enable the profiler in Telerik menu.

Enable Profiler

The reason is that JustMock is using the profiling API to enable the elevated mocking. I heard that TypeMock is using the same thing as well. Please read this post if you want to know more about it.

Now, you can run all tests without any failure.

All Tests passed

You can download the sample test project here “https://github.com/michaelsync/Michael-Sync-s-blog-sample/tree/master/XunitTestsParallel“.

That is. Yes. I am not encouraging you to use this feature. If you have the capacity to remove the service locator or static classes from your project then you should probably do that. Otherwise, using the elevated mocking is one way of solving problem.

Unirest-RT – Simplified, lightweight HTTP library for Windows 8 RT

Mashape_logo

One of my closed friends from Mashape asked me to help in porting their unirest-net library to Windows 8 RT. So, I took a few hours to port their library for Windows 8 RT runtime. You can take a look at my ported version of Unirest in this github respository https://github.com/michaelsync/unirest-rt.

Here is the list of what I changed in order to make .NET library work in Windows 8 RT runtime. I’m sharing it here because I think that it would be helpful for those who like to port some .NET library to Windows RT.

  • Changed “unirest-net” to “Class Library (Windows Store Apps)”
  • Changed “unirest-net-” to “Unit Test Library (Windows Store Apps)”
  • Added “Microsoft.Net.Http” nuget package that was released with Portable Library for RT and Phone.
  • Added “Newtonsoft.Json” nuget package because System.Web.Script.Serialization is not available in Windows RT

The following changes have been made in “unirest-net” project.

  • File Name: unirest-net/unirest-net/src/http/HttpResponse.cs (Note: — means “remove” and ++ means “added”)

    –using System.Web.Script.Serialization; –else if (typeof(Stream).IsAssignableFrom(typeof(T)))
    ++else if (typeof(Stream).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo()))

    –var serializer = new JavaScriptSerializer(); var stringTask = response.Content.ReadAsStringAsync(); Task.WaitAll(stringTask);
    –Body = serializer.Deserialize(stringTask.Result);

    ++Body = JsonConvert.DeserializeObject(stringTask.Result);

  • File Name: unirest-net/unirest-net/src/request/HttpRequest.cs

    –using System.Web.Script.Serialization;
    –var serializer = new JavaScriptSerializer();
    –Body = new MultipartContent { new StringContent(serializer.Serialize(body)) };

    ++Body = new MultipartContent { new StringContent(JsonConvert.SerializeObject(body)) };

The following changes have been maded in “unirest-net-test” project.

  •  The original test project was using nunit unit test framework because Nunit Adapter VS extension (beta 5) doesn’t work with Win RT for some reasons. So, I changed it to VS unit test framework and removed the Nunit nuget package.
  • File Name: unirest-net/unirest-net-tests/src/http/UnirestTests.cs. The orignal code is using the capital “P” (e.g. Unitest.Post(..)..) so I changed to the small letter.

    Unirest.post(“http://localhost”).HttpMethod.Should().Be(HttpMethod.Post);
    Unirest.post(“http://localhost”).URL.OriginalString.Should().Be(“http://localhost”);

That’s all. Hope that you find it useful. If you like to share your experience and chellenge with porting .NET code to Windows RT, please drop a comment or email me via Contacts form. Thanks!

Happy porting!  (Well, I used to write “Happy Silverlighting!” but no more. :| )

Non-app config (e.g. NLog.config) transformation with SlowCheetah in Console App

As we are in the world that everyone is talking about continuous integration, continuous delivery and etc., the transformation of configuration file for different environments become very important for every developments. Microsoft provides us a way to transforming web.config for every web deployment but they seems forgetting about other types of projects like console application and non web.config files.

People like us have tons of projects that are not web projects or contain other types of configuration files. How do we transform those configuration file? Well, we are lucky that there is an awesome tool called SlowCheatah that can help us to transform all of your xml configuration files on build.

Offical SlowCheatah screenshot

Yes. SlowCheatah is very good tool but we always used to have small problems in transforming non-app config files using this tool. We always have to spend a bit of our time to tweet here and there to make it work.

So, I decided to spend some times on writing this post as a check-list for troubleshooting SlowCheatah .

Check-list for troubleshooting SlowCheetah’s XML transformation

1. SlowCheetah Visual Studio Extension

One of the most common mistakes is that you don’t have SlowCheetah extension installed on your Visual Studio. You can verify whether you have it installed or not in “Extensions and Updates” dialog.

SlowCheatah - Extensions

2. Use “Add Transform” menu instead of adding the file manually and modifying the csproj

If you already have that extension installed, you can see “Add Transform” manu in Solution Explorer when you right click on configuration file. Please use that manu for adding the different setting for new xml configuration.

SlowCheatah - Add Transform

3. Verify SlowCheetah in csproj

If two steps above are not helping you then you can manually check the SlowCheetah setting in your csproj. You need to have “TransformOnBuild” and “IsTransformFile” for your new XML configuration file and SlowCheetab import should be included in your csproj.

SlowCheatah Config in proj

SlowCheatah - in csproj

I hope you find this checklist helpful when you are troubleshooting SlowSheetah issue. Please feel free to let me know if you have any comment or question..

Happy transformation!

SlowCheetah + ClickOnce Issue

If you are using SlowCheetah + ClickOnce in your project,

Demo: ConsoleApplication6.rar (4.93 MB)

SlowCheateh+ClickOnce

Error 1 Could not copy the file “bin\Release\ConsoleApplication6.exe.config” because it was not found. ConsoleApplication6

Steps to replicate

  1. Create a console application in VS 2012
  2. Add transformation to your config. (If you already have SlowCheatah tool installed, it will automatically download the SlowCheetah package from nuget. Otherwise, please add it manually)
  3. Publish (Observe: It will work)
  4. After publishing, please try to build or publish again. (Observe: You will see the error above.)

Solution

  • Delete “app.manifest” under “Properties” in Solution Explorer.
  • If you don’t see that file, go to Properties -> Security and check/uncheck “Enable Clickonce security setting”. It will re-generate that manifest file.
  • Note: If you are a ClickOnce user, you might check this project “Shimmer (It’s like ClickOnce but Works™)” as well.

SlowCheateh+ClickOnce - Solution

Date Issue in Excel Export

This is just a tip on solving date issue in exporting the excel file from ASP.NET.

Introduction

One of my guys was using this following code to export the excel file from ASP.NET application. The code is very simple. He created the datatable with the data that he wants to export from database. He bind that datatable with datagrid, write the HTML code to HtmlTextWriter and write those HTML code to the response with “application/vnd.ms-excel” content type.

Download

protected void Button1_Click(object sender, EventArgs e) {
            var datatable = CreateDataTableWithData();

            System.IO.StringWriter stringWriter = GetHtmlTagsfromDatagrid(datatable);

            WriteResponse(stringWriter);
        }

        private void WriteResponse(System.IO.StringWriter stringWriter) {
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            Response.AppendHeader("Content-Disposition", "attachment;filename=myexcel.xls");
            Response.Write(stringWriter.ToString());
            Response.End();
        }

        private static System.IO.StringWriter GetHtmlTagsfromDatagrid(DataTable datatable) {
            System.IO.StringWriter stringWriter = new System.IO.StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
            DataGrid dg = new DataGrid();
            dg.DataSource = datatable;
            dg.DataBind();
            dg.RenderControl(htmlWrite);
            return stringWriter;
        }

        private static DataTable CreateDataTableWithData() {
            var datatable = CreateDataTable();

            datatable.Rows.Add(CreateDataRow(datatable, 1, "Michael Sync",
                "8/31/1982 3:19:40 PM"));
            datatable.Rows.Add(CreateDataRow(datatable, 1, "Shwesin Sync",
                "9/29/1982 3:00:32 PM"));
            datatable.Rows.Add(CreateDataRow(datatable, 1, "Elena Sync",
                "1/15/2011 00:00:01 PM"));
            datatable.Rows.Add(CreateDataRow(datatable, 1, "Tiffany Sync",
                "7/6/2012 00:00:01 PM"));

            datatable.AcceptChanges();
            return datatable;
        }

        private static DataTable CreateDataTable() {
            var datatable = new DataTable();
            datatable.Columns.Add("Id");
            datatable.Columns.Add("Name");
            datatable.Columns.Add("Date of Birth");
            return datatable;
        }

        private static DataRow CreateDataRow(DataTable datatable, int id, string name, string dob) {
            DataRow dr = datatable.NewRow();
            dr[0] = id;
            dr[1] = name;
            //dr[2] = string.Format("{0:yyyy-MM-dd HH:mm:ss}", dob); //Right code
            dr[2] = dob; // Wrong code

            return dr;
        }

Problem

We are able to view the exported file in excel but the problem is that the date formats are not consistent for all rows. Take a look at Elena’s b’day and Tifa’s b’day in this screenshot below. The date formats are different, right?

Why?

This problem occurred because Excel is using the formats from regional setting. The format of short date in my system is M/d/yyyy and the long date format is dddd/MMMM dd,yyyy. All data that follows those date format will be treated as a date in excel. Otherwise, it will be treated as a string. If you look at my b’day “8/31/1982 3:19:40 PM”, it doesn’t follow dddd/MMMM dd,yyyy format so excel treat it as a string.

Solution

There are more than one solution for this problem. You can specify “vnd.ms-excel.numberformat:dd/mm/yy” style to that cell. but the easiest fix that we came up is to set the date format as below while populating the data.

dr[2] = string.Format("{0:yyyy-MM-dd HH:mm:ss}", dob); //Right code

Conclusion

It’s just a simple issue and simple solution but we did face this issue so I’m sharing it here. Hopefully, developers who are facing the same problem might find this post useful. :)

Happy Silverlighting! Oh no! Happy sharing!!

Related ~

C# 3.0 Tutorials: What is Anonymous type?

This is my fourth part of my C# 3.0 tutorials. We have finished learning about automatic properties that introduces new and shorter way of creating the properties, object and collection initializer that gave us the way to initialize the object or collection in shorter and cool way, implicitly typed local variables that can be declared without specifying the type explicitly. I told you that implicitly typed local variables and anonymous type are the best match but I didn’t mention anything about Anonymous type in previous tutorial. Now, it is the time we start discussing about anonymous type which is introduced in C# 3.0.

What’s Anonymous type?

Anonymous type is the type that is created anonymously. Anonymous type is a class that is created automatically by compiler in somewhere you can’t see directly while you are declaring the structure of that class. Confusing? Let me show you one example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
Person _person = new Person { ID = 1, FirstName = "Michael", LastName = "Sync" };
Console.WriteLine("Name: {0} {1}", _person.FirstName, _person.LastName);
Console.ReadLine();
}
}
public class Person {
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}

Please take a look at the code above. I created a class called Person that has three properties such as ID, FirstName and LastName. In Main() function, I declared a variable and initialized the properties of the instance. Yes. this is just a normal way of creating a class and initializing the instance that we have been doing this for years.

Now, I will change the normal class to anonymous type.
Anonymous types

The first thing that you need to do is that remove the class. (C# compiler will create the anonymous class based on what you initialize with.) Secondly, we have to change _person variable to implicitly-typed local variable by replacing “Person” with “var”. Because we have removed Person class from our code so that we won’t know about the type. That’s why the variable “_person” should be declared as a implicitly-typed local variable. Then, we will remove “Person” after “new” keyword.

So, our final code will be like that below ~

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
var _person = new { ID = 1,
FirstName = "Michael",
LastName = "Sync" };

Console.WriteLine("Name: {0} {1}",
_person.FirstName,
_person.LastName);

Console.ReadLine();
}
}
}

It’s just like using object and collection initializer. but the magic comes at this point. The C# compiler will create the anonymous class based on the initializer in IL. You can’t see/access that class directly from code but you should know there is a class.

Now, I think that you understand what Anonymous types are and how to create them. But there is one important must-known thing left to tell you. Keep in mind that you can create the Anonymous type as the way that I showed above but this is NOT what it is designed for.

Let me share you one nice comment from this link.

I think that perhaps your dislike of C#’s new features stems from the fact that you are looking at the new features and trying to imagine how you would use them within the context of your current programming patterns. However, these new features are designed to be used in completely new ways.

Anonymous types are not meant to be used in places where you would care about their name or method overriding — in those cases you should use a class. Anonymous types are for when you want to just group together a bunch of data items. Perhaps you are running a query that returnsa few arbitrary pieces of data, or maybe you have a function that needs to return multiple pieces of data. Can you imagine what it would be like if you had to create a new class for each different query in an application?

C# 3.0’s new set-based operators (LINQ) give us wonderful new tools for working with data sets. One thing we frequently need is objects which do nothing besides hold data to iterate over. Do you want to define a datatype for every query that returns a different set of columns? Quite frankly, it’s a pain in the butt. It leads to a proliferation of meaningless types, type unsafety (just using objects), or somewhere inbetween (having a bunch of standard types and only filling in whatever data is returned because maintaining the types every time a query changes becomes too much work).

The type proliferation problem is especially bad in Java where each class requires its own file. As a side note, I once wrote a compiler for a language with lexical scoping (like Pascal, where a function could have an inner function that can access the local variables of its outer function). This means that each local variable scope requires its own separate data structure, which would have to have its own separate class file if the compiler were targeting the JVM. What’s the point of having a whole bunch of classes with no methods and every member public?

internal class CustomerName_OrderCount { public string Name; public int Count; }
internal class CustomerName_Country { public string Name; public string Country; }
internal class CustomerName_Country_State_Phone { public string Name; public string Country; public string State; public string Phone; }
internal class CustomerName_Country_State_Phone_OrderCount { public string Name; public string Country; public string State; public string Phone; public int Count; }

I wouldn’t want an unnecessary mess like that cluttering up my code.

Additionally, though, having anonymous types means that the compiler knows that calling the constructor and properties have no side-effects, which allows it to reason about the values in ways it otherwise could not. This is important for being able to translate queries to SQL and know that you’re getting the correct semantics.

I really like this comment and it did explain a lot. Anonymous types are designed for LINQ. If you are very new to LINQ then you might feel those C# 3.0 features are not so cool but once you got that then you will just love them. :)

Let’s take a look the practical way of using Anonymous type with LINQ ~

We have one XML file as following structure and data.

<?xml version="1.0" encoding="utf-8" ?>
<Girls>
<Girl>
<Name>Camilla Belle</Name>
<DateOfBirth>October 2, 1986</DateOfBirth>
</Girl>
<Girl>
<Name>Megan Fox</Name>
<DateOfBirth>May 16, 1986</DateOfBirth>
</Girl>
<Girl>
<Name>Vicki Zhao(Zhao Wei)</Name>
<DateOfBirth>March 12, 1976</DateOfBirth>
</Girl>
<Girl>
<Name>Kelly Hu</Name>
<DateOfBirth>February 13, 1968</DateOfBirth>
</Girl>
<Girl>
<Name>Elisha Cuthbert</Name>
<DateOfBirth>November 30, 1982</DateOfBirth>
</Girl>
<Girl>
<Name>Alicia Silverston</Name>
<DateOfBirth>October 4, 1976</DateOfBirth>
</Girl>
<Girl>
<Name>Christy Chung</Name>
<DateOfBirth>September 19, 1970</DateOfBirth>
</Girl>
</Girls>

You can easily query the data from this XML with the code below. Additionally, you will get the strong-typed variable reference. That means you will see “Name” and “DateOfBirth” as the properties of “Girl” class in intellisense.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
XDocument xmlSource = XDocument.Load("Girls.xml");

var girls = from g in xmlSource.Descendants("Girl")
select new
{
Name = g.Element("Name").Value,
DateOfBirth = g.Element("DateOfBirth").Value
};

foreach (var girl in girls) {
Console.WriteLine("Name: {0}, Date Of Birth: {1}",
girl.Name,
girl.DateOfBirth);
}
Console.ReadLine();
}
}
}

Yes. This is what Anonymous type, implicitly-typed variables and object initializer are designed for. They are so powerful and very cool with LINQ. I hope that you now have good understand about what Anonymous type is and how to use it. You should play around with your own scenario or use it in your real project if you have the chance so that you will be more familiar with those new features and will understand more than what I wrote in this tutorial. Good Luck. :)