RSS
Facebook
Twitter

Thursday, August 26, 2010

Introducing CountdownEvent

The new .NET 4.0 has a lot of new multi-threaded goodness, one of which is the CountdownEvent, used to signal between thread just like the good old AutoResetEvent and ManualEvent.

The big difference (as you might have already guessed) is that CountDownEvent can be used to signal after several set has been called.

The reason I’m telling you about this is because today needed to write an integration test to check communication between a client class to a server class (never mind the specifics). Here I was in the middle of an intensive TDD session and I got to a point where I needed to check if two clients send messages to the server two messages arrive at the server and he messages were asynchronous meaning that I needed to delay the test until they arrive.

Using the simple event just didn’t work so I’ve went to the place I go when I have a multi-threading question – Threading in C# Ebook which was updated to include .NET 4.0 and found about CountingEvent – and presto:

[TestMethod]
public void TwoClientsCallServer()
{
client1 = CreatClientAndConnectToServer();
client2 = CreatClientAndConnectToServer();

var countdownEvent = new CountdownEvent(2);

server.OnMessageArrived += (sender, args) => countdownEvent.Signal();

client1.SendMessage();
client2.SendMessage();

var result = countdownEvent.Wait(1000);

Assert.IsTrue(result);
}


I’m not too happy about using timeout in my tests but this is one of these times where I couldn’t find a better solution.



Being curious about how CountdownEvent works I’ve opened Reflector and did a simple investigation



image 



It’s quite simple if you read past the synchronization and the goto – it uses a wait handle (slim) and a counter (int) and it set the handle once that the counter has reached the specify number of “events”.



 



It’s awfully nice of Microsoft – implementing this class, so we won’t have to.



Thursday, August 19, 2010

How to run IronPython code from C#

I’ve just got back from a joint session with Shay at the local .NET user group, I’ve presented IronPython after an excellent IronRuby session done by Shay.

One example I didn’t have the time to show was how to run IronPython script from within C# code. After the session I was asked by a group member to show this exact demo. So without further ado here is how to run IronPython from within the comfort of your C# application:

Step 1: Add references

Add the following assemblies to your project:

  • IronPython.dll
  • IronPython.Modules.dll
  • Microsoft.Scripting.dll

All of the assemblies above are can be found under the IronPython installation folder.

Step 2: write code

Running IronPython is very easy – I’ve decided to create a console application and run an IronPython file that was passed as an argument:

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace IronPythonFileRunner
{
class Program
{
static void Main(string[] args)
{
ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);

ScriptSource source = engine.CreateScriptSourceFromFile(args[0]);
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
}
}
}


You can use the same code to embed IronPython by replacing engine.CreateScriptSourceFromFile with engine.CreateScriptSourceFromString.



Introduction to IronPython



Want to learn more about IronPython? take a look the the whole presentation



or read my Getting Started with IronPython posts.





Wednesday, August 11, 2010

TDD is more than just writing unit tests

In the last few weeks I’ve been presented with a new idea – a team that writes automated unit tests after the code has been fully implemented. Coming from a test first background the practice of writing unit tests seemed – just wrong.

If writing unit tests for your code is a good practice – than writing them before is even better.

When I inquired for the reason for writing code before tests I was answered that “writing the test before is hard and requires understanding of the code”, which means that unless you can predict the future writing tests for the yet-to-be-written code is nearly impossible - wrong again! claiming that in order to write tests you need actual code is just like saying that you cannot write user stories (or requirements) without implementing them first.

Don’t forget that the ‘D’ in TDD is stands for design – you design your code via tests not the other way around.

In fact writing the test after the code has been implemented is harder – you have to be very carful not to miss any feature and to write tests for every single line of code – a problem you don’t have when writing the tests first because you only write code that has a failing test.

The team that used the “test later” approach had another issue – they had a lot of dead code, methods and classes that were not even used or needed that were created because of some obscure requirement. because the feature was not really needed it didn’t have unit tests and just “rotted” over time. These methods would have never been created if test first paradigm was used.

 

From my brief experience writing the tests after the code is just harder and requires excellent memory and is mostly and exercise in futility. The end of this story is yet to be seen, in the meantime I’m teaching the team about TDD and I hope I can show them the benefits of writing tests as a design methodology…



Sunday, August 01, 2010

IronRuby and IronPython on the next Israeli .NET User Group Meeting

This year I’ve decided to learn about Dynamic languages. If you’ve been reading this blog for a while you know that I’ve been learning IronPython for the last year and now I want to share my experience with other .NET developers, And for that purpose I’ll be presenting the .NET dynamic languages with the help of Shay Friedman (aka IronShay) at the Israeli .NET user group.

When?

August 18th, 17:30-20:30.

Where?

The Israeli .NET User Group (IDNUG) August meeting,
Microsoft offices, Dekel room
HaPnina 2, floor 0
Raanana, ISRAEL

What?

17:30 - 18:00   Assembly
18:00 - 19:15   “Introduction to IronRuby”
                           Shay Friedman
19:15 - 19:30   Break
19:30 – 20:30  “Introduction to IronPython”
                           Dror Helper

Abstract #1: Introduction to IronRuby
Ruby has been a home for some great innovative frameworks like Ruby on Rails, Cucumber and Rake. IronRuby has recently been released, unleashing the power of Ruby to the .NET world. In this session you will get familiar with the Ruby language and its amazing ecosystem and you will learn to take advantage of it in your everyday development tasks. Come and see how this great new addition to the .NET family makes your development process faster, clearer and happier.

Abstract #2: Introduction to IronPython
Do you want to learn about dynamic languages and their uses? IronPython is a good place to start. This dynamic .NET language can be used to develop just about anything – windows application, web services and Silverlight to name a few. This session will explain what Python is all about and how to write .NET applications using IronPython. The session is intended for .NET developers without any previous knowledge of Python that want to learn about the power of Dynamic Languages.

How?

Register now at https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032456794&culture=he-IL

 

So if you’re interested of learning more join us on august 18th – for an opportunity to see a great speaker (and myself ;) ).



Related Posts Plugin for WordPress, Blogger...