Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Reactive Extensions and ReactiveUI tests failures
nisbus
#1 Posted : Friday, August 23, 2013 5:09:48 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 8/23/2013(UTC)
Posts: 2
Location: Ireland

Was thanked: 1 time(s) in 1 post(s)
Hi,

I'm using Reactive Extensions and ReactiveUI (http://rx.codeplex.com/ & http://www.reactiveui.net/).

When I run my tests using the VS2010 test runner they all pass.
When NCrunch runs them they sometimes fail and sometimes pass (at random it seems).
This will always fail in NCrunch and always pass in VS2010.

Steps to reproduce:

Code:

    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using ReactiveUI;
    using System.Reactive.Concurrency;
    using Microsoft.Reactive.Testing;
    using System.Reactive.Linq;

    public class NCrunchBug : ReactiveObject
    {        
        IScheduler scheduler;
        List<NCrunchMessage> messages = new List<NCrunchMessage>();
        public List<NCrunchMessage> Messages
        {
            get { return messages; }
        }

        public NCrunchBug(IScheduler sched = null)
        {
            if (sched == null)
                scheduler = Scheduler.Default;
            else
                scheduler = sched;
            RegisterBusListener();
        }

        private void RegisterBusListener()
        {
            RxApp.MessageBus.Listen<NCrunchMessage>()
                .ObserveOn(scheduler)
                .Subscribe(message => messages.Add(message));
        }
    }

    public class NCrunchMessage
    {
        public string Message { get { return "NCRUNCH!!!"; } }
    }

    [TestClass]
    public class NCrunchTester
    {
        [TestMethod]
        public void MessageTest()
        {
            NCrunchBug bug = new NCrunchBug(Scheduler.CurrentThread);
            RxApp.MessageBus.SendMessage<NCrunchMessage>(new NCrunchMessage());
            Assert.AreEqual(1, bug.Messages.Count);
        }
    }


Also the send Report from the extension doesn't work :)

Cheers,
nisbus
Remco
#2 Posted : Friday, August 23, 2013 11:18:52 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi Nisbus,

Thanks for sharing this issue.

Am I correct in my understanding that this test is executing code across multiple threads?

I'm afraid that I have no knowledge of the how this framework behaves, although I suspect that this is a problem with the way the test is designed, rather than NCrunch itself. Because NCrunch executes the test much more frequently (and in a very different environment) to other test runners, there is a much high probability that race conditions within the test will be noticeable with NCrunch but not other test runners.

Intermittent failure of tests that execute code across multiple threads is highly likely to be the result of race conditions within either the test, or the code under test.

If this message bus listens for messages on a background thread, then there is a suspect race condition between the dispatch of the message and where it is received, as this test is checking for the existence of a received message on a different thread to that response for receiving the message.

If you aren't executing this code across multiple threads, check that the test is not somehow sequence dependent (i.e. can be confused by leftover state from previous execution runs of itself or other tests).

Relevant documentation links that you might want to look through: Multi-threaded tests, Test atomicity.


Cheers,

Remco
nisbus
#3 Posted : Monday, August 26, 2013 11:34:54 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 8/23/2013(UTC)
Posts: 2
Location: Ireland

Was thanked: 1 time(s) in 1 post(s)
Hi Remco,

Just in case others run into the same problem.
I figured this out and the solution is to put in the ClassInitialize this line:

RxApp.DeferredScheduler = Scheduler.CurrentThread;

This makes all the tests run successfully.

cheers,
nisbus
1 user thanked nisbus for this useful post.
Remco on 8/26/2013(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.036 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download