Hello new year!

30. December 2008

It’s that time of the year again. I am usually not very interested in new year celebration, and as usual I’ll be at home with my family, which I believe more amazing than going out.

If I am to summarize this year, it’s been the most productive year for me, I joined Rhino Tools, NHibernate, Castle Project, and Fluent NHibernate(even though latter two have 0 commits by me so far) and I learnt _many_ ideas and techniques, and more importantly I met with great people.

Even though I don’t care much about new year, I think it is a great opportunity to thank people who helped me on many different issues including programming. In no particular order, I would like to thank

  1. Ayende Rahien was the first guy I came across while I was trying to find the best programming techniques and he was the one introducing the AltDotNet concept to me. He is also the founder of Rhino Tools, which is my first true OSS experience . He directly(in mailing lists) and indirectly (via his posts on his blog) helped many of us.
  2. Fabio Maulo is the current leader of NHibernate project. He asked me to join NHibernate Core after our work on Linq provider. He helped me in every commit I made, answered all my questions. Not only did I learn good programming techniques but also I believe he is a great example of a good project leader.
  3. Sidar Ok was the first Turkish speaking person I met on Alt.Net. We shared our experience on various topics and we even have some plans on introducing Alt.Net concept to Turkish .net Community(more on that later).
  4. Krzysztof Kozmic Unfortunately I don’t know how I met with this guy, but I’m glad I met. We were to start an OSS project, but due to time limitations this had never happened.
  5. Hamilton Verissimo is one of my idols. He didn’t directly help me, but I learnt a lot from his works
  6. Finally, biggest thank goes to my family and girlfriend, for their support for everything of my life.

If I forgot anybody, please forgive me, those are the ones I can remember now.

I wish you, dear reader, a great new year!

Decoupled design with events

21. December 2008

UPDATE:
Rob was kind enough to discuss on that issue. What I understand is that he thinks this increases complexity for now, since he doesn’t want to have multiple handlers, but spam service is quite enough.
Thanks Rob!

I know, I know. I am talking about Events a lot, but the reason for that is: I love them. They provide hell of a great way to decouple your design, and improve flexibility.

After reading Rob Conery’s Twitter about implementing subkismet for oxite, I decided to take a look at since I did it once, too. I take a look at it, unfortunately it wasn’t like what I expected from Rob but definitely he is a great guy!

My way of handling this issue will be based on events and the code is in BlogSharp codebase

As an illustration, I will work on comment adding process, where a user comes and write comments.

There is a problem with Rob’s implementation, IMHO. Spam control should not be part of comment adding process. Putting it right into Create method of CommentController will viloate the SRP and OCP as there may be some other controls on the comment as well, such as check for duplication, or flood, etc. You may also want to notify the author of the post whenever a comment is added.

All these issues can be easily addressed by using events.

public class PostService : IPostService
{
    #region IPostService Members

    public void AddPost(IPost post);
    public void AddComment(IPostComment comment)
    {
        var commentAdding = new CommentAddingEventArgs(comment);
        this.CommentAdding.Raise(this,commentAdding);
        if (commentAdding.Cancel)
            return;
        Repository<IPostComment>.Instance.Add(comment);
        var commentAdded = new CommentAddedEventArgs(comment);
        this.CommentAdded.Raise(this,commentAdded);
    }

    public void RemoveComment(IPostComment comment);
    public void RemovePost(IPost post);
    public IPost GetPostById(int id);
    public IPost GetPostByFriendlyTitle(string friendlyTitle);
    #endregion

    #region IPostService Members
    public event EventHandler<IPostService, PostAddingEventArgs> PostAdding = delegate { };
    public event EventHandler<IPostService, PostAddedEventArgs> PostAdded = delegate { };
    public event EventHandler<IPostService, PostRemovingEventArgs> PostRemoving = delegate { };
    public event EventHandler<IPostService, PostRemovedEventArgs> PostRemoved = delegate { };
    public event EventHandler<IPostService, CommentAddingEventArgs> CommentAdding = delegate { };
    public event EventHandler<IPostService, CommentAddedEventArgs> CommentAdded = delegate { };
    #endregion
}


Then, you let your plugins add themselves as event handler for particular action, and act accordingly.

PS: Raise is an extension method (thanks Sidar Ok for warning me about the ugliness of previous implementation)

public static class EventHandlerHelpers
{
    public static void Raise<TSource,TEventArgs>(this EventHandler<TSource,TEventArgs> @event,TSource source,TEventArgs eventArgs)
    {
        if(@event!=null)
            @event(source, eventArgs);
    }
}

