3

I feel like I may be going about something entirely wrong, so if you have an alternate approach, please let me know.

I'm building a service with service stack. I'm testing that service with Xunit and NCrunch. I want my tests to run in parallel, and they currently do thanks to NCrunch... However, there is one problem.

All of my tests rely on the fact that my service is up and running - it's a shared resource.. a dependency. I don't particularly care for it, but it seems that service stack does not let you spin up multiple instances for each test (plus, I'd need to do some nasty table prefixes or something inside postgresql - blek)...

All I need is some way to start my service via code before any tests have run - and stop it when all tests have finished. I need it to be via code because I don't receive code coverage information on server-side code if I have to start the service manually.

I can manually start the service and all my tests run in parallel and pass perfectly fine - but the problem is that I can't seem to find a good hook for a 'pre-test' and 'post-test' action.

I have looked into using a semaphore as a sort of 'reference counting' solution, but it's actually the opposite of what I'd need. I don't want to have to specify ahead of time how many tests will be ran.. I need something similar to a dijkstra counting semaphore - but I haven't found an implementation that works based upon a named (system-wide) semaphore.

A Mutex won't work - because the test that happens to start the service still has to know when all of the other tests finish before stopping the service - it cannot just do so in the Dispose method.

A lot of the normal solutions related to synchronizing objects between threads does not apply in this situation because the tests are ran inside their own AppDomain.

Thanks for all your help in advance.

syllogism
  • 655
  • 1
  • 4
  • 12
  • I know how I would do this with Python's unit testing framework: put all of the unit tests in a test-group, and add a before/after hook on the group itself. – jpaugh Jun 08 '13 at 17:00
  • @syllogism Are these tests intended to be unit tests for the service? Or are they supposed to be integration tests for your entire system? – mclark1129 Jun 10 '13 at 17:44
  • @syllogism, perhaps you can elaborate a bit more. I have recently implemented service-stack, where I wanted multiple instances and the ability to "spin up" and "spin down" services at will. In the end, I actually encased each web-service in its' own app domain and this has worked wonders for arbiturarily starting and stopping them. – Moo-Juice Jun 10 '13 at 17:46
  • @MikeC These are integration tests. Ran from the client perspective. – syllogism Jun 10 '13 at 18:15
  • @Moo-Juice The problem is that each test has a dependency on the service being started before the test is ran. I've attempted to spin up a new AppHost instance before each test with a random guid prefixed to the endpoint - but when I do - I received an exception that appears before any ServiceStack code. I really just need some way of starting the service via code before any tests have run and stop the service after all have finished. – syllogism Jun 10 '13 at 18:23
  • @syllogism, and you've also probably found out that ServiceStack won't allow more than one AppHost per App-Domain, which is why I isolated my services in their own app-domain. – Moo-Juice Jun 10 '13 at 18:31
  • @Moo-Juice Each test is ran inside it's own AppDomain to begin with, which is what I wrote above. – syllogism Jun 10 '13 at 18:39
  • 1
    Have you seen [xUnit - run code before and after ALL tests](http://stackoverflow.com/questions/13829737/xunit-run-code-before-and-after-all-tests)? I would think the unit testing framework would have a global Start/Stop method for you to hook into, but it looks like xUnit does not support that until 2.0. – Dustin Kingen Jun 11 '13 at 16:22

1 Answers1

1

I wrote a blog post about a solution to a similar problem a couple of years ago.

http://netvignettes.wordpress.com/2012/01/03/sharepoint-net-3-5-woes-and-synchronization-between-processes/

Scroll down to the code blocks and start reading around there, so you can skip the SharePoint cruft at the top.

Evan Machusak
  • 694
  • 4
  • 9
  • Thank you for that - I intend on trying it out in a few hours. I will grant you the +50 as soon as I verify that it's what I'm looking for =) – syllogism Jun 11 '13 at 21:42
  • @Your answer is exactly what I asked for, but even after implementing synchronization using an EventWaitHandle as you suggested - I'm seeing random failures due to the fact that I can't properly close the handle after all tests have finished (because I have no hook there either). I believe I either need to wait for Xunit 2.0 as mentioned above or just deal with manually running my service before starting my tests. Giving you the +50 for pointing me towards the most-likely-to-succeed possibility. – syllogism Jun 12 '13 at 00:38
  • Did you implement the reference counting mechanism I discussed? I didn't show the code for using a mapped memory file to share a single integer across appdomains/processes in the blog post but I have a sample of how to do that. You do know when a test is done, right? When the reference counter hits zero you close the handle (and terminate your service). – Evan Machusak Jun 13 '13 at 16:00