Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ScenarioState sometimes not injected while running multithreaded under TestNG #239

Closed
adrian-herscu opened this issue Oct 9, 2016 · 1 comment
Assignees
Milestone

Comments

@adrian-herscu
Copy link

adrian-herscu commented Oct 9, 2016

Using JGiven version 0.11.4 -- my test class looks like:

@Test(singleThreaded=true)
public class Caching extends PegasusRestTest {

    @Test
    public void shouldCacheProduct() {
        given_pegasus_machine_with_cache();

        dukesClientActions
            .when().deleting_key("1999757850_-1_-1");
        ....

It has few other similarly looking methods.

The deleting_key method is implemented as follows:

public class PegasusDukesClientActions<SELF extends PegasusDukesClientActions<SELF>>
    extends GenericActions<DukesClient, SELF> {

    @ExpectedScenarioState
    protected ThreadLocal<CacheConfiguration> targetDukesConfiguration;

    public SELF deleting_key(final String key) {
        log.debug("deleting key {}", key); //$NON-NLS-1$
        log.trace("targetDukesConfiguration = {}", targetDukesConfiguration);
        try (val dukesClient = new DukesClientWrapper<>(
            new InflateDeflateTranscoder(), targetDukesConfiguration.get())) {  <<<--- NPE here
            dukesClient.delete(key);
        .....

The targetDukesConfiguration is initialized like this:

public class PegasusDukesClientFixtures<SELF extends PegasusDukesClientFixtures<SELF>>
    extends GenericFixtures<DukesClient, SELF> {

    @ProvidedScenarioState
    protected final ThreadLocal<CacheConfiguration> targetDukesConfiguration =
        new ThreadLocal<>();

    public SELF a_Dukes_cache_at(final URI url, final int timeoutMs) {
        log.debug("target Dukes cache at {}", url); //$NON-NLS-1$
        targetDukesConfiguration.set(new CacheConfiguration() {
            {
                setHostsAndPorts(url.toString());
        .....

Hence I expect in the PegasusDukesClientActions stage to have targetDukesConfiguration initialized at least with a new empty ThreadLocal.

However, when I am setting @test(singleThreaded=false) on Caching class I am sporadically getting a NullPointerException when accessing targetDukesConfiguration in the deleting_key method implementation above.

Here is a log with just two test methods:

11:37:52.246 [DEBUG] [pool-1-thread-2]: PegasusDukesClientFixtures - target Dukes cache at http://somewhere/pools/productservicecache
11:37:52.263 [DEBUG] [pool-1-thread-1]: PegasusDukesClientFixtures - target Dukes cache at http://somewhere/pools/productservicecache
......
......
11:37:53.980 [DEBUG] [pool-1-thread-2]: PegasusDukesClientActions - deleting key 1510742630_-1_-1
11:37:53.980 [TRACE] [pool-1-thread-2]: PegasusDukesClientActions - targetDukesConfiguration = null
.....
11:37:54.043 [DEBUG] [pool-1-thread-1]: PegasusDukesClientActions - deleting key 1999757850_-1_-1
11:37:54.044 [TRACE] [pool-1-thread-1]: PegasusDukesClientActions - targetDukesConfiguration = null

Sometimes it works, but most of the times it does not. Easy to reproduce :)

@adrian-herscu adrian-herscu changed the title @ScenarioState is not injected while running multithreaded under TestNG @ScenarioState sometimes not injected while running multithreaded under TestNG Oct 9, 2016
@janschaefer janschaefer self-assigned this Oct 9, 2016
@janschaefer janschaefer added this to the v0.12.2 milestone Oct 9, 2016
@janschaefer
Copy link
Contributor

That one was quite tough :-D

@janschaefer janschaefer modified the milestones: v0.13.0, v0.12.2 Oct 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants