Showing posts with label XUnit. Show all posts
Showing posts with label XUnit. Show all posts

Wednesday, September 25, 2013

Enterprise Library 6 - SLAB - Supporting Complex Types for Payload

Previously we have discussed there are only certain primitive types supported by EventSource introduced in .net framework 4.5. This post is an attempt how we can work around this limitation. Actually, we can decorate EventSource methods with [Event] and [NonEvent] attributes. The other most important point is the following:

[Event] methods can be private.

We can use this to only include private [Event] methods which would be used to generate ETW manifest from the EventSource. Additionally we can introduce public methods in the EventSource type supporting the non-primitive custom type as parameters. Hence these object-oriented types make writing the logs easier without putting any load on the underlying infrastructure supporting the event mechanism.

Let's create a sample C# project targeting .net framework 4.5. We add a type EventSourceWithComplexType to the project. As you can see below this is an EventSource. Here we are using Lazy initialization introduced in .net framework 4.0 for singleton implementation [Please refer here for singleton implementation]. It has a private constructor to make sure that it cannot be instantiated from outside. The most noticeable thing is its two methods. Here we have a method StudentCreatedCore() decorated with [Event] attribute, has a list of parameters including types supported by the ETW infrastructure. It has a corresponding StudentCreated() method with [NonEvent] attribute. It has a public access modified and has a non-primitive type for parameter. The most important thing is that it is using the StudentCreatedCore() method.


Can The implementation satisfy EventSourceAnalyzer?
We have discussed that we can use EventSourceAnalyzer for testing our EventSource implementation [Discussion]. Let's see if we can satisfy EventSourceAnalyzer for the above implementation. We just need to call InspectAll() method with an instance of the EventSource. Here we are using xUnit for testing supported by resharper. You need to install the nuget package. You would also need to get EnterpriseLibrary.SemanticLogging package for using EventSourceAnalyzer.


As a matter of fact, EventSourceAnalyzer has no complaints and the unit test passes. Zindabad!



Listening for Events
Now let's see if we can listen for events using this EventSource implementation. Here we are attaching the EventSource with ConsoleSink using the ConsoleLog [Discussion]. We are using the same EventLister to enable the events. Here we are just using the public method on EventSource type with an instance of Student type. Let's see if we could see the event's data on the console [keep the spirit high, friend!!!].


And we do see that event's data is being printed on the console in the same format as we provided in the StudentCreatedCore() method. Hence the runtime was able to do it from the private method. It has no problem with generating the ETW manifest for private [Event] methods and using it at run-time. Zindabad!



Download


Love for other people what you love for yourself.    [Prophet Muhammad - Salallahu-Alaihe-Wasallam]

Saturday, September 14, 2013

Event Source Analyzer for Potential Run-time Errors

You might write the best EventSource with all the required methods for logging. But we must remember that your event source would generate manifest and the manifest is used at run-time to write ETW events. This might result in unexpected runtime errors. One wonders how those issues can be found out earlier in the development cycle. Semantic Logging Application Block provides EventSourceAnalyzer for just this purpose. It is used to verify the run-time checks for an EventSource instance.



The type provides Inspection methods. In case of a possible failure during verification, it throws EventSourceAnalyzerException. I think the best usage of this type is to write unit tests to verify such checks for the defined EventSource.



Checks Performed
Now what checks are performed by EventSourceAnalyzer. It performs a 3-point check on EventSource. The tests are as follows:
  1. Is there any error when the given EventSource is enabled by an EventListener?
  2. Can event schema be requested from the EventSource?
  3. Can all Event based methods be invoked in the specified EventSource class?
If the answer of all the above questions is Yes, the unit test is passed!

Let's Start Testing Our EventSource
Here is an method from an EventSource. The compiler generates manifest for ETW events based on the method parameters of Event method. So at run-time calling WriteEvent with a different parameter list than the generated manifest would cause failure. The same is the case with the following EventSource method.


If we use EventSourceAnalyzer for an EventSource containing this method, this should result in the an exception providing the details about the possible issue at runtime.



Generating an ETW event with a non-existent EventId would also result in failure at runtime. Let's see the following method where the Event method is decorated with an EventId which would be used to generate the manifest for the associated event.


Now at runtime it could generate an event with an Id in the manifest just because of an incorrect argument to WriteEvent method. We can determine that earlier in the cycle. The analyzer would result in the following exception in this case.



There is a typo here with the exception message. I have logged a connect item for this. If you really dislike this then you can vote for this.



There are only a handful of types which could be used for ETW events. Someone has specified a unofficial list here as follows: [ Boolean, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, String, Guid]. Since WriteEvent accepts an object array as method argument, the compiler wouldn't have any issue if we pass an instance of an unsupported type. This would only cause failure at runtime. Look at the following method which is using DateTime type.


The Analyzer wouldn't like this and would result an error. Here is the generated exception for the above case:



The analyzer even checks for the order of the parameters in the WriteEvent and the EventSource method. They should be in the same order. In the following we are passing arguments to WriteEvent in a different order than EventSource method.


EventSourceAnalyzer can be a little flexible about this. We can use the following property so that the order is not checked.



Because of the way EventSourceAnalyzer inspects the EventSource, we might need to set ExcludeEventListenerEmulation as well. So that EventListener is not used, otherwise, it would mimic the runtime and would result in failure. The order is important at run-time so we should avoid setting this property to true generally.


The analyzer can also be used to verify that not only the parameters count and order should match, but the types of EventSource method and WriteEvent should also be matching. Here is the exact case in the following:


This would not be checked by EventSourceAnalyzer by default. We can set the EventSourceAnalyzer as follows so that this is also checked.


Since the methods in the EventSource are used to define Event Types for the resulting Event Provider. There should be no non-event methods in the EventSource class. But we do need helper methods sometime. All such helper methods should be clearly marked as NonEventAttribute. Yes, even if they are private.



Writing Unit Tests
As you might have noticed, EventSourceAnalyzer is provided by Semantic Logging Application Block [SLAB] in Enterprise Library 6.0. On the other hand EventSource is provided by .net framework 4.5. SLAB extends the EventSource by providing the amazing ways we can utilize the event data but it is not a requirement to generate ETW event data. We can even have an out-proc event listener which could utilize the generated data and direct it to any sink. The source project doesn't need any reference of the application block.

But we still want to test the logic, we can just install the package to our unit test project. In the unit test we can verify all these checks pretty early. These unit tests can be added to our continuous integration build. Let's install the application block nuget package to our unit test project.



We can use xUnit for our unit tests. As we have discussed before, we can add its support in IDE by installing a Resharper extension. Here we are installing a release version of the package.



Let's add the following unit test to inspect EventSource for runtime checks.


In case a test fails, it shows it as follows:



Download

Monday, May 20, 2013

Writable Property Assertions using AutoFixture.Idioms

AutoFixture.Idioms allows us to write unit tests which tend to follow common templates. Mark Seemann calls it Idiomatic unit testing. Previously we have discussed about testing guard clauses using Guard Assertions. In this post we are going to discuss about testing writable properties. Unit testing property assignments is a big part of our unit tests. AutoFixture.Idioms provides WritablePropertyAssertion to test all such properties in a single unit tests. This minimizes the unit testing effort and keeps the unit tests less brittle.

You would need to add the nuget packages for xUnit , AutoFixture and AutoFixture.Idioms as described in the previous post. Let's add a Student class in our project. It has two writable properties StudentId and StudentName.

You might be thinking about separate tests for each writable property in Student. This makes it more difficult as the number of such properties increase. With AutoFixture, we just need one unit test to verify that the properties are being correctly assigned and they reflect the assigned value afterwards. Just see how simple the unit test has become.

Let's assume we have a defect in our code which is causing a property not to be assigned properly. Here we have StudentId always return a fixed value no matter the value assigned to it. This is definitely a defect. Let's update the code as follows:

The unit test should check for such defects and should report. When we run the unit test now, it does fail with the following details:

Thursday, May 2, 2013

Resharper 8 EAP - New Features

Technology tools and components providers have learnt that involvement of community from the very early part of development process is very critical for a successful product. Microsoft now has Community Technology Preview (CTP) versions in order to bring community on board as the features are being added. Based on the community feedback the features are tweaked and improved. There is also user voice where you can add the features you want. You can also vote on the features entered by other community members. It provides a place for stake holders to give ideas about new features or improvements about other features. The example user voice pages can be found for Visual Studio and Windows Phone. You can also use connect page to submit bugs and suggestions for existing Microsoft Products. This includes us, as developers, being in complete product life cycle. Agile helps!!!

Jet Brains [the provider of Resharper] has a similar program to include developers as the features are being added to their products. They have named it Early Access Program [EAP]. I don't know when they started this but I used the first EAP version for dotPeek. Many of my friends migrated to it when RedGate put a price tag on Reflector.

Resharper 8 is still in EAP program. There is a build every 10-15 days with additional / improved / fixed features. You can download and play with it. For you it makes it easier for you to transition when the tool is out in production. If there is a defect then your feedback helps JetBrains fix it. In this post I wanted to discuss about the exciting new features that I can see myself depending on a lot when the product is out. The nightly build of Resharper 8 EAP version can be downloaded from here:



Once downloaded, the EAP version can be installed on the machine with administrator's rights. The supported versions which couldn't be found on the machine are just grayed out. Since I already have Visual Studio 2012 on my machine now, you can see that it is only providing me the option to select that visual studio version.



Resharper Extension Manager for Resharper Plugins
Resharper 8 is introducing a new mechanism for releasing and managing plugins through Extension Manager. This is similar to Extension and Updates feature in Visual Studio. Since resharper is itself a Visual Studio Extension, the name Extension Manager can be confusing for some. People might think that resharper has provided a new way to manage those extensions downloaded from Visual Studio or any custom Extensions & Updates gallery. The Extension Manager is supposed to manage Resharper Plugins. You can find it in Resharper Menu.



It has a very similar view as Visual Studio Extension & Updates Dialog. There are three tabs to search an online or custom plugins gallery. You can see the list of installed plugins and uninstall them. If there is an update to an already installed plugin, it can be found in Updates tab. These are the similar tab to Visual Studio Extensions & Updates dialog.



The extension galleries can be configured through Resharper's options. If you select Settings in the above dialog then it just opens up the Options dialog with Extension Manager settings selected. You can also open the same dialog from Resharper -> Option in main menu. Here we can add, update or remove an already configured gallery. We can also disable a gallery, which can be enabled again if you want.



As discussed above, the new Extension Manager can be used to install and uninstall a plugin. The feature also supports disabling a plugin. The configuration is in the Plugins tab in the same Options dialog. You can also find the other details about a plugin here including its version number.



Please remember that these plugins are provided by community and they can have their own license terms. You can also create your own plugin and host it on JetBrain's plugin store. There are currently only two stable plugins here. I don't know how costing would work here. I really liked that these plugins can have their own dependencies.



Assembly Explorer
It is very natural for a product to atleast provide those required features that a competitor provides. The products then add extra useful features to gain competitive edge. You can find the same pattern in all markets including smart phones. Since RedGate's Reflector integrates with Visual Studio, JetBrain also needed to included its assembly exploring tool into visual studio. dotPeek is resharper's pet to explore an assembly. The same feature are added in Resharper 8 EAP in the form of Assembly Explorer. You can find the tool in resharper's main menu.



Assembly Explorer is provided as another tool window which can be handled like any other tabbed window in Visual Studio. This should greatly help in multi-monitor scenario. Here we can see other assembly references to determine assembly dependencies. We can also see the list of namespaces and the types contained by these namespaces. We can drill down in the tree to get the details of the members of these types. It is pretty much the same as in Object Browser. The difference is that Object Browser has a few extra sections to provide type's member's details and documentation.



Code navigation is supported by double clicking an item in Assembly Explorer. There are different options provided to support this. One of the option is to use the dotPeek decompiler engine. This is a reverse-engineering utility from JetBrains. It also supports using Code Definition window.



An assembly opened in assembly explorer can be exported to project. The option is provided in the context menu for an assembly in Assembly explorer. It seems that the same feature is being added to dotPeek 1.1 EAP. So the limitations are same. It can only be exported in to a C# project.



You can provide the following settings for the new project. Once a project is already exported, it allows to use and open the already exported project. You can also create a new solution file and open the project in Visual Studio.



The list of assemblies currently loaded form a session. This can be saved to load the same session in the future. The list is exported as dotPeek assembly list (dpl) file.



I think this is a great feature but this can be improved by providing support in additional contexts. A user should be able to open assembly explorer wherever he can select an assembly in Visual Studio. It includes Project's assembly references and Object Browser. An extra context menu item can be provided here to see the details of assembly contents. Like Object Browser, it can also support providing code documentation. It doesn't have to add them in a separate tab. As as starting point showing the documentation in tooltip should work. I can understand that introducing a new window has given JetBrain more control but these features might have been integrated with Object Browser as enhancement feature in order to build on the current knowledge and experience of developers.

Command Line Tools
Resharper 8 is expected to come up with a few command line tools. You might be wondering when it is already integrated with Visual Studio then why do we need these additional command line tools. Basically these tools are not intended to be used on developer's machine, they are provided for Continuous Integration scenarios for build machines. These tools provide the same check as we have on developer's machines. They would provide an extra defense for pre-commit checks avoiding unaccetable check-ins. The tools can be downloaded from the line provided above as a compressed file. They are not part of the installation package for Reharper 8. I am planning a separate post to discuss about command line tools in Resharper.



Supporting xUnit based Unit Tests
xUnit is yet another unit testing framework. You can install it from Nuget Pacakge Manager in your Visual Studio Test Project.



Visual Studio 2012 doesn't have any built-in support for running MSTest based unit tests. Running xUnit based unit tests would result the following in Output window.



Resharper also doesn't have any built-in support either. Running the same tests in Resharper would result in the following:



But you can add this support by using an EAP plugin for resharper. Just search for the following in the Resharper's Extension Manager as follows and install it. I needed to re-install my Visual Studio to actually start using it.



Let's see how we can use it now. Let's assume we have the simplest calculator in the world. The calculator just has one method, called Add. The method takes two operands, adds them and returns the result.

Now we need to test this in xUnit. We can add xUnit tests by decorating the test methods with Fact attribute as follows:

Now just run it using resharper. You can notice that you can run it now and the output is shown as follows:



Resharper 8 EAP has also introduce action indicators to streamline between gutter marks and bulbs. You can find it in the editor:



We can always go back to settings based on earlier version of resharper in the options dialog.



Note: This is not an exhaustive list of new features in Resharper 8 EAP. There are various other improvements. To see them all you can visit Jet Brain's blog. These are the features which caught my immediate eye and got me excited. Let's continue to explore!!!

Download Code

Wednesday, September 24, 2008

SSIS Unit Testing Framework

Hi guyz,

I have always been displeased being not able to unit test my packages
created in SSIS. This was because there was no way to do this.

But the days of darkness are gone. SQL Server Integration Services
Community has felt this need of the day and has come up with a Unit Testing
Framework. Now guyz! if you are not from development background even then
you don't need to worry because no .net code has to be written to write
your unit tests. All you have to create is an XML files with your commands.
So you don't need to know any .net code. Still I have not found out if I
want to write some .net code then how can I do that.

The thing I liked most is that this Unit Test framework is based on xUnit.
It means it follows the same setUP, test. and tearDown flow. There is also
support for Test Suites. The developers of Microsoft Dynamics AX should be
happy to listen to this news.

To keep ease in consideratino, a GUI has been added to create Unit Tests.

I am copying a sample XML used for this framework from codeplex.



You can download it from the following codeplex.com link:
SSIS Unit Testing Framework


You can see that it is just version 1.0 release in Aug, 2008. So
don't expect it to be all bug free.

You would be glad to know that the utility is available as .

So I can't wait to put my hands on this. Share your experience with me
about this.

Enjoy!