Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Properly close Sources, close #751

  • Loading branch information...
commit 2e45cdc0414c4af3668b045228158b751716b87c 1 parent 93985e5
@slandelle slandelle authored
View
24 gatling-charts/src/main/scala/com/excilys/ebi/gatling/charts/result/reader/FileDataReader.scala
@@ -15,7 +15,7 @@
*/
package com.excilys.ebi.gatling.charts.result.reader
-import java.io.File
+import java.io.{ FileInputStream, InputStream }
import java.util.{ HashMap => JHashMap }
import java.util.regex.Pattern
@@ -48,7 +48,7 @@ object FileDataReader {
class FileDataReader(runUuid: String) extends DataReader(runUuid) with Logging {
- private def multipleFileIterator(files: Seq[File]): Iterator[String] = files.map(file => Source.fromFile(file, configuration.simulation.encoding).getLines()).reduce((first, second) => first ++ second)
+ private def multipleFileIterator(streams: Seq[InputStream]): Iterator[String] = streams.map(Source.fromInputStream(_, configuration.simulation.encoding).getLines()).reduce((first, second) => first ++ second)
val inputFiles = simulationLogDirectory(runUuid, create = false).files.filter(_.jfile.getName.matches(FileDataReader.SIMULATION_FILES_NAME_PATTERN)).map(_.jfile).toSeq
@@ -77,8 +77,15 @@ class FileDataReader(runUuid: String) extends DataReader(runUuid) with Logging {
(runStart, runEnd, runRecords.head)
}
-
- val (runStart, runEnd, runRecord) = preProcess(multipleFileIterator(inputFiles))
+
+ val (runStart, runEnd, runRecord) = {
+ val streams = inputFiles.map(new FileInputStream(_))
+ try {
+ preProcess(multipleFileIterator(streams))
+ } finally {
+ streams.foreach(_.close)
+ }
+ }
val step = StatsHelper.step(math.floor(runStart / FileDataReader.SEC_MILLISEC_RATIO).toInt, math.ceil(runEnd / FileDataReader.SEC_MILLISEC_RATIO).toInt, configuration.charting.maxPlotsPerSeries) * FileDataReader.SEC_MILLISEC_RATIO
val bucketFunction = StatsHelper.bucket(_: Int, 0, (runEnd - runStart).toInt, step, step / 2)
@@ -106,7 +113,14 @@ class FileDataReader(runUuid: String) extends DataReader(runUuid) with Logging {
resultsHolder
}
- val resultsHolder = process(multipleFileIterator(inputFiles), bucketFunction)
+ val resultsHolder = {
+ val streams = inputFiles.map(new FileInputStream(_))
+ try {
+ process(multipleFileIterator(streams), bucketFunction)
+ } finally {
+ streams.foreach(_.close)
+ }
+ }
def requestNames: List[String] = resultsHolder
.requestNameBuffer
View
17 gatling-core/src/main/scala/com/excilys/ebi/gatling/core/feeder/csv/SeparatedValuesParser.scala
@@ -20,6 +20,7 @@ import scala.tools.nsc.io.Path
import com.excilys.ebi.gatling.core.config.{ GatlingConfiguration, GatlingFiles }
import com.excilys.ebi.gatling.core.util.FileHelper.{ COMMA_SEPARATOR, SEMICOLON_SEPARATOR, TABULATION_SEPARATOR }
+import com.excilys.ebi.gatling.core.util.IOHelper.use
object SeparatedValuesParser {
@@ -36,14 +37,18 @@ object SeparatedValuesParser {
if (!file.exists) throw new IllegalArgumentException("file " + file + " doesn't exists")
- val rawLines = Source.fromFile(file.jfile, GatlingConfiguration.configuration.simulation.encoding).getLines.map(_.split(separator))
+ use(Source.fromFile(file.jfile, GatlingConfiguration.configuration.simulation.encoding)) { source =>
- val lines = escapeChar.map { escape =>
- rawLines.map(_.map(_.stripPrefix(escape).stripSuffix(escape)))
- }.getOrElse(rawLines).toArray
+ val rawLines = source.getLines.map(_.split(separator))
- val headers = lines.head
+ val lines = escapeChar.map { escape =>
+ rawLines.map(_.map(_.stripPrefix(escape).stripSuffix(escape)))
+ }.getOrElse(rawLines).toArray
+
+ val headers = lines.head
+
+ lines.tail.map(line => (headers zip line).toMap)
+ }
- lines.tail.map(line => (headers zip line).toMap)
}
}
View
4 gatling-core/src/main/scala/com/excilys/ebi/gatling/core/util/IOHelper.scala
@@ -15,11 +15,9 @@
*/
package com.excilys.ebi.gatling.core.util
-import java.io.Closeable
-
object IOHelper {
- def use[T, C <: Closeable](closeable: C)(block: C => T) = {
+ def use[T, C <: { def close() }](closeable: C)(block: C => T) = {
try
block(closeable)
finally
Please sign in to comment.
Something went wrong with that request. Please try again.