Potential bug in Grails unit testing (?)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Potential bug in Grails unit testing (?)

Thomas P. Fuller
Hi Folks,

I was working on my plugin recently and I may have discovered a defect in unit testing (this is a painful problem so it won't hurt to document what I did to fix it). Here are the details:

My plugin uses mixins to add functionality to the Infinispan distributed cache.

The first mixin added query functionality and did so as follows:

@Category (Cache)
class QueryCapabilities {
...some query methods
}

then in the InfinispanGrailsPlugin.groovy class:

def doWithDynamicMethods = { ctx ->              
        Cache.metaClass.mixin QueryCapabilities
}

The unit test for QueryCapabilities has the mixin added exactly as it is above in the setUp method and I follow this pattern for other unit tests that pertain to the new *Capabilities mixins.

* Unit tests pass when executed singularly (test-app -unit QueryCapabilities).
* Unit tests pass when executed altogether (test-app -unit).
* The example application runs fine.

So recently I added several new mixins and I notice a problem.

* Unit tests pass when executed singularly (test-app -unit QueryCapabilities).
* Unit tests DO NOT pass when executed altogether (test-app -unit) -- in fact all unit tests for *Capabilities classes are now failing. Investigation reveals that the extra methods appear in the classes they're being mixed into, but for some reason when they're called, the result is a MissingMethodException.
* The example application runs fine.

The difference between the example application and the unit tests is that the plugin adds all the mixins at once. The next step was to see if this would fix the problem, and I added a new test which is the first test that is executed when the "grails test-app -unit" is run.

class AAASetUpMixinsTests extends GrailsUnitTestCase {
   
    protected void setUp() throws Exception {
        Listenable.metaClass.mixin ListenerCapabilities
       
        EmbeddedCacheManager.metaClass.mixin EmbeddedCacheManagerListenerCapabilities
       
        Cache.metaClass.mixin CacheListenerCapabilities, QueryCapabilities
    }
   
    void testEmpty () {
        // Ignore this.
    }
}

And this fixes the problem.

Any thoughts on this?

Tom
 
Thomas P. Fuller, MSc
Managing Director

Coherent Logic Limited
High Performance Software Engineering

[hidden email]
IM: thospfuller (Yahoo)

Registered in England, #05560634



145-157 St. John Street
London, EC1V 4PY United Kingdom

work:  44.[0]207.788.7654
mobile:  44.[0]781.828.7465

Reply | Threaded
Open this post in threaded view
|

Re: Potential bug in Grails unit testing (?)

ld@ldaley.com

On 31/10/2010, at 7:06 PM, Thomas P. Fuller wrote:

> I was working on my plugin recently and I may have discovered a defect in unit testing (this is a painful problem so it won't hurt to document what I did to fix it). Here are the details:
>
> My plugin uses mixins to add functionality to the Infinispan distributed cache.
>
> The first mixin added query functionality and did so as follows:
>
> @Category (Cache)
> class QueryCapabilities {
> ...some query methods
> }
>
> then in the InfinispanGrailsPlugin.groovy class:
>
> def doWithDynamicMethods = { ctx ->              
>         Cache.metaClass.mixin QueryCapabilities
> }
>
> The unit test for QueryCapabilities has the mixin added exactly as it is above in the setUp method and I follow this pattern for other unit tests that pertain to the new *Capabilities mixins.
>
> * Unit tests pass when executed singularly (test-app -unit QueryCapabilities).
> * Unit tests pass when executed altogether (test-app -unit).
> * The example application runs fine.
>
> So recently I added several new mixins and I notice a problem.
>
> * Unit tests pass when executed singularly (test-app -unit QueryCapabilities).
> * Unit tests DO NOT pass when executed altogether (test-app -unit) -- in fact all unit tests for *Capabilities classes are now failing. Investigation reveals that the extra methods appear in the classes they're being mixed into, but for some reason when they're called, the result is a MissingMethodException.
> * The example application runs fine.
>
> The difference between the example application and the unit tests is that the plugin adds all the mixins at once. The next step was to see if this would fix the problem, and I added a new test which is the first test that is executed when the "grails test-app -unit" is run.
>
> class AAASetUpMixinsTests extends GrailsUnitTestCase {
>    
>     protected void setUp() throws Exception {
>         Listenable.metaClass.mixin ListenerCapabilities
>        
>         EmbeddedCacheManager.metaClass.mixin EmbeddedCacheManagerListenerCapabilities
>        
>         Cache.metaClass.mixin CacheListenerCapabilities, QueryCapabilities
>     }
>    
>     void testEmpty () {
>         // Ignore this.
>     }
> }
>
> And this fixes the problem.
>
> Any thoughts on this?


Only that it's very unlikely that this is a Grails issue (it looks like a Groovy problem). My guess is that the mixin mechanism has issues with mixins not being added at once.

Your best bet is to try and isolate in a small reproducible case and open an issue against Groovy.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Potential bug in Grails unit testing (?)

Thomas P. Fuller
Hi Luke,

Thanks for your response. I did consider that this could be a Groovy issue but figured I'd take this up with the Grails dev forum first in the event that this has been seen before.

Tom
 
 
Thomas P. Fuller, MSc
Managing Director

Coherent Logic Limited
High Performance Software Engineering

[hidden email]
IM: thospfuller (Yahoo)

Registered in England, #05560634



145-157 St. John Street
London, EC1V 4PY United Kingdom

work:  44.[0]207.788.7654
mobile:  44.[0]781.828.7465



From: Luke Daley <[hidden email]>
To: [hidden email]
Sent: Sun, 31 October, 2010 23:37:36
Subject: Re: [grails-dev] Potential bug in Grails unit testing (?)


On 31/10/2010, at 7:06 PM, Thomas P. Fuller wrote:

> I was working on my plugin recently and I may have discovered a defect in unit testing (this is a painful problem so it won't hurt to document what I did to fix it). Here are the details:
>
> My plugin uses mixins to add functionality to the Infinispan distributed cache.
>
> The first mixin added query functionality and did so as follows:
>
> @Category (Cache)
> class QueryCapabilities {
> ...some query methods
> }
>
> then in the InfinispanGrailsPlugin.groovy class:
>
> def doWithDynamicMethods = { ctx ->             
>        Cache.metaClass.mixin QueryCapabilities
> }
>
> The unit test for QueryCapabilities has the mixin added exactly as it is above in the setUp method and I follow this pattern for other unit tests that pertain to the new *Capabilities mixins.
>
> * Unit tests pass when executed singularly (test-app -unit QueryCapabilities).
> * Unit tests pass when executed altogether (test-app -unit).
> * The example application runs fine.
>
> So recently I added several new mixins and I notice a problem.
>
> * Unit tests pass when executed singularly (test-app -unit QueryCapabilities).
> * Unit tests DO NOT pass when executed altogether (test-app -unit) -- in fact all unit tests for *Capabilities classes are now failing. Investigation reveals that the extra methods appear in the classes they're being mixed into, but for some reason when they're called, the result is a MissingMethodException.
> * The example application runs fine.
>
> The difference between the example application and the unit tests is that the plugin adds all the mixins at once. The next step was to see if this would fix the problem, and I added a new test which is the first test that is executed when the "grails test-app -unit" is run.
>
> class AAASetUpMixinsTests extends GrailsUnitTestCase {
>   
>    protected void setUp() throws Exception {
>        Listenable.metaClass.mixin ListenerCapabilities
>       
>        EmbeddedCacheManager.metaClass.mixin EmbeddedCacheManagerListenerCapabilities
>       
>        Cache.metaClass.mixin CacheListenerCapabilities, QueryCapabilities
>    }
>   
>    void testEmpty () {
>        // Ignore this.
>    }
> }
>
> And this fixes the problem.
>
> Any thoughts on this?


Only that it's very unlikely that this is a Grails issue (it looks like a Groovy problem). My guess is that the mixin mechanism has issues with mixins not being added at once.

Your best bet is to try and isolate in a small reproducible case and open an issue against Groovy.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email