Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10596

IF-condition evaluation on the non-null object sometimes doesn't return true

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.10
    • 3.0.11, 4.0.3
    • None
    • None

    Description

      Context

      We use Java 8 (slightly different minor versions on local machines and some ENVs)

      java version "1.8.0_311"
      java version "1.8.0_321"

      Issue started happening after Groovy upgrade from 3.0.9 to 3.0.10

      (Case 1) Test fails when run using "mvn clean install"

      Tested code sample:

      @CompileStatic
      class MetainfoHelper {
      
          public static final String SCHEMA_KEY_IN_JSON = "\$schema"
          public static final String SCHEMA_KEY_IN_DB = "_\$schema"
      
          public static void convertSchemaKeyForSavingIntoDB(Document document) {
              String schema = document.getString(SCHEMA_KEY_IN_JSON)
              if (schema) {
                  document.remove(SCHEMA_KEY_IN_JSON)
                  document.put(SCHEMA_KEY_IN_DB, schema)
              }
          }
      
          public static void convertSchemaKeyAfterReadingFromDB(Document document) {
              String schema = document.getString(SCHEMA_KEY_IN_DB)
              if (schema) {
                  document.remove(SCHEMA_KEY_IN_DB)
                  document.put(SCHEMA_KEY_IN_JSON, schema)
              }
              document.remove(ID_IN_DB)
          }
      
      } 

       

      Test sample:

      @CompileStatic
      @ExtendWith(MockitoExtension.class)
      class MetainfoHelperTest {
      
          @Test
          void convertSchemaKeyForSavingIntoDBTest() {
              Document document = new Document(MetainfoHelper.SCHEMA_KEY_IN_JSON, "http://json-schema.org/draft-04/schema#")
              MetainfoHelper.convertSchemaKeyForSavingIntoDB(document)
              assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_JSON)).isNull()
              assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_DB)).isNotNull()
          }
      
          @Test
          void convertSchemaKeyBackFromDB() {
              Document document = new Document(MetainfoHelper.SCHEMA_KEY_IN_DB, "http://json-schema.org/draft-04/schema#")
              MetainfoHelper.convertSchemaKeyAfterReadingFromDB(document)
              assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_DB)).isNull()
              assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_JSON)).isNotNull()
          }
      
      } 

       

      (Case 2) At some point of time during the runtime the condition with non-null object reference started to be evaluated as false

      Affected code part sample:

      @CompileStatic
      class DbAcquisitionService implements IDistributedAcquisitionService {
      
          @Override
          <T extends Acquirable> T tryAcquire(Class<T> clazz, Map<String, ?> filter, String ownerKey, String ownerInstance, boolean createIfMissing = false) { {
              final updated = dao.findAndModify(query, updateOperations) as T
              systemLogger.debug("tryAcquire - find and modify completed, updated is [$updated]")
              if (updated) {
                  systemLogger.debug("tryAcquire - updated is true")
                  return updated
              } else {
                  systemLogger.debug("tryAcquire - updated is not true, can't acquire")
                  return null
              }
          }
      
      } 

       

      Logs at the time of issue:

      2022-04-22 06:23:46.487 - DEBUG - [scheduling-pool-3][80] - c.c.k.c.d.DbAcquisitionService - [] : tryAcquire - find and modify completed, updated is [BpmReceivedTask(bpKey:kmc, taskId:a422a3f0-d37d-4579-81bf-38d7a758ee79)]
      2022-04-22 06:23:46.487 - DEBUG - [scheduling-pool-3][80] - c.c.k.c.d.DbAcquisitionService - [] : tryAcquire - updated is not true, can't acquire

      Fix we made to resolve the issue

      In both cases in order to fix the issue the next change was made:

      if (object) {
      }

      ==>

      if (object != null) {
      }

       

       

      Do you have any ideas or suggestions why that could happen?

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              pavel.golovenkov Pavel Golovenkov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: