Oren Eini

CEO of RavenDB

a NoSQL Open Source Document Database

Get in touch with me:

oren@ravendb.net +972 52-548-6969

Posts: 7,582
|
Comments: 51,212
Privacy Policy · Terms
filter by tags archive
time to read 1 min | 124 words

Is unshamelessly a word? Nevermind about that, I've a real problem here.

If you're not aware, I'm the author of a fairly nice (and I'm being objective here) mocking framework for .Net. So what is the problem? I'm reading a lot of stuff on the TDD mailing list, and mocking comes up fairly frequently, especially lately, for some reason. So far I managed to keep from putting little comments like: "In Rhino Mocks you can do this easily by...", since I think that this isn't nice, and on the long run, it will sound as if I'm trying to sell it (even though it's free).

Any ideas on what is the recommended behavior?

time to read 7 min | 1227 words

A few days ago, Ron Jeffries offered a challange, find a way to use mocks to battle dead locks using mutexes. This is following a huge debate on the Test Driven Deveopment list about mocks, pros & cons. I think you can guess where I am on this debate. Anyway, this is exactly the kind of a challange that I like, so I gave it a shot.

Now, it's not a 100% solution, I'll be the first to admit, but it's good enough as a proof of concept as well as answering the challange. Now, for the purpose of discussion, I limited myself to the following properties of mutexes:

  • You can only acquire & release a mutex, you can't pulse it, try acquiring, etc.
  • An actual mutex is not involved, as a matter of fact (I could add it, but then what would happen with a real dead lock?), beside, it would complicate the implementation, and this is a PoC only.

I track the following issues within a thread:

  • Acquiring but not releasing.
  • Releasing an unaquired mutex.
  • Releasing out of order.

Between mutliply threads:

  • Acquiring & releasing not on the same order on different threads.

All in all, it took me about an hour to write this, so take that into account when you read it. Here is a sample code that explains how it works, first, here are our threads:

private static void OneTwoThreeAquireRelease()
{
   IMutex one = MutexRepository.Get("One");
   IMutex two = MutexRepository.Get("Two");
   IMutex three = MutexRepository.Get("Three");

   one.Aquire();
   two.Aquire();
   three.Aquire();

   three.Release();
   two.Release();
   one.Release();

   Assert.IsTrue(VerifyingMockMutexRepository.NoAcquiredMutexesOnThread);
}
private static void TwoOneThreeAquireRelease()
{
   IMutex one = MutexRepository.Get("One");
   IMutex two = MutexRepository.Get("Two");
   IMutex three = MutexRepository.Get("Three");
   
   two.Aquire();
   one.Aquire(); 
   three.Aquire();
   
   three.Release();
   one.Release(); 
   two.Release();
   Assert.IsTrue(VerifyingMockMutexRepository.NoAcquiredMutexesOnThread);
}

Notice that within each thread, they are perfectly okay, but when you mix them... you get a potential dead lock. And here is the test:

[Test]
public void AcuireAndReleaseInWrongOrderOnDifferentThreads()
{
    Thread[] threads = new Thread[2];
    threads[0] = new Thread(OneTwoThreeAquireRelease);
    threads[1] = new Thread(TwoOneThreeAquireRelease);

    RunAllThreadsAndWaitForCompletion(threads);
    
    IList<string> problems = VerifyingMockMutexRepository.GetAllProblems();
    Assert.AreEqual(2, problems.Count);
    Assert.AreEqual("Mutex One is in circular relationship with Two", problems[0]);
    Assert.AreEqual("Mutex Two is in circular relationship with One", problems[1]);
}

private static void RunAllThreadsAndWaitForCompletion(ICollection<Thread> threads)
{
    foreach (Thread thread in threads)
    {
        thread.Start();
    }
    foreach (Thread thread in threads)
    {
        thread.Join();
    }
}

Viola! Success, we have liftoff :-D We have successfully detected a potential deadlock. The code is structure to a IMutex and IMutexRepository, where the MutexRepository is merely the access point for that, so it's very easy to switch that for tests (or you can use DI or Castle to do it for you.)

You can get the code here. Again, this is a Proof Of Concept only, so have pity on me when you point out that when this or that happens, everything blows apart. I tried to make it work correctly in all cases, but I probably missed something.

Why I didn't use Rhino Mocks? Well, I could, but it just wasn't worth the time & trouble when I started from scratch and I had to execute behavior on the tests. If I had an existing interface, I would've probably used it, though.

time to read 1 min | 157 words

Here is an interesting question, I got several reqeusts to add an EntityList<T> to NHibernate Generics. One of the more interesting attributes on NHibernate Generics is that I can execute an action when an item is added or removed to the set. The problem that I've now is adapting it to the indexer of IList<T>, how I should behave with regard to it? There are three possibilities:

  • Override a value with the same value - do nothing
  • Override a value with a new value - call remove on the old value and add on the new value
  • Override a value with a ne value already on the list - what do I do here?

Any suggestions?

More on SQL

time to read 2 min | 211 words

Just thought to let you know, I just changed a cursor-based query to use a set base approach, and the performance just shot thought the roof. From over 5 minutes for the cursor approach (which examined the data and then executed several statements for each row) to set base approach (which execute several statements for the whole set) and run at roughly half a minute.

I will be the first to admit that I'm not a SQL guru, not even close to a Zen student level, actually. But I think that I can begin to like it, the possibilities are great, and I like to be able to say what, and not how. It gives the engine much more space to work with, and, I assume, Microsoft spent more time optimizing their engine than I can spend on optimizing my query J

Now all I need to do is wait for the whole process to run, and see if the results are any good, I've a pretty good feeling about it. Just to give you a hint about the difference in performance, the cursor based query (which run on test data, much smaller than the real one, of course) would issue ~80,001 queries. The set base approach issues 2.

SQL Fun

time to read 2 min | 389 words

Recently I've been dealing with a lot of SQL code, and it's has been very interesting to see all the myriad of ways that you can botch a query J.  One thing that I encountered is the tendecy to keep thinking procedurally instead of set-base. A lot of people seems to be famliar with inserting from select, like this:

(This will duplicate all the blogs in the table, not very useful in this instance, admittedly, but very useful elsewhere):

insert into Blogs

(

      blog name

)

select blog name

      from Blogs

 

But not a lot of people seem to be familiar with the Update counterpart for this statement. Let's take an example, I'm hosting a blogging server, and I found out that some of my users where using nasty words in their blog's name. Therefor, I decided that I want to set all the blogs' names to <User Name>'s Blog. How do I do that? The code that I've seen lately opens a cursor, and iterate over each and every one of the rows, update each in turn.  This is procedural thinking, and usually ineffiecnt. It's much better to do it via a set base command, like this one:

 

update Blogs

set blog name = u.user name + '''s Blog'

from Users u, UsersBlogs ub

where u.user id = ub.user id and

      Blogs.blog id = ub.blog id

 

This command even hanldes a many-to-many relationship between users and blogs (it uses the one of the blog's owners). It's not as obvious, and it's a bit harder to discover, but I think it is better. It was actually quite fun to re-write it this way, it made it much easier to read and understand. I spares you the reams of code that you would need to write this command using cursors.

time to read 2 min | 380 words

A brand new release of Rhino Mocks, with a twist.

Jeff emailed me a patch yesterday, that enabled the mocking of delegates. This is not something that I would've ever considered, to tell you the truth, but the idea (and the code) were sound, and I can certainly see uses for this. Consider all the possibilities for near-functional programming that .Net 2.0 opens for us, and you'll see what I'm excited about.

Here is an example of using this new functionality:

[Test]
public void GenericDelegate()
{
  Action<int> action = mocks.CreateMock<Action<int>>();
  for (int i = 0; i < 10; i++)
  {
    action(i);
  }
  mocks.ReplayAll();
  ForEachFromZeroToNine(action);
  mocks.VerifyAll();
}
private void ForEachFromZeroToNine(Action<int> act)
{
  for (int i = 0; i < 10; i++)
  {
    act(i);
  }
}

The other big change is removing IDisposable from the MockRepository. This is a breaking change for somet people, but it is a must. The problem is that the #1 problem that we have with new users of Rhino Mocks is exceptions masking. Removing the IDisposable means that it takes a bit longer to setup the mock repository, but I hope it will also encourage the recommended use of putting the setup & verification in the [SetUp] and [TearDown] methods.

Another change that I made was a bug fix for SetupResult not respecting IgnoreParameters(), which drove me crazy for a while. I’ve updated the documentation and the API docs to reflect the changes . As usualy, you can download or get the source.

I Hate MySQL

time to read 1 min | 143 words

An interesting discovery: TEXT columns in MySQL will only hold 65,000 bytes.

The problem is that when you hit this limit, MySQL will just silently corrupt your data, instead of throwing an exception, like it should be.

I got this problem when I tried to edit Rhino Mocks' documentation, and the size of it was just over 64Kb. I wasted quite a bit of time over this, thinking that maybe the limitation was in ASP.Net, Cuyahoga, NHibernate, etc.

Ugly, Ugly, Ugly.

I know that a lot of people have complained about MySQL liberalalism when saving saving data, but I just can't believe that they are doing this. What is the point in putting the burden of validation on the developer? That is what database are for!!

time to read 2 min | 366 words

Okay, it's about time that I update this thing. Now it's properly versions in the NQA repository, and you can reach it via: svn://svn.berlios.de/nqa/NHibernate.Generics/ or via: http://svn.berlios.de/wsvn/nqa/NHibernate.Generics/.

There are some new stuff there as well:

  • Tests for ManyToMany, which a lot of people requested.
    • Pay attention to many-to-many, lazy loading, and the edge case I talk about here.
    • I added an overload for this, and you must use this overload when you are creating the collection that will be the authoritative source (inverse=false, in the mapping). Check the User class in the tests for the detials.
  • Proper handling of Clear() on EntitySet
  • Introducing the AllowModification, and this one require some explenation. I found myself needing to modify the EntitySet<T> from within the action for adding and removing items. This caused problems, because ordinarily this would mean that I would get an infinite loop, but no worries, you can see how I solved it below (which is why I need the DisposableAction for). There is a similar property for the EntityRef<T>.
   1:  //Require a class member variable, can't use a local
   2:  //variable, because of delegates rules.
   3:  _posts = new EntitySet<Post>(
   4:  delegate(Post p)
   5:  {
   6:      using (_posts.AllowModifications)
   7:      {
   8:          _posts.Add(new Post("Duplicate"));
   9:      }
  10:  }, null);
  11:  _posts.Add(new Post("Original"));
  12:  Assert.AreEqual(2, _posts.Count);

That is all for now, I think. I changed the download to be just the compiled dll (you can get the source from the repository above). Most important of them all, I updated the documentation with the new stuff.

One interesting tidbit, so far I had over 950 dowloads of NHibernate Generics!

FUTURE POSTS

  1. fsync()-ing a directory on Linux (and not Windows) - about one day from now

There are posts all the way to Jun 09, 2025

RECENT SERIES

  1. Webinar (7):
    05 Jun 2025 - Think inside the database
  2. Recording (16):
    29 May 2025 - RavenDB's Upcoming Optimizations Deep Dive
  3. RavenDB News (2):
    02 May 2025 - May 2025
  4. Production Postmortem (52):
    07 Apr 2025 - The race condition in the interlock
  5. RavenDB (13):
    02 Apr 2025 - .NET Aspire integration
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats
}