Skip to content

Timeout value not resetting on Retry of failed test #2786

Closed

Description

@db798

Not sure if this has been raised before, couldn't find a similar issue.

I have a test with both Retry and Timeout attribute but when a test is retried the timeout isn't being reset to 0.

using NUnit.Framework;
using System;
using System.Threading;

namespace RetryTimeoutTest
{
    public class Class1
    {
        private static int count1 = 0;
        private static int count2 = 0;

        [Test]
        [Retry(3)]
        public void Test1()
        {
            count1++;
            Console.WriteLine(count1);

            Thread.Sleep(2000);

            if (count1 < 2)
            {
                Console.WriteLine("In failing assert");
                Assert.True(false);
            }
            else
            {
                Console.WriteLine("In passing assert");
                Assert.True(true);
            }
        }

        [Test]
        [Retry(3)]
        [Timeout(3000)]
        public void Test2()
        {
            count2++;
            Console.WriteLine(count2);

            Thread.Sleep(2000);

            if (count2 < 2)
            {
                Console.WriteLine("In failing assert");
                Assert.True(false);
            }
            else
            {
                Console.WriteLine("In passing assert");
                Assert.True(true);
            }
        }
    }
}

Expected Result: Both tests to pass after a retry

Actual Result Test1 passes as expected but Test2 fails with message Test exceeded Timeout value of 3000ms.

It looks like the timeout isn't being reset when the test starts the second time, is this the expected result or should a reset be performed for each retry of the tests.

ps sorry about the code sample, doesn't look to be formatting everything

@ChrisMaddock edit: fixed formatting

Activity

  1. ChrisMaddock commented on Mar 28, 2018

    @ChrisMaddock
    Contributor

    This sounds to me like an edge case we haven't thought of. I agree I'd expect it to work as you say.

    Will just wait for someone else on @nunit/framework-team to confirm!

  2. jnm2 commented on Mar 28, 2018

    @jnm2
    Contributor

    Yes, I can't think of any good way to use timeouts with retries the way it currently.

    We'll have to make sure the timeout command wraps the test command before any attributes get a chance to wrap it. Would that change any behavior in combination with other attributes?

  3. CharliePoole commented on Mar 28, 2018

    @CharliePoole
    Member

    I think you can consider this confirmed. Rationale: Timeout works at the WorkItem level. Retry works at the command level. Since the work item executes the command chain, the single timeout value applies to the aggregate of the repetitions.

    Changing this would probably mean re-implementing timeout at the command level, since attributes don't generally get to play at the WorkItem level.

  4. DaiMichael commented on May 2, 2018

    @DaiMichael
    Contributor

    If you'll let me... I'll have a crack at this and look at what @CharliePoole has suggested.

  5. mikkelbu commented on May 3, 2018

    @mikkelbu
    Member

    @DaiMichael You are most welcome. I've assigned to the issue to myself, so that nobody starts to work on it. I'll try to help as best I can, but I don't have much knowledge about this area, so it can be we will need some help from @CharliePoole or @jnm2 😄.

  6. self-assigned this
    on May 3, 2018
  7. jnm2 commented on May 3, 2018

    @jnm2
    Contributor

    This could be a tough one, but totally doable with enough persistence from all sides! Given the latter, I'm in favor of it! 😃 Charlie is a great one to get input from on this.

    @CharliePoole I have a question, already: How do we make sure that the suggested timeout command is always wrapped inside the retry command and not vice versa? We don't have an existing mechanism to control the ordering of command wrappers, do we?

  8. CharliePoole commented on May 3, 2018

    @CharliePoole
    Member

    The NUnit V2 TestDecorator addins used a priority level to sort them. That turned out to be problematic, since every addin would have to know it's own desired order with respect to every other addin in order to really use it correctly. IOW, it would work for "internal" addins but not user-creted ones.

    For NUnit 3, I figured we could have multiple "slots" for addins, with a separate derived marker interface for each of them. I implemented two slots, one to wrap the test itself and one to wrap SetUp / TearDown. I envisioned a third one wrapping and perhaps modifying the final result, but decided not to implement it till needed. This might be the time to think about adding a slot.

    Currently, three attributes implement IWrapSetUpTearDown:

    • MaxtimeAttribute
    • RetryAttribute
    • RepeatAttribute

    TimeoutAttribute doesn't implement it but instead sets a test property and modifies the execution context. NUnit's internal code takes care of creating the command wrapper. The wrapper is added last of all, on top of all other wrappers, which makes the timeout apply to any repetition.

    One simple solution would be to create an interface for repeated tests... like IRepeatedTest. We could use that for Retry and Repeat and apply the wrappers after timeout is applied.

    We would also have the option of checking that there is only one IRepeatedTest applied to each method. As an alternative, we should warn people that using both Retry and Repeat on the same method will produce non-deterministic results.

  9. jnm2 commented on May 3, 2018

    @jnm2
    Contributor

    What if RetryAttribute and RepeatAttribute could indicate that they must not be wrapped by typeof(MaxTimeAttribute) or typeof(TimeoutAttribute)? Then we do a topological sort and wrap in the correct order?

    I hope we can come up with something generalizable that isn't specific to repeating or specific to measuring time.

  10. CharliePoole commented on May 3, 2018

    @CharliePoole
    Member

    That's precisely the kind of thinking that went into the original V2 implementation. It failed because assigning an arbitrary mapping like that requires global knowledge of all possible attributes, including those created by users (or us) in the future. It doesn't suit an implementation, which is by its very nature, a decentralized aggregation of unconnected (attribute) nodes.

    OTOH if we say: here are the possible kinds of wrappers you can add and things within each category may be applied in any order then we force authors to create attributes that are aware of the possibility that other attributes may pile on top of them or be found beneath them. They have to write the wrapper code in a more robust way in order for all combinations to work.

    A categorization of wrappers by what they want to wrap makes the most sense to me. We have now...

    1. Wrappers that wrap a test method
    2. Wrappers that wrap the "whole" test (setup+method+teardown)
    3. Wrappers that repeat things (proposed)

    I understand the urge to do something extremely general. It's the way we were all trained to think. It's also the way that many of us deliberately cast aside in the 90s, when we saw it wasting more time in the present than it saved in the future.

    This is something that needs design, obviously. But it doesn't need to be designed to accommodate future needs that may come up. We'll never have to back out something we didn't implement!

    My design for wrapping tests (in my head) was "Create categories of things to be wrapped." My implementation was "Create two categories right now and see if more are needed later." If it were me, I'd simply follow up by adding another category before giving up on the entire concept.

  11. DaiMichael commented on May 3, 2018

    @DaiMichael
    Contributor

    I get the gist of what is being said, I will take a look at the option @CharliePoole is suggesting.

    I have created a couple of unit tests as per @ChrisMaddock and I will suplement them when I get going. I've tweeked them to have short Sleep() as is.

    I have created a branch issue-2786 (on my fork) as well which I'll work off.

    I'll keep everyone posted!

  12. CharliePoole commented on May 3, 2018

    @CharliePoole
    Member

    @DaiMichael My suggestion, of course, involves a moderately significant internal change to how NUnit applies attributes to tests. We'll point you to the places where you may need to make changes. As a starter, you'll see that the wrapping of commands takes place in SimpleWorkItem.cs

    Fair warning... because this is a fairly important change, expect lots of reviewing. 😈

  13. DaiMichael commented on May 6, 2018

    @DaiMichael
    Contributor

    Apologies for radio silence, I didn't run for the hills, busy in work and it is unbelieveably not raining in the UK so been riding my bike!

    @CharliePoole Looking forward to it 😩 😉 ...

    Here is a brief investigation with Charlies suggestion

    // Repeatable rather than Repeated as a retry doesn't necessary 
    // repeat as it is optional on outcome. Happy to go 
    // with Charlie suggestion if not liked?
    interface IRepeatableTest { }
    
    // Add interface to commands
    public class RetryCommand : DelegatingTestCommand, IRepeatableTest
    public class RepeatedTestCommand : DelegatingTestCommand, IRepeatableTest
    
    // Need to do a bit more debugging / investigation for full solution. But...
    // Change the command order in regards to 
    // IRepeatableTest (i.e. repeat after timeout command) to following method.
    SimpleWorkItem.MakeTestCommand()
    

    If this seems like a fair way to proceed, I will on this basis and flag any questions or issue / gotchas ?

  14. 2 remaining items

  15. DaiMichael commented on May 8, 2018

    @DaiMichael
    Contributor

    @jnm2 @CharliePoole

    I have a working solution, with tests that uses Charlie's interface idea and I used Joseph's naming IWrapTestRepeat. It will also makes the Test which has both 'RetryAttributeandRepeatAttribute` have a warning with a message.

    It was relatively straight forward, so I hope there is nothing controversial in there.

    Code is checked in on my branch, shall I do a Pull Request?

  16. jnm2 commented on May 8, 2018

    @jnm2
    Contributor

    Code is checked in on my branch, shall I do a Pull Request?

    As soon as you would like reviews, yes please! It's nice to have something in front of us.

  17. CharliePoole commented on May 8, 2018

    @CharliePoole
    Member

    @jnm2 This is definitely a nit, but here goes...

    Current naming pattern for this family of interfraces is IVerbObject, where Verb and Object describe what the interface does in the construction or execution of a test.

    IApplyToTest applies changes to the test itself
    IApplyToContext applies changes to the test execution context
    IWrapTestMethod wraps the execution of a test method
    IWrapSetUpTearDown wraps the setup and teardown
    IRepeatTest repeats the test execution

    The "-able" convention for interfaces is a good one - it's just not the one we have been using for attributes.

    IWrapTestRepeat would appear to mean that the interface wraps the repetition of a test rather than actually repeating the test.

  18. jnm2 commented on May 8, 2018

    @jnm2
    Contributor

    IWrapTestRepeat would appear to mean that the interface wraps the repetition of a test rather than actually repeating the test.

    Oops—and that's what I was accidentally envisioning. IRepeatTest is better because it is responsible to cause the repetition.

  19. CharliePoole commented on May 9, 2018

    @CharliePoole
    Member

    @DaiMichael Has a nice fix of this issue in PR #2849. Aside from the basic fix, there's the question of issuing a warning if multiple IRepeatTest attributes are used. I think we need to discuss what we want to happen and then how to implement a warning, if that's what we decide. This is a kind of warning we have never had before, so how we do it will create some kind of precedent.

    WHAT WE MIGHT DO

    There are a range of options...

    1. Do nothing... document that multiple repeat attributes together may interfere with one another and are not recommended. User deals with any mess they create. This is what we do with multiple combining attributes, for example, like Combinatorial and PairWise.

    2. Create some kind of Warning status which shows up as a warning result on the test if it otherwise succeeds.

    3. Use an actual warning (Assert.Warn) that will combine with any other warnings and errors that show up.

    4. Mark the test as invalid and don't allow it to run at all.

    On top of all this, I think we need to ask whether this is something NUnit should do centrally or each repeating attribute should do on its own if it can't work with other repeating attributes.

    HOW TO WARN

    If we decide to warn, I kind of think it should use the standard warning facility. That way, other warnings or failures will show up as well.

  20. DaiMichael commented on May 10, 2018

    @DaiMichael
    Contributor

    I'm leaning towards option 4 or a derivative (Skip with warning etc...).

    Whilst I accept generally we can't protect the overall usage of the framework against every eventuality, with repeatable tests it could cause problems and might be worth guarding against. Obviously someone could do [Retry(99999999)] but that is their problem (although we could but a limit in!). But if we get a race condition between 2 repeatable attributes (one could be a custom written one) that only manifests itself on a server and not locally, this might be hard to discover plus randomly hang up a server/build/process with no helpful message. By making the test Skipping/Invalid/etc.. and warning at least we don't get to a situation where issues might occur. We might save ourselves further issues being raise here! 😉

    My 2 cents..

  21. jnm2 commented on May 16, 2018

    @jnm2
    Contributor

    I think we should mark as invalid (4) and do this centrally. This blocks the area off, giving us freedom to do whatever we want without fear of backwards compatibility down the road if someone ever asks for the ability to combine repeating attributes.

  22. CharliePoole commented on May 16, 2018

    @CharliePoole
    Member

    That would be my preference as well... math any test with more than one such attribute as invalid.

  23. DaiMichael commented on May 16, 2018

    @DaiMichael
    Contributor

    Okay, will proceed with this.

  24. added a commit that references this issue on Jun 10, 2018
    74b32ba
  25. added a commit that references this issue on Sep 6, 2018
    fbcc921
  26. added a commit that references this issue on Sep 6, 2018
    6be3703
  27. added this to the 3.11 milestone on Sep 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions