question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Raise.With does not work properly in release mode

See original GitHub issue

Just try to execute this code in release and in debug mode. Debug works, release fails. Tried with FakeItEasy version 1.25.3

public interface ITestA
{
    event EventHandler Closed;
    event EventHandler Loaded;

    void DoStuff ();

    void Wicked ();
}

public interface ITestB
{
    void DoStuff ();

    void Wicked ();

    void Wicked1 (ITestA a);
}

public class TestB
{
    private readonly ITestA _a;
    private readonly ITestB _b;

    public TestB (ITestA a, ITestB b)
    {
        _a = a;
        _b = b;
    }

    public void Attach ()
    {
        WeakEventManager<ITestA, EventArgs>.AddHandler(_a, EventNames.Closed, Closed);
        WeakEventManager<ITestA, EventArgs>.AddHandler(_a, EventNames.Loaded, Loaded);
        _a.Wicked();
        _b.Wicked();
    }

    private void Loaded (object sender, EventArgs e)
    {
        _b.DoStuff();
    }

    private void Closed (object sender, EventArgs e)
    {
        _a.DoStuff();

        // _b.Wicked1(_a.Value);
    }
}

[TestFixture]
public class MyTest
{
    [Test]
    [Repeat (1000)]
    public void Test ()
    {
        var a = A.Fake<ITestA>();
        var b = A.Fake<ITestB>();

        var inst = new TestB(a, b);
        inst.Attach();
        A.CallTo(() => a.Wicked()).MustHaveHappened(Repeated.Exactly.Once);
        A.CallTo(() => b.Wicked()).MustHaveHappened(Repeated.Exactly.Once);

        a.Closed += Raise.With(null, new EventArgs()).Now;

        A.CallTo(() => a.DoStuff()).MustHaveHappened(Repeated.Exactly.Once);

        // A.CallTo(() => b.Wicked1(a)).MustHaveHappened(Repeated.Exactly.Once);
        a.Loaded += Raise.With(null, new EventArgs()).Now;
        A.CallTo(() => b.DoStuff()).MustHaveHappened(Repeated.Exactly.Once);
    }
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
blairconradcommented, Jul 27, 2015

@MrVermond, it doesn’t appear to be Raise. I sloppily rewrote the test so we could raise the events via a public method:

public class ITestA
{
    event EventHandler Closed;
    event EventHandler Loaded;

    public virtual void DoStuff() { }

    public virtual void Wicked() { }

    public void RaiseLoaded()
    {
        Loaded(this, EventArgs.Empty);
    }

    public void RaiseClosed()
    {
        Closed(this, EventArgs.Empty);
    }
}

public class ITestB
{
    public virtual void DoStuff() { }

    public virtual void Wicked() { }
}

public class TestB
{
    private readonly ITestA _a;
    private readonly ITestB _b;

    public TestB(ITestA a, ITestB b)
    {
        _a = a;
        _b = b;
    }

    public void Attach()
    {
        WeakEventManager<ITestA, EventArgs>.AddHandler(_a, "Closed", Closed);
        WeakEventManager<ITestA, EventArgs>.AddHandler(_a, "Loaded", Loaded);
        _a.Wicked();
        _b.Wicked();
    }

    private void Loaded(object sender, EventArgs e)
    {
        _b.DoStuff();
    }

    private void Closed(object sender, EventArgs e)
    {
        _a.DoStuff();
    }
}

[TestFixture]
public class MyTest
{
    [Test]
    [Repeat(1000)]
    public void Test()
    {
        var a = A.Fake<ITestA>();
        var b = A.Fake<ITestB>();

        var inst = new TestB(a, b);
        inst.Attach();
        A.CallTo(() => a.Wicked()).MustHaveHappened(Repeated.Exactly.Once);
        A.CallTo(() => b.Wicked()).MustHaveHappened(Repeated.Exactly.Once);

        a.RaiseClosed();
        A.CallTo(() => a.DoStuff()).MustHaveHappened(Repeated.Exactly.Once);

        a.RaiseLoaded();
        A.CallTo(() => b.DoStuff()).MustHaveHappened(Repeated.Exactly.Once);
    }
}

and got this failure:

Errors and Failures:                                                     
1) Test Error : LessFaking.MyTest.Test                                   
   FakeItEasy.ExpectationException :                                     

  Assertion failed for the following call:                               
    LessFaking.ITestB.DoStuff()                                      
  Expected to find it exactly once but found it #0 times among the calls:
    1: LessFaking.ITestB.Wicked()

This lead me to believe that the problem is that the runtime thinks we’re done with some objects so we can start unhooking events.

Indeed, adding this line to the bottom either of my new test or your original test causes them to pass:

Assert.That(inst, Is.Not.Null);

So I think we may just be seeing normal WeakEventManager behaviour.

0reactions
blairconradcommented, Jul 29, 2015

@MrVermond, I’m glad to have been of assistance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

My code does not work on Release mode while ...
A program that works in debugging mode but fails in release mode is often caused by a bad operation on a pointer (dereferencing...
Read more >
The program works in Debug mode but not in Release mode
Hey, I have been working on a program and it works perfectly in the CVI environment during debug mode but when I am...
Read more >
App works in debug mode but does not work in release mode
I am making a chat app..with firebase and CloudFirestore...in debug mode the app is working fine ..the chat and all is loading but...
Read more >
App doesn't work in release apk but works fine in debug ...
My app works fine in debug mode but when i make its release build blank screen comes up as soon as internet is...
Read more >
Is throwing exceptions in Debug mode a bad idea? [closed]
For that reason I want to log exceptions only in Release mode but not in Debug mode. Let's say my intention is to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found