RajOpteamix / moq

Automatically exported from code.google.com/p/moq
Other
0 stars 0 forks source link

memory usage is off the charts if a logclass is mocked and creating using fixture setup and kept during a big text fixture #358

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a dummy class, that uses the mocked class, in this case the mocked 
class will be a logger
2. Create a mock and register it on fixture setup / set it to null on fixture 
teardown
3. Create a simple test that calls that mock from the dummy class with some 
params

What is the expected output? What do you see instead?
The memory should not increase exponentially...

Please use labels and text to provide additional information.
Please see the following example:
    public abstract class LoggerClass
    {
        public abstract void CallMe(string str);
    }

    public class RealClass
    {
        private readonly LoggerClass cls;

        public RealClass(LoggerClass cls)
        {
            this.cls = cls;
        }

        public void CallDummyMethod()
        {
            // to see the increase easier
            cls.CallMe(RandomString(1000));
        }

        private string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }
    }

    [TestFixture]
    public class TestClass
    {
        private Mock<LoggerClass> loggerMock;

        [TestFixtureTearDown]
        public void FixtureTeardown()
        {
            loggerMock = null;
        }

        [Test, Repeat(1000000000)]
        public void MoqMemoryIssueTest()
        {
            var cls = new RealClass(loggerMock.Object);
            cls.CallDummyMethod();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            loggerMock = new Mock<LoggerClass>();
        }
    }

If you run this, you will see that the memory will reach 1Gb ram withing a few 
seconds.

*NOTE* if the mock is recreated on setup, this would not be an issue - in 
theory, but i still don't understand, why is it storing anything if NO setups 
were made on the mock...

Original issue reported on code.google.com by emanuelv...@gmail.com on 10 Feb 2013 at 12:00