kick it on DotNetKicks.com

,

Contributing to OSS – Creating a test case & patch

15. December 2008

For some time from now, I have been _trying_ to contribute some opensource projects including NHibernate, Castle(even though i couldn’t contribute the latter yet). People are trying to help the projects by creating issues in the issue trackers. The thing we come face to face usually is that the information provided in the issues are not enough to reproduce, or too time consuming to write a test for(e.g. NHibernate tests usually needs 3 files, 1 for test fixture, 1 for mappings and 1 for the entity). This issue prevents developers from fixing the issue very quickly.

In this post, I will try to explain how to write a test for NHibernate codebase, but the idea is the same for all projects.

There was already a really good article in NHForgegoing over the same steps I am going to follow, but I want people get used to OSS world, hence Source Code Management

Getting the source code

You need to have an SVN client, and during this article I will use Tortoise SVN directly (there are some other clients namely VisualSVNand  AnkhSVN (free)built on top of Tortoise SVN).

  1. In order to get the source code, you should create a folder named nhibernate wherever you want.
  2. Right-click the folder, and press SVN Checkout
  3. Enter the URL of repository(https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/ for nhibernate) and imagepress OK to check out the code.  
  4. Luckily, I was able to get the code in one trip, but this is not always the case. Connection may get broken, or sourceforge may kill you during the checkout. If this is the case, right click the folder and click on Update

    image  
  5. Open a command window in that folder, go to nhibernate subfolder and type nant. This action not only builds the sourcecode, but also generate AssemblyInfo.cs which is required for you to build solution in VS.

    image 
  6. Now you are ready to open solution in visual studio. Go ahead and double-click on .sln file.

    image

    Ups wtf is that? Due to some custom build steps nhibernate is using, visual studio warns us. Uncheck the box below and click on Load project normally. Then press ok.

Creating the test case

The best way to illustrate a bug is a test case. I am probably the last person to talk on how a unit test should be and there are already some good articles on what to do / or better what not to do on the web, but i want to illustrate some points that I think important.

  1. Isolation
    A test case should be isolated from every other non-relevant  component. I see several times that people are creating tests with other projects dll’s. This can be really hard for us to isolate. NHibernate tests shouldn’t have any other dependency other then NHibernate itself.
  2. Short test cases
    It may not be easy to find where the bug is when we have tens of lines in a test. This idea is related to isolation in someway and should be taken into account.
  3. Verify results
    There are times when I see a test code that doesn’t check for the results, but only checks if it doesn’t throw exception. This is plain wrong! A code may not throw an exception, but still not produce the correct result. Please take care with this issue.

I know, I talked a lot, and now it is code time!

  1. First thing you should do is to create a test folder named NHXXXX in NHibernate.Test/NHSpecificTest. Tests that are related to bugs go into NHSpecificTest for tracking purposes.
  2. Second thing is to create entities. (Assuming that you’ll need to demonstrate it using a session and you’ll query the database, otherwise you can skip this step and the following).
    public class MyClass
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
    }
  3. Now it comes to mappings. The thing you must be aware of is that if you give “Mappings.hbm.xml”, NHibernate will automagically catches it.
    <?xml version="1.0" encoding="utf-8"?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="NHibernate.Test"
                       namespace="NHibernate.Test.NHSpecificTest.NH1552">
    
        <class name="MyClass">
            <id name="Id">
                <generator class="native"/>
            </id>
            <property name="Name"/>
        </class>
    </hibernate-mapping>
  4. Now we should create the test case. Our test case should inherit from NHibernate.Test.NHSpecificTest.BugTestCase to make the things easier.

    If our test is specific to a dialect, we should override virtual bool AppliesTo(Dialect dialect) method, and for this blogpost we’ll use issue NH-1552which is specific to MsSql2005Dialect
    protected override bool AppliesTo(NHibernate.Dialect.Dialect dialect)
    {
        return dialect is MsSql2005Dialect;
    }

  5. There are some predefined methods for the test case such as OnSetup() and OnTearDown(). In OnSetUp() method, you initialize the data and on TearDown() you generally only remove the data. For our purpose, since we check if paging works with SqlQuery, we need at least 3 items.
    protected override void OnSetUp()
    {
        using (var session = this.OpenSession())
        {
            using (var tran = session.BeginTransaction())
            {
                MyClass newServ = new MyClass();
                newServ.Name = "tuna";
                MyClass newServ2 = new MyClass();
                newServ2.Name = "sidar";
                MyClass newServ3 = new MyClass();
                newServ3.Name = "berker";
                session.Save(newServ);
                session.Save(newServ2);
                session.Save(newServ3);
                tran.Commit();
            }
        }
    }

  6. Now we write the TearDown() to remove the data. 
    protected override void OnTearDown()
    {
        using (var session = this.OpenSession())
        {
            using (var tran = session.BeginTransaction())
            {
                session.Delete("from MyClass");
                tran.Commit();
            }
        }
    }

  7. Now we need to write 3 test cases for paging, one with FirstResult, one with MaxResult, and one with both.  
     
    I have to admit that _works_as_expected_ doesn’t mean much, but that’s the way i like it.
    [Test]
    public void Paging_with_sql_works_as_expected_with_FirstResult()
    {
        using (var session = this.OpenSession())
        {
            using (var tran = session.BeginTransaction())
            {
                string sql = "select * from MyClass order by Name asc";
                IList<MyClass> list = session.CreateSQLQuery(sql)
                    .AddEntity(typeof(MyClass))
                    .SetFirstResult(1)
                    .List<MyClass>();
                Assert.That(list.Count, Is.EqualTo(2));
                Assert.That(list[0].Name, Is.EqualTo("sidar"));
                Assert.That(list[1].Name, Is.EqualTo("tuna"));
            }
        }
    }
    
    [Test]
    public void Paging_with_sql_works_as_expected_with_MaxResult()
    {
        using (var session = this.OpenSession())
        {
            using (var tran = session.BeginTransaction())
            {
                string sql = "select * from MyClass order by Name asc";
                IList<MyClass> list = session.CreateSQLQuery(sql)
                    .AddEntity(typeof(MyClass))
                    .SetMaxResults(2)
                    .List<MyClass>();
                Assert.That(list.Count, Is.EqualTo(2));
                Assert.That(list[0].Name, Is.EqualTo("berker"));
                Assert.That(list[1].Name, Is.EqualTo("sidar"));
            }
        }
    }
    
    
    [Test]
    public void Paging_with_sql_works_as_expected_with_FirstResultMaxResult()
    {
        using (var session = this.OpenSession())
        {
            using (var tran = session.BeginTransaction())
            {
                string sql = "select * from MyClass";
                IList<MyClass> list = session.CreateSQLQuery(sql)
                    .AddEntity(typeof(MyClass))
                    .SetFirstResult(1)
                    .SetMaxResults(1)
                    .List<MyClass>();
                Assert.That(list.Count, Is.EqualTo(1));
                Assert.That(list[0].Name, Is.EqualTo("sidar"));
            }
        }
    }


  8. Now make sure that the tests are failing(now they all pass after we apply patch from Dana)

    image
    Now it is failing, btw, the image is from Resharper, which is a joy to use but you can also use NUnit
  9. If you can fix the bug, go ahead and fix it, if not it’s still fine. Jump to create a patch section

     

    Creating a Patch

    image 
    Creating a patch produces a diff for modified code pieces. When you click on Create Patch, you will probably see a bunch of files in the list, please make sure you only choose the ones you have added.


    image

    That’s it, take this patch file and send it to JIRA

    kick it on DotNetKicks.com

, , , ,

Yet another benefit of Twitter

11. December 2008

I have been using Twitterfor a while, and I am enjoying it. It gives me a chance to follow what other people I know do and it can really be insightful to read the discussions going on twitter between known figures of .net.

Some minutes ago, I got one more benefit of it: An answer to a question from a person that I don’t know. He doesn’t even follow me.

image 

Thank you very much @ninereeds. I really appreciate it!

Writing Testable Time Dependent Code

2. December 2008

I’ve recently received an email about a code snippet I’ve provided in Ayende’s post. Ayende, as usual, provided a simple yet good code for this problem. Mine is more complex way of doing this, but i like the pattern behind it.

You should be aware that there is no way to make DateTime.Now stop(if you don’t want to touch system clock and run your test in virtual environment, of course). You have to abstract it.

public class TimeAbstractionTests
{
    public TimeAbstractionTests()
    {
        CallContext.SetData(Constants.CONTEXT_STORE_KEY,null);
    }


    [Fact]
    public void Time_should_set_to_System_time_for_first_time()
    {
        DateTime x = Time.Now;
        Assert.Equal(DateTime.Now, Time.Now);
    }

    [Fact]
    public void Time_set_to_system_time_when_only_system_time_is_selected()
    {
        using(Clock.System())
        {
            Assert.Equal(DateTime.Now, Time.Now);
        }
    }

    [Fact]
    public void Time_should_freeze_when_frozen_time_is_selected()
    {
        DateTime frozenTime=new DateTime(2011,11,11);
        using (Clock.Frozen(frozenTime))
        {
            Assert.Equal(frozenTime, Time.Now);
        }
    }

    [Fact]
    public void Time_should_nest()
    {
        DateTime frozenTime = new DateTime(2011, 11, 11);
        using (Clock.Frozen(frozenTime))
        {
            Assert.Equal(frozenTime, Time.Now);
            using (Clock.System())
            {
                Assert.Equal(DateTime.Now, Time.Now);
                using (Clock.Frozen(frozenTime))
                {
                    Assert.Equal(frozenTime, Time.Now);
                }
            }
        }
    }
}

This tells what I’d like to achieve pretty well. I want one FrozenTime, one System time, and I want them to nest(no specific reason actually)

For nesting, Stack<T> structure should be used. Everytime I ask for a Clock, it puts the demanded Time into stack stored in CallContext, and Time.Now thing takes the latest ITime instance from that stack.

Implementation for FrozenTime and DefaultTime is simple, here they are.

public class FrozenTime:ITime
{
    public FrozenTime(DateTime now)
    {
        this.now = now;
    }

    private readonly DateTime now;
    #region ITime Members

    public DateTime Now
    {
        get { return now; }
    }

    #endregion
}public class DefaultTime:ITime
{
    #region ITime Members

    public DateTime Now
    {
        get { return DateTime.Now; }
    }

    #endregion
}

Now it comes to the Time class. As I said, it gets the topmost ITime(using Peek(), since we don’t want to remove) instance from the stack and invokes .Now on it.

public static class Time
{

    public static DateTime Now
    {
        get
        {
            Stack<ITime> timeStack = 
(Stack<ITime>) CallContext.GetData(Constants.CONTEXT_STORE_KEY); if (timeStack == null||timeStack.Count==0) { Clock.System(); timeStack =
(Stack<ITime>) CallContext.GetData(Constants.CONTEXT_STORE_KEY); } return timeStack.Peek().Now; } } }

Clock class is where I say that I want xxxxTime to be active during the scope. In order to support using(Clock.System()) like usage, I must have something IDisposable. I would have implemented IDisposable for ITime implementations, but it violates SRP so I choose another way.

public class Clock
{
    public static IDisposable System()
    {
        var time = new DefaultTime();
        PushIntoStack(time);
        return new DisposeCallback(PopFromStack);
    }
   . . . . . . 
}

As you see, when somebody calls .Dispose() in callback(which is done at the end of using() structure) it pops the latest ITime instance.

This is definitely more work than Ayende’s solution, but I like this way more.

You can download the solution here(Time.rar (35.61 kb))

kick it on DotNetKicks.com

,

Is it likely(), or unlikely()?

1. December 2008

This post is relatively off-topic compared to my other posts on C# and .net but I see a value behind it, so decided to blog about it.

While I have been dealing with my assignment on linux system calls, I have seen something interesting. There were 2 macros in use for condition evaluation: likely(condition) or unlikely(condition). This is a compiler optimization used in linux kernel and it isn’t meant to be used in normal applications. It is even useless in ordinary applications. However, this is definitely not the case for an OS kernel where even a single instruction for performance matters.

Now, how does this work? What is the magic behind it?

When we take a look at the definition of likely and unlikely macros we see

#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)

I didn’t know about __builtin_expect, so I searched for it and reached that page.

It says that this call can be used to provide the compiler with branch prediction information and the compiler uses it to produce optimized assembly code. Something had appeared in my head, I wonder if there was more magic going on so I gave it a go.

int test_likely(int c)
{
    if(__builtin_expect(c,1))
    {
        c++;
    }
    else
    {
        c=1
    }
    return c;
}

This code yielded in the following assembly code

image

And for unlikely,

int test_unlikely(int c)
{
    if(__builtin_expect(c,0))
    {
        c++;
    }
    else
    {
        c=1
    }
    return c;
}

compiler produces

image

Do you see the difference? On line 13(d), we see that it executes jne or je instruction. If the condition is likely to happen, it doesn’t branch and executes the code under if, otherwise branch to else. If the condition is unlikely to happen, it keeps else close to the code, since else block is likely to be executed.

Those kind of optimizations should be used as a last resort. In most applications, you don’t even need such optimizations, you should concentrate on bottlenecks instead, such as network traffic etc.

I’d like to end this post with the quote I already said:

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” (Donald Knuth)

, ,