Description: | In XMLReporter.java:158, the synchronized map, results ,
is iterated over in an unsynchronized manner, but according to the
Oracle Java 7 API specification,
this is not thread-safe and can lead to non-deterministic behavior.
This pull request adds a fix by synchronizing the iteration on results. |
Fix Description: |
(see diff) |
Violation Types: |
- missing/condition/synchronization
|
In File: | org/testng/reporters/XMLReporter.java |
In Method: | getSuiteAttributes(ISuite) |
Code with Misuse: |
class XMLReporter { private Properties getSuiteAttributes(ISuite suite) { Properties props = new Properties(); props.setProperty(XMLReporterConfig.ATTR_NAME, suite.getName());
// Calculate the duration Map<String, ISuiteResult> results = suite.getResults(); Date minStartDate = new Date(); Date maxEndDate = null; // TODO: We could probably optimize this in order not to traverse this twice for (Map.Entry<String, ISuiteResult> result : results.entrySet()) { ITestContext testContext = result.getValue().getTestContext(); Date startDate = testContext.getStartDate(); Date endDate = testContext.getEndDate(); if (minStartDate.after(startDate)) { minStartDate = startDate; } if (maxEndDate == null || maxEndDate.before(endDate)) { maxEndDate = endDate != null ? endDate : startDate; } }
// The suite could be completely empty if (maxEndDate == null) { maxEndDate = minStartDate; } addDurationAttributes(config, props, minStartDate, maxEndDate); return props; }
}
|
Code with Pattern(s): |
class SyncIterateResults {
ITestContext pattern(ISuite suite) {
// This invokation (may?) return a synchronized map.
Map<String, ISuiteResult> results = suite.getResults();
synchronized(results) {
for (ISuiteResult sr : results.values()) {
ITestContext context = sr.getTestContext();
return context; // do something with context
}
}
return null;
}
}
|