Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/591#discussion_r12130230
--- Diff: core/src/test/scala/org/apache/spark/util/FileLoggerSuite.scala
---
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.util
+
+import java.io.IOException
+
+import scala.io.Source
+import scala.util.Try
+
+import org.apache.hadoop.fs.Path
+import org.scalatest.{BeforeAndAfter, FunSuite}
+
+import org.apache.spark.SparkConf
+import org.apache.spark.io.CompressionCodec
+
+/**
+ * Test writing files through the FileLogger.
+ */
+class FileLoggerSuite extends FunSuite with BeforeAndAfter {
+ private val fileSystem = Utils.getHadoopFileSystem("/")
+ private val allCompressionCodecs = Seq[String](
+ "org.apache.spark.io.LZFCompressionCodec",
+ "org.apache.spark.io.SnappyCompressionCodec"
+ )
+ private val logDir = "/tmp/test-file-logger"
+ private val logDirPath = new Path(logDir)
+
+ after {
+ Try { fileSystem.delete(logDirPath, true) }
+ }
+
+ test("Simple logging") {
+ testSingleFile()
+ }
+
+ test ("Simple logging with compression") {
+ allCompressionCodecs.foreach { codec =>
+ testSingleFile(Some(codec))
+ }
+ }
+
+ test("Logging multiple files") {
+ testMultipleFiles()
+ }
+
+ test("Logging multiple files with compression") {
+ allCompressionCodecs.foreach { codec =>
+ testMultipleFiles(Some(codec))
+ }
+ }
+
+ test("Logging when directory already exists") {
+ // Create the logging directory multiple times
+ new FileLogger(logDir, new SparkConf, overwrite = true)
+ new FileLogger(logDir, new SparkConf, overwrite = true)
+ new FileLogger(logDir, new SparkConf, overwrite = true)
+
+ // If overwrite is not enabled, an exception should be thrown
+ intercept[IOException] { new FileLogger(logDir, new SparkConf,
overwrite = false) }
+ }
+
+
+ /* ----------------- *
+ * Actual test logic *
+ * ----------------- */
+
+ /**
+ * Test logging to a single file.
+ */
+ private def testSingleFile(codecName: Option[String] = None) {
+ val conf = getLoggingConf(codecName)
+ val codec = codecName.map { c => CompressionCodec.createCodec(conf) }
+ val logger =
+ if (codecName.isDefined) {
+ new FileLogger(logDir, conf, compress = true)
+ } else {
+ new FileLogger(logDir, conf)
+ }
+ assert(fileSystem.exists(logDirPath))
+ assert(fileSystem.getFileStatus(logDirPath).isDir)
+ assert(fileSystem.listStatus(logDirPath).size === 0)
+
+ logger.newFile()
+ val files = fileSystem.listStatus(logDirPath)
+ assert(files.size === 1)
+ val firstFile = files.head
+ val firstFilePath = firstFile.getPath
+
+ logger.log("hello")
+ logger.flush()
+ assert(readFileContent(firstFilePath, codec) === "hello")
+
+ logger.log(" world")
+ logger.close()
+ assert(readFileContent(firstFilePath, codec) === "hello world")
+ }
+
+ /**
+ * Test logging to multiple files.
+ */
+ private def testMultipleFiles(codecName: Option[String] = None) {
+ val conf = getLoggingConf(codecName)
+ val codec = codecName.map { c => CompressionCodec.createCodec(conf) }
+ val logger =
+ if (codecName.isDefined) {
+ new FileLogger(logDir, conf, compress = true)
+ } else {
+ new FileLogger(logDir, conf)
+ }
+
+ logger.newFile("Jean_Valjean")
+ logger.logLine("Who am I?")
+ logger.logLine("Destiny?")
+ logger.newFile("John_Valjohn")
--- End diff --
Oops, my bad. It's hard to get this one right.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---