commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Waldemar Maier (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (LANG-1144) Multiple calls of org.apache.commons.lang3.concurrent.LazyInitializer.initialize() are possible
Date Wed, 10 Jun 2015 21:02:00 GMT

     [ https://issues.apache.org/jira/browse/LANG-1144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Waldemar Maier updated LANG-1144:
---------------------------------
    Description: 
It is possible to create a construct, that allows multiple calls of LazyInitializer.initialize,
when calculations (which can be very expensive) return null as result. 
The Junit Test can be something like this:
*(fix can be appplied from attached patch-file)*
{code:java}
package edu.test;

import static org.junit.Assert.assertEquals;

import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.apache.commons.lang3.concurrent.LazyInitializer;
import org.junit.Test;

public class LazyInitializerTest {

  private int lazyinitCounter = 0;

  private LazyInitializer<Object> lazyIinit = new LazyInitializer<Object>() {

    @Override
    protected Object initialize() throws ConcurrentException {
      lazyinitCounter++;
      return doSomeVeryExpensiveOperations();
    }
  };
  
  
  private Object doSomeVeryExpensiveOperations() {
    // do db calls
    // do some complex math calculations
    // the result of them all is null
    return null;
  }
  
  
  @Test
  public void testInitialization() throws Exception {
    lazyIinit.get();
    lazyIinit.get();
    assertEquals("Multiple call of LazyInitializer#initialize", 1, lazyinitCounter);
  }

}


{code}

  was:
It is possible to create a construct, that allows multiple calls of LazyInitializer.initialize,
when calculations (which can be very expensive) return null as result. 
The Junit Test can be something like this:
{code:java}
package edu.test;

import static org.junit.Assert.assertEquals;

import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.apache.commons.lang3.concurrent.LazyInitializer;
import org.junit.Test;

public class LazyInitializerTest {

  private int lazyinitCounter = 0;

  private LazyInitializer<Object> lazyIinit = new LazyInitializer<Object>() {

    @Override
    protected Object initialize() throws ConcurrentException {
      lazyinitCounter++;
      return doSomeVeryExpensiveOperations();
    }
  };
  
  
  private Object doSomeVeryExpensiveOperations() {
    // do db calls
    // do some complex math calculations
    // the result of them all is null
    return null;
  }
  
  
  @Test
  public void testInitialization() throws Exception {
    lazyIinit.get();
    lazyIinit.get();
    assertEquals("Multiple call of LazyInitializer#initialize", 1, lazyinitCounter);
  }

}


{code}


> Multiple calls of org.apache.commons.lang3.concurrent.LazyInitializer.initialize() are
possible
> -----------------------------------------------------------------------------------------------
>
>                 Key: LANG-1144
>                 URL: https://issues.apache.org/jira/browse/LANG-1144
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.concurrent.*
>    Affects Versions: 3.4
>         Environment: Java 1.8 on Windows 7 x64
>            Reporter: Waldemar Maier
>            Priority: Critical
>         Attachments: 0001-LANG-1144-allow-nulls-as-return-value.patch
>
>
> It is possible to create a construct, that allows multiple calls of LazyInitializer.initialize,
when calculations (which can be very expensive) return null as result. 
> The Junit Test can be something like this:
> *(fix can be appplied from attached patch-file)*
> {code:java}
> package edu.test;
> import static org.junit.Assert.assertEquals;
> import org.apache.commons.lang3.concurrent.ConcurrentException;
> import org.apache.commons.lang3.concurrent.LazyInitializer;
> import org.junit.Test;
> public class LazyInitializerTest {
>   private int lazyinitCounter = 0;
>   private LazyInitializer<Object> lazyIinit = new LazyInitializer<Object>()
{
>     @Override
>     protected Object initialize() throws ConcurrentException {
>       lazyinitCounter++;
>       return doSomeVeryExpensiveOperations();
>     }
>   };
>   
>   
>   private Object doSomeVeryExpensiveOperations() {
>     // do db calls
>     // do some complex math calculations
>     // the result of them all is null
>     return null;
>   }
>   
>   
>   @Test
>   public void testInitialization() throws Exception {
>     lazyIinit.get();
>     lazyIinit.get();
>     assertEquals("Multiple call of LazyInitializer#initialize", 1, lazyinitCounter);
>   }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message